Version: Next

整合SpringMVC

  • 配置项目WEB支持
    • Project Structure -> Artifact -> lib
    • Tomcat部署

web.xml

  • dispatcherServlet
    • load-on-startup
    • init-param配置springMVC配置文件
  • 乱码过滤——CharacterEncodingFilter
    • 配置encodingutf-8
<!-- 乱码过滤-->
<filter>
<filter-name>characterEncoding</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>
</filter>
<filter-mapping>
<filter-name>characterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- dispatcherServlet-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

spring-mvc.xml

  • 注解驱动
  • 默认Handler静态资源过滤
  • 开启Controller层包扫描
  • 视图解析器InternalResourceViewResolver
<!-- 注解驱动-->
<mvc:annotation-driven/>
<!-- handler-->
<mvc:default-servlet-handler/>
<!--Controller层包扫描-->
<context:component-scan base-package="com.bsx.controller"/>
<!-- 视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="viewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>

将配置文件整合

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="spring-mvc.xml"/>
<import resource="spring-dao.xml"/>
<import resource="spring-service.xml"/>
</beans>