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

與SpringSecurity的配置相似,spring一樣爲咱們提供了一個實現類WebMvcConfigurationSupport和一個註解@EnableWebMvc以幫助咱們減小bean的聲明。java

 

applicationContext-MvcConfig.xmlweb

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.javaspring

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】

相關文章
相關標籤/搜索