在之前搭建maven项目这篇的基础上继续集成,引入spring mvc支持
一、添加jar包引用
修改pom.xml文件,加入:(其他关联的jar包maven会自动引用)
<!-- 项目属性 -->
<properties>
<spring.version>3.2.9.RELEASE</spring.version>
</properties> <!-- 依赖项定义 -->
<dependencies> <!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
如图:
二、添加spring mvc支持
1、修改web.xml文件,加入spring mvc核心代码:
<!-- spring核心 -->
<servlet>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- spring mvc配置文件(classpath*:不仅包含class路径,还包括jar文件中(class路径)) -->
<param-value>classpath*:/spring-mvc.xml</param-value>
</init-param>
<!-- 在容器启动时以最高的优先级加载当前servlet -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<!-- 拦截所有url(也可以写*.do,只拦截.do结尾的url) -->
<url-pattern>/</url-pattern>
</servlet-mapping>
2、加入其他配置文件路径(以后扩展功能的时候需要)和监听器
<!-- 配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- 配置文件命名统一用spring-context开头 -->
<param-value>classpath*:/spring-context*.xml</param-value>
</context-param> <!-- spring监听器(在容器启动时自动装配配置文件) -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
3、加入字符集过滤器,防止jsp和java交互时的乱码问题(一定要加的)
<!-- 字符集过滤器 -->
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
web.xml最后如图:
三、添加spring mvc配置文件
在"src/main/resources"代码文件夹中新建文件"spring-mvc.xml",内容为:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="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.2.xsd"> <description>Spring MVC Configuration</description> <!-- 使用Annotation自动注册Bean -->
<context:component-scan base-package="org.xs.demo1" use-default-filters="false">
<!-- 子上下文中只注册Controller等与web层相关的bean -->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan> <!-- 注册两种注解映射 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" />
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" /> <!-- 定义视图文件解析 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:order="3">
<property name="contentType" value="text/html" />
<!-- jsp页面的位置 -->
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
四、验证测试
1、增加Controller类
在"src/main/java"代码文件夹的"org.xs.demo1"的包下新建(这个包名和spring-mvc.xml中的"Annotation"的扫描bean地址相对应
2、增加jsp页面
在WEB-INF的views文件夹中新建(views的地址和spring-mvc.xml中的视图文件路径prefix相对应)
3、新建server并运行测试