<?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" 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"> <mvc:resources location="/css/" mapping="/css/**"></mvc:resources> <mvc:resources location="/js/" mapping="/js/**"></mvc:resources> <mvc:resources location="/img/" mapping="/img/**"></mvc:resources> <mvc:resources location="/image/" mapping="/image/**"></mvc:resources> <mvc:resources location="/libs/" mapping="/libs/**"></mvc:resources> <!-- 自动扫描@Controller注入为bean --> <context:component-scan base-package="com.deppon.cubc"/> <!-- 解决静态资源被拦截的问题 --> <mvc:default-servlet-handler/> <mvc:interceptors> <mvc:interceptor> <!-- 需要拦截的路径 --> <mvc:mapping path="/**"/> <!-- 不需要拦截的路径 --> <mvc:exclude-mapping path="/libs/**"/> <mvc:exclude-mapping path="/image/**"/> <mvc:exclude-mapping path="/img/**"/> <mvc:exclude-mapping path="/js/**"/> <mvc:exclude-mapping path="/css/**"/> <mvc:exclude-mapping path="/font/**"/> <mvc:exclude-mapping path="/WEB-INF/view/**"/> <ref bean="sessionInterceptor"/> </mvc:interceptor> </mvc:interceptors> <!-- 自定义拦截器类 --> <bean id="sessionInterceptor" class="com.deppon.cubc.web.common.interceptor.SessionInterceptor"></bean> <!-- exception resolver 返回异常后的处理 --> <bean id="handlerExceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver" > <property name="exceptionMappings"> <props> <prop key="com.deppon.cubc.web.authorization.SessionTimeoutException">redirect:/login</prop> </props> </property> </bean> <!-- 以下为SpringMVC配置 --> <!-- <mvc:annotation-driven /> 是一种简写形式,完全可以手动配置替代这种简写形式,简写形式可以让初学都快速应用默认配置方案。 <mvc:annotation-driven /> 会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,是spring MVC为@Controllers分发请求所必须的。 并提供了:数据绑定支持,@NumberFormatannotation支持,@DateTimeFormat支持,@Valid支持,读写XML的支持(JAXB),读写JSON的支持(Jackson)。 后面,我们处理响应ajax请求时,就使用到了对json的支持。 后面,对action写JUnit单元测试时,要从spring IOC容器中取DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean, 来完成测试,取的时候要知道是<mvc:annotation-driven />这一句注册的这两个bean。 --> <mvc:annotation-driven> <!-- 返回json数据,@response使用 --> <mvc:message-converters register-defaults="true"> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> <value>application/json;charset=UTF-8</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> <!-- 配置视图解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 视图前缀 --> <property name="prefix"> <value>/WEB-INF/view/</value> </property> <!-- 视图后缀 --> <property name="suffix"> <value>.html</value> </property> <property name="order" value="256"/> </bean> </beans>
web.xml中的配置:<!-- 编码过滤器 --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <async-supported>true</async-supported> <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> <!-- Spring监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 防止Spring内存溢出监听器 --> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <!-- Spring MVC servlet --> <servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <!-- 此处可以可以配置成*.do,对应struts的后缀习惯 --> <url-pattern>/</url-pattern> </servlet-mapping>
springmvc文件配置
最新推荐文章于 2017-04-17 10:35:40 发布