1.Eclipse中 NEW ->OTHER->Maven->maven project
2.选择项目路径
3.选择项目类型->next->输入groupid和artifactid->finished
4.新建好项目之后pom.xml添加springmvc、Jackson、taglib、jstl依赖,保存
1 <dependencies> 2 <dependency> 3 <groupId>junit</groupId> 4 <artifactId>junit</artifactId> 5 <version>3.8.1</version> 6 <scope>test</scope> 7 </dependency> 8 <!-- https://mvnrepository.com/artifact/org.springframework/spring-web --> 9 <dependency> 10 <groupId>org.springframework</groupId> 11 <artifactId>spring-web</artifactId> 12 </dependency> 13 <dependency> 14 <groupId>org.springframework</groupId> 15 <artifactId>spring-webmvc</artifactId> 16 <version>4.2.7.RELEASE</version> 17 </dependency> 18 <dependency> 19 <groupId>jstl</groupId> 20 <artifactId>jstl</artifactId> 21 <version>1.2</version> 22 </dependency> 23 <dependency> 24 <groupId>taglibs</groupId> 25 <artifactId>standard</artifactId> 26 <version>1.1.2</version> 27 </dependency> 28 <dependency> 29 <groupId>com.fasterxml.jackson.core</groupId> 30 <artifactId>jackson-annotations</artifactId> 31 <version>2.5.0</version> 32 </dependency> 33 <dependency> 34 <groupId>com.fasterxml.jackson.core</groupId> 35 <artifactId>jackson-core</artifactId> 36 <version>2.5.0</version> 37 </dependency> 38 <dependency> 39 <groupId>com.fasterxml.jackson.core</groupId> 40 <artifactId>jackson-databind</artifactId> 41 <version>2.5.0</version> 42 </dependency> 43 <dependency> 44 <groupId>com.fasterxml.jackson.jr</groupId> 45 <artifactId>jackson-jr-all</artifactId> 46 <version>2.5.0</version> 47 </dependency> 48 </dependencies>
5.配置build path->source
6.配置build path->libraries->jre system library 改成jdk1.6+版本
7.add library ->server runtime ->apache tomcat v7.0 (配置Tomcat的runtime包,避免运行项目时报缺少httpservletXXX的问题
8.把maven项目转为dynamic web module
右击项目->properties->project facets->dynamic web module 2.5+(3.0 是jdk7+的)同时修改对应的compiler级别,选择dynamic web module,java,js
9.设置部署文件路径列表 properties->deployment assembly
10.配置web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" 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_2_5.xsd" version="2.5" > <context-param> <param-name>webAppRootKey</param-name> <param-value>maven.example.root</param-value> </context-param> <!-- Spring的log4j监听器 <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener>--> <!-- 字符集 过滤器 --> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Spring view分发器 --> <!-- 如要添加MVC驱动、注解检测、视图解析等。dispatcher-servlet.xml--> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/dispatcher-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
11.配置dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <mvc:annotation-driven /> <mvc:default-servlet-handler /> <context:component-scan base-package="com.cici.*" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> <value>text/plain;charset=UTF-8</value> <value>application/json;charset=UTF-8</value> </list> </property> </bean> </list> </property> </bean> </beans>
用maven构建的springmvc项目就搭建完成了。