這一篇文章講的是Spring Web MVC各部分的配置方法,包括Java代碼配置和XML文件配置以及MVC命名空間的使用方法。
啟用MVC Java配置和XML命名空間
默認配置
要啟用MVC Java配置(@Controller等各種注解)和XML命名空間,如果使用的是Java配置,在配置類上再添加@EnableWebMvc注解即可。
1
2
3
4
5
|
@Configuration @EnableWebMvc public class WebAppConfig { } |
如果使用XML配置文件的話,添加下面一行即可。
1
|
< mvc:annotation-driven /> |
不論使用哪種方式,都會在Spring中注冊一些組件來提供最基本的MVC功能。這些功能在文檔中說的很清楚。我簡單翻譯了一下:
上面的配置會注冊一個RequestMappingHandlerMapping,一個RequestMappingHandlerAdapter和一個ExceptionHandlerExceptionResolver來提供注解控制器和注解方法(比如@RequestMapping和@ExceptionHandler等)處理請求的功能。
還會啟用以下功能:
- 通過一個ConversionService實例,來進行Spring 3 方式的類型轉換及數據綁定支持。
- @NumberFormat格式化數字字段的支持
- @DateTimeFormat格式化Date、Calendar、Long、JodaTime類型字段的支持。
- 在控制器方法上使用@Valid驗證Bean的支持,如果檢測到JSR-303 Bean驗證的實現。
- 一組HttpMessageConverter,用于在字符串和所需Java類型之間進行類型轉換,具體的列表參見Spring文檔 22.16.1. Enabling the MVC Java Config or the MVC XML Namespace。
通過這些默認配置,我們即可開始最基本的Spring MVC使用。
自定義配置
上面提供了最基本的配置。如果需要自定義某些配置也可以。如果使用Java配置的話,讓配置類實現WebMvcConfigurer接口,更常用的辦法是繼承WebMvcConfigurerAdapter基類,通過重寫基類中的方法即可配置相關功能。
1
2
3
4
5
6
7
|
@Configuration @EnableWebMvc public class WebConfig extends WebMvcConfigurerAdapter { //有很多個方法可以重寫,來提供自定義功能 } |
如果使用XML配置文件,通過IDE的自動補全功能查看一下<mvc:annotation-driven/>有哪些子屬性和子元素。
類型轉換和格式化
默認情況下Spring注冊了Number(包括所有基本數字類型)和java.util.Date的類型轉換和格式化功能。要提供類型的轉換和格式化功能,就需要自己注冊相應的類型轉換器和格式化器。
如果使用Java配置的話,重寫addFormatters(FormatterRegistry registry)方法并添加相應功能即可。
1
2
3
4
5
6
7
8
9
10
|
@Configuration @EnableWebMvc public class WebConfig extends WebMvcConfigurerAdapter { @Override public void addFormatters(FormatterRegistry registry) { // Add formatters and/or converters } } |
如果使用XML配置的話,需要注冊一個ConversionService,然后添加到<mvc:annotation-driven>節點中。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
< mvc:annotation-driven conversion-service = "conversionService" /> < bean id = "conversionService" class = "org.springframework.format.support.FormattingConversionServiceFactoryBean" > < property name = "converters" > < set > < bean class = "org.example.MyConverter" /> </ set > </ property > < property name = "formatters" > < set > < bean class = "org.example.MyFormatter" /> < bean class = "org.example.MyAnnotationFormatterFactory" /> </ set > </ property > < property name = "formatterRegistrars" > < set > < bean class = "org.example.MyFormatterRegistrar" /> </ set > </ property > </ bean > |
驗證功能
Spring自己提供了一組接口和類提供了一套驗證功能。不過更通用的方法是使用Bean Validation進行Java對象的驗證,Bean Validation的一個實現就是Hibernate Validator。
默認情況下當@EnableWebMvc或<mvc:annotation-driven/>配置之后,如果Spring檢測到Bean Validation,就會自動注冊一個LocalValidatorFactoryBean來提供驗證功能。如果我們希望手動處理驗證過程,可能希望將驗證器實例注入到控制器中,這時候就不能使用自動注冊的LocalValidatorFactoryBean了。這時候我們可以選擇手動注冊一個LocalValidatorFactoryBeanBean實例,然后注解@Primary讓自定義LocalValidatorFactoryBean被優先使用。
還有一種辦法就是直接覆蓋Spring的默認驗證器配置。如果使用Java配置的話,重寫getValidator()方法即可。
1
2
3
4
5
6
7
8
9
10
|
@Configuration @EnableWebMvc public class WebConfig extends WebMvcConfigurerAdapter { @Override public Validator getValidator() { // return "global" validator } } |
如果使用XML配置文件,定義一個Validator然后添加到<mvc:annotation-driven>中。
1
|
< mvc:annotation-driven validator = "globalValidator" /> |
上面定義的都是全局驗證器,我們也可以在某個控制器中定義一個局部驗證器,然后和全局驗證器結合起來使用。這時候需要使用@InitBinder注解方法。
1
2
3
4
5
6
7
8
9
|
@Controller public class MyController { @InitBinder protected void initBinder(WebDataBinder binder) { binder.addValidators( new FooValidator()); } } |
配置好驗證器之后。當Spring識別到@Valid注解的方法參數之后,就會執行驗證,將驗證結果綁定到BindingResult上,我們可以在方法中訪問BindingResult來獲取驗證結果。
攔截器
我們實現了攔截器之后,就可以將其應用到Web程序中。使用Java配置的話,重寫addInterceptors(InterceptorRegistry registry)方法,然后在其中添加自己的攔截器即可。如果要配置攔截路徑和排除路徑也可以在這里配置。
1
2
3
4
5
6
7
8
9
10
11
12
|
@Configuration @EnableWebMvc public class WebConfig extends WebMvcConfigurerAdapter { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor( new LocaleInterceptor()); registry.addInterceptor( new ThemeInterceptor()).addPathPatterns( "/**" ).excludePathPatterns( "/admin/**" ); registry.addInterceptor( new SecurityInterceptor()).addPathPatterns( "/secure/*" ); } } |
使用XML配置文件的話可以使用MVC命名空間,配置也比較簡單。
1
2
3
4
5
6
7
8
9
10
11
12
|
< mvc:interceptors > < bean class = "org.springframework.web.servlet.i18n.LocaleChangeInterceptor" /> < mvc:interceptor > < mvc:mapping path = "/**" /> < mvc:exclude-mapping path = "/admin/**" /> < bean class = "org.springframework.web.servlet.theme.ThemeChangeInterceptor" /> </ mvc:interceptor > < mvc:interceptor > < mvc:mapping path = "/secure/*" /> < bean class = "org.example.SecurityInterceptor" /> </ mvc:interceptor > </ mvc:interceptors > |
視圖控制器
這是一種定義ParameterizableViewController的簡單方式,當該控制器被請求的時候不會執行任何邏輯操作,直接轉到相應視圖。視圖控制器的常見用法是將網站的首頁直接和/請求映射。
使用Java配置可以這樣寫,下面的配置將/映射到名為index的視圖。
1
2
3
4
5
6
7
8
9
10
|
@Configuration @EnableWebMvc public class WebConfig extends WebMvcConfigurerAdapter { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController( "/" ).setViewName( "index" ); } } |
使用XML配置也很簡單。
1
|
< mvc:view-controller path = "/" view-name = "index" /> |
視圖解析器
使用Java配置,只需要重寫configureViewResolvers(ViewResolverRegistry registry)方法即可。下面配置了JSP視圖。如果需要其它視圖解析器可以參見其相應文檔,以及ViewResolverRegistry的JavaDoc。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
@Configuration @EnableWebMvc public class WebConfig extends WebMvcConfigurerAdapter { @Override public void configureViewResolvers(ViewResolverRegistry registry) { registry.enableContentNegotiation( new MappingJackson2JsonView()); registry.jsp() .prefix( "/WEB-INF/jsp/" ) .suffix( ".jsp" ) .viewClass(JstlView. class ); } } |
如果使用XML配置文件可以使用MVC命名空間簡化配置。除了內置的JSP解析器外,其它視圖解析器可能還需要額外的配置,這里不再細述。
1
2
3
4
5
|
< mvc:view-resolvers > < mvc:jsp prefix = "/WEB-INF/jsp/" suffix = ".jsp" view-class = "org.springframework.web.servlet.view.JstlView" /> </ mvc:view-resolvers > |
資源處理
靜態資源處理
這里說的主要是靜態資源的處理。前面說了很多關于控制器、視圖的知識,但是如何映射CSS、JS文件,前面沒有說明。配置方法在這里說明。
使用Java配置的話,重寫addResourceHandlers(ResourceHandlerRegistry registry)方法,然后添加相應的映射即可。
1
2
3
4
5
6
7
8
9
10
|
@Configuration @EnableWebMvc public class WebConfig extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler( "/static/**" ).addResourceLocations( "/resources/static/" ); } } |
使用XML配置文件 也同樣簡單。
1
|
< mvc:resources mapping = "/static/**" location = "/resources/static/" /> |
這樣映射之后,假如我們有/resources/static/bootstrap.css文件,那么就可以使用/static/bootstrap.css路徑來訪問該文件了。同樣的在視圖文件中也可以如此引用。還可以使用cache-period設置資源的過期時間,單位是秒。如果需要指定多個資源位置,可以使用逗號分隔。
資源的版本控制
有些頻繁更新的資源可能需要版本控制,強制讓客戶端使用最新的資源。Spring框架也支持資源的版本控制,我們需要定義資源鏈來實現這個功能。資源鏈由一個ResourceResolver實例和多個ResourceTransformer實例組成。內建的VersionResourceResolver能滿足我們的大部分需求,它可以定義一些策略來配置版本控制,例如FixedVersionStrategy會依據日期、版本號或者其他東西作為版本;ContentVersionStrategy會計算資源的MD5值。
ContentVersionStrategy策略是一個不錯的策略,不過由于它會計算MD5,所以開銷比較大, 因此在使用這種策略的時候最好打開緩存來提高性能。
如果使用Java配置的話,和前面的例子差不多,只不過需要多調用resourceChain(true)等方法并添加相應的版本資源解析器和版本策略。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@Configuration @EnableWebMvc public class WebConfig extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler( "/static/**" ) .addResourceLocations( "/resources/static/" ) .resourceChain( true ).addResolver( new VersionResourceResolver().addContentVersionStrategy( "/**" )); } } |
下面是使用XML配置的例子。
1
2
3
4
5
6
7
8
9
10
|
< mvc:resources mapping = "/static/**" location = "/resources/static/" > < mvc:resource-chain > < mvc:resource-cache /> < mvc:resolvers > < mvc:version-resolver > < mvc:content-version-strategy patterns = "/**" /> </ mvc:version-resolver > </ mvc:resolvers > </ mvc:resource-chain > </ mvc:resources > |
默認Servlet
開啟這個選項可以讓DispatcherServlet處理根路徑/下的靜態資源請求,說的詳細點就是假如靜態文件是webapp/css/site.css,那么我們可以直接通過/css/site.css來訪問這個文件。如果不啟用這個功能,那么靜態文件就只能映射到其他路徑下比如/static。
這個配置項實際上會配置一個DefaultServletHttpRequestHandler,映射到路徑/**,并具有最低的優先級。由于DefaultServletHttpRequestHandler會將所有請求轉發到默認Servlet,所以它必須被配置為最后一個處理映射才行。
使用Java配置的話,重寫configureDefaultServletHandling(...)方法并開啟該選項。
1
2
3
4
5
6
7
8
9
10
|
@Configuration @EnableWebMvc public class WebConfig extends WebMvcConfigurerAdapter { @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); } } |
如果使用XML配置的話,添加以下行。
1
|
< mvc:default-servlet-handler /> |
消息轉換
如果我們需要覆蓋Spring默認的消息轉換器,可以重寫configureMessageConverters(List<HttpMessageConverter<?>> converters)方法,然后向converters參數添加我們自己的消息轉換器。如果僅僅希望增加自己的類型轉換器,重寫extendMessageConverters()方法。
使用XML配置文件的話,可以使用MVC命名空間。
1
2
3
4
5
6
7
8
9
10
|
< mvc:annotation-driven > < mvc:message-converters > < bean class = "org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" > < property name = "objectMapper" ref = "objectMapper" /> </ bean > < bean class = "org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter" > < property name = "objectMapper" ref = "xmlMapper" /> </ bean > </ mvc:message-converters > </ mvc:annotation-driven > |
高級自定義配置
上面的配置使用Spring提供的簡化類或者MVC命名空間,幫助我們快速配置功能。有時候可能需要更高級的功能定制,這樣就需要自己處理這些底層Bean的初始化和屬性設置。
Java配置自定義
我們先來看一看@EnableWebMvc注解的定義。可以看到它還使用了一個@Import注解,引用了DelegatingWebMvcConfiguration類。當我們注解@EnableWebMvc的時候,實際上初始化和配置的底層類就是DelegatingWebMvcConfiguration。
1
2
3
4
5
6
|
@Retention (RetentionPolicy.RUNTIME) @Target (ElementType.TYPE) @Documented @Import (DelegatingWebMvcConfiguration. class ) public @interface EnableWebMvc { } |
因此我們如果要自定義MVC的話,第一件事就是移除注解@EnableWebMvc。然后繼承DelegatingWebMvcConfiguration類并實現它的requestMappingHandlerAdapter()方法。
1
2
3
4
5
6
7
8
9
10
|
@Configuration public class WebConfig extends DelegatingWebMvcConfiguration { @Override @Bean public RequestMappingHandlerAdapter requestMappingHandlerAdapter() { // 在這里進行高級配置 } } |
在項目中DelegatingWebMvcConfiguration子類和@EnableWebMvc注解配置類只能存在一個,因為它們做的事情實際上是一樣的。而且這里的配置并不影響Spring MVC的其他配置。
自定義MVC命名空間配置
這里的自定義配置更困難,因為Spring沒有提供相應的配置機制。如果實在需要自定義MVC命名空間配置,可以考慮使用Spring提供的BeanPostProcessor機制,在檢測到Bean之后修改它的值。
1
2
3
4
5
6
7
8
9
10
|
@Component public class MyPostProcessor implements BeanPostProcessor { public Object postProcessBeforeInitialization(Object bean, String name) throws BeansException { if (bean instanceof RequestMappingHandlerAdapter) { // 在這里自定義屬性 } } } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.jianshu.com/p/f9925c4e6211