SpringToolSuite中用maven简单搭建springmvc web项目《一》

2018-03-01 11:21:11来源:oschina作者:会唱歌的小孩纸人点击

分享

样例码云传送门:https://gitee.com/syuson/SpringMVC


一、创建Maven项目新建maven project
默认选择项,下一步
选择webapp,下一步
Group Id:组织唯一标识符,即对应项目包结构;Artifact Id:项目唯一标识符,即项目名称/根目录;Package:根据Group Id与Artifact Id会默认生成一个包,不填也可以。下一步
倘若发现资源文件夹缺失,须自行补充缺失的资源文件夹(文件未缺失,跳过5~7)
查看丢失文件
创建丢失文件
得到如下结构的web项目 二、配置pom.xml文件,添加对spring的依赖

pom.xml文件配置


xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4.0.0
com.test
springMVC
war
0.0.1-SNAPSHOT
springMVC Maven Webapp
http://maven.apache.org



4.0.0.RELEASE

1.1.3

3.0.1
2.2

1.2
1.1.2

3.8.1

1.8
2.3.2



org.springframework
spring-core
${spring.version}


org.springframework
spring-beans
${spring.version}


org.springframework
spring-context
${spring.version}


org.springframework
spring-jdbc
${spring.version}


org.springframework
spring-expression
${spring.version}


org.springframework
spring-web
${spring.version}


org.springframework
spring-webmvc
${spring.version}


org.springframework
spring-tx
${spring.version}



javax.servlet
javax.servlet-api
${servlet.version}
provided


javax.servlet.jsp
jsp-api
${jsp-api.version}
provided



javax.servlet
jstl
${jstl.version}


taglibs
standard
${standard.version}



com.fasterxml.jackson.core
jackson-core
2.5.2


com.fasterxml.jackson.core
jackson-databind
2.5.2



junit
junit
${junit.version}
test






org.apache.maven.plugins
maven-compiler-plugin
${maven.compiler.plugin.version}

${jdk.version}
${jdk.version}



springMVC

springMVC接收json参数须依赖如下jar




com.fasterxml.jackson.core
jackson-core
2.5.2


com.fasterxml.jackson.core
jackson-databind
2.5.2

修改完后clean项目会自动下载配置的jar包



三、配置spring-context.xml、spring-mvc.xml文件

在资源文件目录下创建spring-context.xml、spring-mvc.xml文件



spring-context.xml文件


<?xml version="1.0" encoding="UTF-8"?>
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"
default-lazy-init="false">
Spring公共配置








spring-mvc.xml文件


<?xml version="1.0" encoding="UTF-8" standalone="no"?>
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">














class="org.springframework.web.servlet.view.InternalResourceViewResolver">
value="org.springframework.web.servlet.view.JstlView" />






创建views文件夹



四、配置web.xml文件

web.xml


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
Archetype Created Web Application


contextConfigLocation
classpath:spring/spring-context.xml



org.springframework.web.context.ContextLoaderListener



spring
org.springframework.web.servlet.DispatcherServlet


contextConfigLocation
classpath:spring/spring-mvc.xml

1


spring
/



CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter

encoding
UTF-8


forceEncoding
true



CharacterEncodingFilter
/*


index.jsp


五、创建控制器与视图进行测试

创建HelloWorldController.java控制器


package com.test;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/hello-world")
public class HelloWorldController {
@RequestMapping(value = "/hello")
@ResponseBody
public ModelAndView hello(
@RequestParam(value = "name", required = false, defaultValue = "Maven SpringMVC") String name) {
// 指定视图
ModelAndView modelAndView = new ModelAndView("hello-world");
// 向视图中添加数据,供页面展示
modelAndView.addObject("message", "Hello world! My Name Is ");
modelAndView.addObject("name", name);
return modelAndView;
}
}

创建hello-world.jsp视图层


<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>




SpringToolSuite中用maven简单搭建springmvc web项目《一》



SpringToolSuite中用maven简单搭建springmvc web项目《一》


${message} ${name}





修改index.jsp视图层


<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>




SpringToolSuite中用maven简单搭建springmvc web项目《一》



SpringToolSuite中用maven简单搭建springmvc web项目《一》



点击跳转





最终项目结构



clean项目,在tomcat中部署服务,访问项目:http://localhost:8090/springMVC/ (根据本地环境,填写端口号)



最新文章

123

最新摄影

闪念基因

微信扫一扫

第七城市微信公众平台