与SpringSecurity的配置类似,spring同样为我们提供了一个实现类WebMvcConfigurationSupport和一个注解@EnableWebMvc以帮助我们减少bean的声明。
applicationContext-MvcConfig.xml
- <!-- 启用注解,并定义组件查找规则 ,mvc层只负责扫描@Controller -->
- <context:component-scan base-package="web.function"
- use-default-filters="false">
- <context:include-filter type="annotation"
- expression="org.springframework.stereotype.Controller" />
- </context:component-scan>
- <!-- 视图处理器 -->
- <bean id="viewResolver"
- class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/WEB-INF/views/jsp/function/" />
- <property name="suffix" value=".jsp" />
- </bean>
- <!-- 定义国际化资源文件查找规则 ,各种messages.properties -->
- <bean id="messageSource"
- class="org.springframework.context.support.ResourceBundleMessageSource"
- p:basename="config.messages.messages">
- </bean>
- <!-- servlet适配器,这里必须明确声明,因为spring默认没有初始化该适配器 -->
- <bean id="servletHandlerAdapter"
- class="org.springframework.web.servlet.handler.SimpleServletHandlerAdapter" />
- <!-- 定义文件上传处理器 -->
- <bean id="multipartResolver"
- class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
- p:defaultEncoding="UTF-8" />
- <!-- 异常处理器 -->
- <bean id="exceptionResolver" class="web.core.CP_SimpleMappingExceptionResolver">
- <property name="defaultErrorView" value="common_error" />
- <property name="exceptionAttribute" value="exception" />
- <property name="exceptionMappings">
- <props>
- <prop key="java.lang.RuntimeException">common_error</prop>
- </props>
- </property>
- </bean>
- <!-- 定义公共参数初始化拦截器 -->
- <bean id="initInterceptor" class="web.core.CP_InitializingInterceptor" />
- <!-- 本地化资源处理器 -->
- <bean id="localeResolver"
- class="org.springframework.web.servlet.i18n.CookieLocaleResolver" />
- <!-- 定义本地化变更拦截器 -->
- <bean id="localeChangeInterceptor"
- class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
- <!-- 请求拦截器,每一个用户请求都会被拦截 -->
- <mvc:interceptors>
- <ref bean="localeChangeInterceptor" />
- <ref bean="initInterceptor" />
- </mvc:interceptors>
- <!-- 定义注解驱动Controller方法处理适配器 ,注:该适配器必须声明在<mvc:annotation-driven />之前,否则不能正常处理参数类型的转换 -->
- <bean
- class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
- <property name="webBindingInitializer">
- <bean class="web.core.CP_PropertyEditorRegistrar">
- <property name="format" value="yyyy-MM-dd"></property>
- </bean>
- </property>
- <property name="messageConverters">
- <list>
- <bean
- class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter" />
- <bean
- class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
- </list>
- </property>
- </bean>
- <!-- 会自动注册RequestMappingHandlerMapping与RequestMappingHandlerAdapter
- 两个bean,是spring MVC为@Controllers分发请求所必须的。 并提供了:数据绑定支持,@NumberFormatannotation支持,@DateTimeFormat支持,@Valid支持,读写XML的支持(JAXB),读写JSON的支持(Jackson) -->
- <mvc:annotation-driven />
- <!-- 资源访问处理器 -->
- <mvc:resources mapping="/static/**" location="/WEB-INF/static/" />
MvcConfig.Java
- @Configuration
- @EnableWebMvc
- @ComponentScan(basePackages = "web.function", useDefaultFilters = false, includeFilters = {
- @ComponentScan.Filter(type = FilterType.ANNOTATION, value = {Controller.class})
- })
- public class MvcConfig extends WebMvcConfigurationSupport {
- private static final Logger logger = Logger
- .getLogger(MvcConfig.class);
- /**
- * 描述 : <注册试图处理器>. <br>
- *<p>
- <使用方法说明>
- </p>
- * @return
- */
- @Bean
- public ViewResolver viewResolver() {
- logger.info("ViewResolver");
- InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
- viewResolver.setPrefix("/WEB-INF/views/jsp/function/");
- viewResolver.setSuffix(".jsp");
- return viewResolver;
- }
- /**
- * 描述 : <注册消息资源处理器>. <br>
- *<p>
- <使用方法说明>
- </p>
- * @return
- */
- @Bean
- public MessageSource messageSource() {
- logger.info("MessageSource");
- ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
- messageSource.setBasename("config.messages.messages");
- return messageSource;
- }
- /**
- * 描述 : <注册servlet适配器>. <br>
- *<p>
- <只需要在自定义的servlet上用@Controller("映射路径")标注即可>
- </p>
- * @return
- */
- @Bean
- public HandlerAdapter servletHandlerAdapter(){
- logger.info("HandlerAdapter");
- return new SimpleServletHandlerAdapter();
- }
- /**
- * 描述 : <本地化拦截器>. <br>
- *<p>
- <使用方法说明>
- </p>
- * @return
- */
- @Bean
- public LocaleChangeInterceptor localeChangeInterceptor(){
- logger.info("LocaleChangeInterceptor");
- return new LocaleChangeInterceptor();
- }
- /**
- * 描述 : <基于cookie的本地化资源处理器>. <br>
- *<p>
- <使用方法说明>
- </p>
- * @return
- */
- @Bean(name="localeResolver")
- public CookieLocaleResolver cookieLocaleResolver(){
- logger.info("CookieLocaleResolver");
- return new CookieLocaleResolver();
- }
- /**
- * 描述 : <注册自定义拦截器>. <br>
- *<p>
- <使用方法说明>
- </p>
- * @return
- */
- @Bean
- public CP_InitializingInterceptor initializingInterceptor(){
- logger.info("CP_InitializingInterceptor");
- return new CP_InitializingInterceptor();
- }
- /**
- * 描述 : <RequestMappingHandlerMapping需要显示声明,否则不能注册自定义的拦截器>. <br>
- *<p>
- <这个比较奇怪,理论上应该是不需要的>
- </p>
- * @return
- */
- @Bean
- public RequestMappingHandlerMapping requestMappingHandlerMapping() {
- logger.info("RequestMappingHandlerMapping");
- return super.requestMappingHandlerMapping();
- }
- /**
- * 描述 : <添加拦截器>. <br>
- *<p>
- <使用方法说明>
- </p>
- * @param registry
- */
- @Override
- protected void addInterceptors(InterceptorRegistry registry) {
- // TODO Auto-generated method stub
- logger.info("addInterceptors start");
- registry.addInterceptor(localeChangeInterceptor());
- registry.addInterceptor(initializingInterceptor());
- logger.info("addInterceptors end");
- }
- /**
- * 描述 : <HandlerMapping需要显示声明,否则不能注册资源访问处理器>. <br>
- *<p>
- <这个比较奇怪,理论上应该是不需要的>
- </p>
- * @return
- */
- @Bean
- public HandlerMapping resourceHandlerMapping() {
- logger.info("HandlerMapping");
- return super.resourceHandlerMapping();
- }
- /**
- * 描述 : <资源访问处理器>. <br>
- *<p>
- <可以在jsp中使用/static/**的方式访问/WEB-INF/static/下的内容>
- </p>
- * @param registry
- */
- @Override
- protected void addResourceHandlers(ResourceHandlerRegistry registry) {
- logger.info("addResourceHandlers");
- registry.addResourceHandler("/static/**").addResourceLocations("/WEB-INF/static/");
- }
- /**
- * 描述 : <文件上传处理器>. <br>
- *<p>
- <使用方法说明>
- </p>
- * @return
- */
- @Bean(name="multipartResolver")
- public CommonsMultipartResolver commonsMultipartResolver(){
- logger.info("CommonsMultipartResolver");
- return new CommonsMultipartResolver();
- }
- /**
- * 描述 : <异常处理器>. <br>
- *<p>
- <系统运行时遇到指定的异常将会跳转到指定的页面>
- </p>
- * @return
- */
- @Bean(name="exceptionResolver")
- public CP_SimpleMappingExceptionResolver simpleMappingExceptionResolver(){
- logger.info("CP_SimpleMappingExceptionResolver");
- CP_SimpleMappingExceptionResolver simpleMappingExceptionResolver= new CP_SimpleMappingExceptionResolver();
- simpleMappingExceptionResolver.setDefaultErrorView("common_error");
- simpleMappingExceptionResolver.setExceptionAttribute("exception");
- Properties properties = new Properties();
- properties.setProperty("java.lang.RuntimeException", "common_error");
- simpleMappingExceptionResolver.setExceptionMappings(properties);
- return simpleMappingExceptionResolver;
- }
- /**
- * 描述 : <RequestMappingHandlerAdapter需要显示声明,否则不能注册通用属性编辑器>. <br>
- *<p>
- <这个比较奇怪,理论上应该是不需要的>
- </p>
- * @return
- */
- @Bean
- public RequestMappingHandlerAdapter requestMappingHandlerAdapter() {
- logger.info("RequestMappingHandlerAdapter");
- return super.requestMappingHandlerAdapter();
- }
- /**
- * 描述 : <注册通用属性编辑器>. <br>
- *<p>
- <这里只增加了字符串转日期和字符串两边去空格的处理>
- </p>
- * @return
- */
- @Override
- protected ConfigurableWebBindingInitializer getConfigurableWebBindingInitializer() {
- logger.info("ConfigurableWebBindingInitializer");
- ConfigurableWebBindingInitializer initializer = super.getConfigurableWebBindingInitializer();
- CP_PropertyEditorRegistrar register = new CP_PropertyEditorRegistrar();
- register.setFormat("yyyy-MM-dd");
- initializer.setPropertyEditorRegistrar(register);
- return initializer;
- }
- }