码迷,mamicode.com
首页 > 编程语言 > 详细

SpringMVC4零配置--Web上下文配置【MvcConfig】

时间:2016-08-12 15:07:23      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:

与SpringSecurity的配置类似,spring同样为我们提供了一个实现类WebMvcConfigurationSupport和一个注解@EnableWebMvc以帮助我们减少bean的声明。

 

applicationContext-MvcConfig.xml

Xml代码  技术分享
  1. <!-- 启用注解,并定义组件查找规则 ,mvc层只负责扫描@Controller -->  
  2.     <context:component-scan base-package="web.function"  
  3.         use-default-filters="false">  
  4.         <context:include-filter type="annotation"  
  5.             expression="org.springframework.stereotype.Controller" />  
  6.     </context:component-scan>  
  7.   
  8.   
  9.   
  10.   
  11.     <!-- 视图处理器 -->  
  12.     <bean id="viewResolver"  
  13.         class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  14.         <property name="prefix" value="/WEB-INF/views/jsp/function/" />  
  15.         <property name="suffix" value=".jsp" />  
  16.     </bean>  
  17.   
  18.     <!-- 定义国际化资源文件查找规则 ,各种messages.properties -->  
  19.     <bean id="messageSource"  
  20.         class="org.springframework.context.support.ResourceBundleMessageSource"  
  21.         p:basename="config.messages.messages">  
  22.     </bean>  
  23.   
  24.     <!-- servlet适配器,这里必须明确声明,因为spring默认没有初始化该适配器 -->  
  25.     <bean id="servletHandlerAdapter"  
  26.         class="org.springframework.web.servlet.handler.SimpleServletHandlerAdapter" />  
  27.   
  28.     <!-- 定义文件上传处理器 -->  
  29.     <bean id="multipartResolver"  
  30.         class="org.springframework.web.multipart.commons.CommonsMultipartResolver"  
  31.         p:defaultEncoding="UTF-8" />  
  32.   
  33.     <!-- 异常处理器 -->  
  34.     <bean id="exceptionResolver" class="web.core.CP_SimpleMappingExceptionResolver">  
  35.         <property name="defaultErrorView" value="common_error" />  
  36.         <property name="exceptionAttribute" value="exception" />  
  37.         <property name="exceptionMappings">  
  38.             <props>  
  39.                 <prop key="java.lang.RuntimeException">common_error</prop>  
  40.             </props>  
  41.         </property>  
  42.     </bean>  
  43.   
  44.   
  45.     <!-- 定义公共参数初始化拦截器 -->  
  46.     <bean id="initInterceptor" class="web.core.CP_InitializingInterceptor" />  
  47.   
  48.   
  49.   
  50.   
  51.     <!-- 本地化资源处理器 -->  
  52.     <bean id="localeResolver"  
  53.         class="org.springframework.web.servlet.i18n.CookieLocaleResolver" />  
  54.   
  55.     <!-- 定义本地化变更拦截器 -->  
  56.     <bean id="localeChangeInterceptor"  
  57.         class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />  
  58.   
  59.   
  60.     <!-- 请求拦截器,每一个用户请求都会被拦截 -->  
  61.     <mvc:interceptors>  
  62.         <ref bean="localeChangeInterceptor" />  
  63.         <ref bean="initInterceptor" />  
  64.     </mvc:interceptors>  
  65.   
  66.   
  67.   
  68.   
  69.     <!-- 定义注解驱动Controller方法处理适配器 ,注:该适配器必须声明在<mvc:annotation-driven />之前,否则不能正常处理参数类型的转换 -->  
  70.     <bean  
  71.         class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">  
  72.         <property name="webBindingInitializer">  
  73.             <bean class="web.core.CP_PropertyEditorRegistrar">  
  74.                 <property name="format" value="yyyy-MM-dd"></property>  
  75.             </bean>  
  76.         </property>  
  77.         <property name="messageConverters">  
  78.             <list>  
  79.                 <bean  
  80.                     class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter" />  
  81.                 <bean  
  82.                     class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />  
  83.             </list>  
  84.         </property>  
  85.     </bean>  
  86.   
  87.   
  88.     <!-- 会自动注册RequestMappingHandlerMapping与RequestMappingHandlerAdapter   
  89.         两个bean,是spring MVC为@Controllers分发请求所必须的。 并提供了:数据绑定支持,@NumberFormatannotation支持,@DateTimeFormat支持,@Valid支持,读写XML的支持(JAXB),读写JSON的支持(Jackson) -->  
  90.     <mvc:annotation-driven />  
  91.   
  92.   
  93.     <!-- 资源访问处理器 -->  
  94.     <mvc:resources mapping="/static/**" location="/WEB-INF/static/" />  

 MvcConfig.java

Java代码  技术分享
  1. @Configuration  
  2. @EnableWebMvc  
  3. @ComponentScan(basePackages = "web.function", useDefaultFilters = false, includeFilters = {  
  4.         @ComponentScan.Filter(type = FilterType.ANNOTATION, value = {Controller.class})  
  5. })  
  6. public class MvcConfig extends WebMvcConfigurationSupport {  
  7.       
  8.     private static final Logger logger = Logger  
  9.             .getLogger(MvcConfig.class);  
  10.   
  11.     /**                                                           
  12.     * 描述 : <注册试图处理器>. <br>  
  13.     *<p>  
  14.         <使用方法说明>   
  15.      </p>                                                                                                                                                                                                                                                 
  16.     * @return                                                                                                       
  17.     */    
  18.     @Bean  
  19.     public ViewResolver viewResolver() {  
  20.         logger.info("ViewResolver");  
  21.         InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();  
  22.         viewResolver.setPrefix("/WEB-INF/views/jsp/function/");  
  23.         viewResolver.setSuffix(".jsp");  
  24.         return viewResolver;  
  25.     }  
  26.       
  27.     /**                                                           
  28.     * 描述 : <注册消息资源处理器>. <br>  
  29.     *<p>  
  30.         <使用方法说明>   
  31.      </p>                                                                                                                                                                                                                                                 
  32.     * @return                                                                                                       
  33.     */    
  34.     @Bean  
  35.     public MessageSource messageSource() {  
  36.         logger.info("MessageSource");  
  37.         ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();  
  38.         messageSource.setBasename("config.messages.messages");  
  39.           
  40.         return messageSource;  
  41.     }  
  42.       
  43.     /**                                                           
  44.     * 描述 : <注册servlet适配器>. <br>  
  45.     *<p>  
  46.         <只需要在自定义的servlet上用@Controller("映射路径")标注即可>   
  47.      </p>                                                                                                                                                                                                                                                 
  48.     * @return                                                                                                       
  49.     */    
  50.     @Bean  
  51.     public HandlerAdapter servletHandlerAdapter(){  
  52.         logger.info("HandlerAdapter");  
  53.         return new SimpleServletHandlerAdapter();  
  54.     }  
  55.       
  56.     /**                                                           
  57.     * 描述 : <本地化拦截器>. <br>  
  58.     *<p>  
  59.         <使用方法说明>   
  60.      </p>                                                                                                                                                                                                                                                 
  61.     * @return                                                                                                       
  62.     */    
  63.     @Bean  
  64.     public LocaleChangeInterceptor localeChangeInterceptor(){  
  65.         logger.info("LocaleChangeInterceptor");  
  66.         return new LocaleChangeInterceptor();  
  67.     }  
  68.       
  69.     /**                                                           
  70.     * 描述 : <基于cookie的本地化资源处理器>. <br>  
  71.     *<p>  
  72.         <使用方法说明>   
  73.      </p>                                                                                                                                                                                                                                                 
  74.     * @return                                                                                                       
  75.     */    
  76.     @Bean(name="localeResolver")  
  77.     public CookieLocaleResolver cookieLocaleResolver(){  
  78.         logger.info("CookieLocaleResolver");  
  79.         return new CookieLocaleResolver();  
  80.     }  
  81.       
  82.     /**                                                           
  83.     * 描述 : <注册自定义拦截器>. <br>  
  84.     *<p>  
  85.         <使用方法说明>   
  86.      </p>                                                                                                                                                                                                                                                 
  87.     * @return                                                                                                       
  88.     */    
  89.     @Bean  
  90.     public CP_InitializingInterceptor initializingInterceptor(){  
  91.         logger.info("CP_InitializingInterceptor");  
  92.         return new CP_InitializingInterceptor();  
  93.     }  
  94.       
  95.       
  96.     /**                                                           
  97.      * 描述 : <RequestMappingHandlerMapping需要显示声明,否则不能注册自定义的拦截器>. <br>  
  98.      *<p>  
  99.         <这个比较奇怪,理论上应该是不需要的>   
  100.       </p>                                                                                                                                                                                                                                                 
  101.      * @return                                                                                                       
  102.      */   
  103.     @Bean  
  104.     public RequestMappingHandlerMapping requestMappingHandlerMapping() {  
  105.         logger.info("RequestMappingHandlerMapping");  
  106.           
  107.         return super.requestMappingHandlerMapping();  
  108.     }  
  109.       
  110.       
  111.       
  112.       
  113.       
  114.   
  115.     /**                                                           
  116.     * 描述 : <添加拦截器>. <br>  
  117.     *<p>  
  118.         <使用方法说明>   
  119.      </p>                                                                                                                                                                                                                                                 
  120.     * @param registry                                                                                                       
  121.     */    
  122.     @Override  
  123.     protected void addInterceptors(InterceptorRegistry registry) {  
  124.         // TODO Auto-generated method stub  
  125.         logger.info("addInterceptors start");  
  126.         registry.addInterceptor(localeChangeInterceptor());  
  127.         registry.addInterceptor(initializingInterceptor());  
  128.         logger.info("addInterceptors end");  
  129.     }  
  130.   
  131.     /**                                                           
  132.      * 描述 : <HandlerMapping需要显示声明,否则不能注册资源访问处理器>. <br>  
  133.      *<p>  
  134.         <这个比较奇怪,理论上应该是不需要的>   
  135.       </p>                                                                                                                                                                                                                                                 
  136.      * @return                                                                                                       
  137.      */   
  138.     @Bean  
  139.     public HandlerMapping resourceHandlerMapping() {  
  140.         logger.info("HandlerMapping");  
  141.         return super.resourceHandlerMapping();  
  142.     }  
  143.       
  144.     /**                                                           
  145.      * 描述 : <资源访问处理器>. <br>  
  146.      *<p>  
  147.         <可以在jsp中使用/static/**的方式访问/WEB-INF/static/下的内容>   
  148.       </p>                                                                                                                                                                                                                                                 
  149.      * @param registry                                                                                                       
  150.      */    
  151.     @Override  
  152.     protected void addResourceHandlers(ResourceHandlerRegistry registry) {  
  153.         logger.info("addResourceHandlers");  
  154.         registry.addResourceHandler("/static/**").addResourceLocations("/WEB-INF/static/");  
  155.     }  
  156.       
  157.     /**                                                            
  158.     * 描述 : <文件上传处理器>. <br>   
  159.     *<p>   
  160.         <使用方法说明>    
  161.      </p>                                                                                                                                                                                                                                                  
  162.     * @return                                                                                                        
  163.     */    
  164.     @Bean(name="multipartResolver")  
  165.     public CommonsMultipartResolver commonsMultipartResolver(){  
  166.         logger.info("CommonsMultipartResolver");  
  167.         return new CommonsMultipartResolver();  
  168.     }  
  169.       
  170.     /**                                                           
  171.     * 描述 : <异常处理器>. <br>  
  172.     *<p>  
  173.         <系统运行时遇到指定的异常将会跳转到指定的页面>   
  174.      </p>                                                                                                                                                                                                                                                 
  175.     * @return                                                                                                       
  176.     */    
  177.     @Bean(name="exceptionResolver")  
  178.     public CP_SimpleMappingExceptionResolver simpleMappingExceptionResolver(){  
  179.         logger.info("CP_SimpleMappingExceptionResolver");  
  180.         CP_SimpleMappingExceptionResolver simpleMappingExceptionResolver= new CP_SimpleMappingExceptionResolver();  
  181.         simpleMappingExceptionResolver.setDefaultErrorView("common_error");  
  182.         simpleMappingExceptionResolver.setExceptionAttribute("exception");  
  183.         Properties properties = new Properties();  
  184.         properties.setProperty("java.lang.RuntimeException", "common_error");  
  185.         simpleMappingExceptionResolver.setExceptionMappings(properties);  
  186.         return simpleMappingExceptionResolver;  
  187.     }  
  188.       
  189.      /**                                                           
  190.      * 描述 : <RequestMappingHandlerAdapter需要显示声明,否则不能注册通用属性编辑器>. <br>  
  191.      *<p>  
  192.         <这个比较奇怪,理论上应该是不需要的>   
  193.       </p>                                                                                                                                                                                                                                                 
  194.      * @return                                                                                                       
  195.      */   
  196.     @Bean  
  197.     public RequestMappingHandlerAdapter requestMappingHandlerAdapter() {  
  198.         logger.info("RequestMappingHandlerAdapter");  
  199.         return super.requestMappingHandlerAdapter();  
  200.     }  
  201.       
  202.     /**                                                           
  203.     * 描述 : <注册通用属性编辑器>. <br>  
  204.     *<p>  
  205.         <这里只增加了字符串转日期和字符串两边去空格的处理>   
  206.      </p>                                                                                                                                                                                                                                                 
  207.     * @return                                                                                                       
  208.     */    
  209.     @Override  
  210.     protected ConfigurableWebBindingInitializer getConfigurableWebBindingInitializer() {  
  211.         logger.info("ConfigurableWebBindingInitializer");  
  212.         ConfigurableWebBindingInitializer initializer = super.getConfigurableWebBindingInitializer();  
  213.         CP_PropertyEditorRegistrar register = new CP_PropertyEditorRegistrar();  
  214.         register.setFormat("yyyy-MM-dd");  
  215.         initializer.setPropertyEditorRegistrar(register);  
  216.         return initializer;  
  217.     }  
  218.       
  219.       
  220. }  

 

SpringMVC4零配置 :代码下载

SpringMVC4零配置--web.xml

SpringMVC4零配置--应用上下文配置【AppConfig】

SpringMVC4零配置--SpringSecurity相关配置【SpringSecurityConfig】

SpringMVC4零配置--Web上下文配置【MvcConfig】

SpringMVC4零配置--Web上下文配置【MvcConfig】

标签:

原文地址:http://www.cnblogs.com/yhtboke/p/5764658.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!