spring整合springmvc
阿新 • • 發佈:2020-07-25
spring整合springMVC主要需要配置兩個xml配置檔案,web.xml和springmvc.xml
步驟:
一、配置springmvc.xml
主要配置:
1.配置自動掃描的包:掃描controller層
2.配置springmvc的註解驅動,不開啟此驅動無法使用RequestBody,ResponseBody等註解
3.配置檢視解析器
4.配置過濾靜態資源等:目的是讓靜態資源不被攔截
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--配置自動掃描的包--> <context:component-scan base-package="com.xj.controller"/> <!--配置springmvc的註解驅動--> <mvc:annotation-driven/> <!--配置檢視解析器--> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/"/> <property name="suffix" value=".jsp"/> </bean> <!--過濾靜態資源--> <mvc:resources mapping="/css/**" location="/css/"/> <mvc:resources mapping="/images/**" location="/images/"/> <mvc:resources mapping="/js/**" location="/js/"/> </beans>
二、配置web.xml
主要配置:
1.配置監聽器Listener
作用:用於載入spring配置檔案(dao和service層的ioc容器)
2.配置過濾器Filter
characterEncodingFilter:用來指定請求或者響應的編碼格式
其中encoding用來指定編碼格式,forceEncoding表示是否強制request/response的編碼格式也是encoding格式
3.配置前端控制器
作用:
1.載入springmvc配置檔案(controller層的ioc容器)
2.攔截請求
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" 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>contextConfigLocation</param-name> <param-value>classpath:ApplicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</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>forceRequestEncoding</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>forceResponseEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <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:springmvc.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> </web-app>