simpleDateFormat = Web MVC framework

4.3.9.RELEASE Spring Framework 562 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 bean id = objectMapper class = org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean p:indentOutput = true

p:simpleDateFormat =

yyyy-MM-dd p:modulesToInstall = com.fasterxml.jackson.module.paramnames.ParameterNamesModule bean id = xmlMapper parent = objectMapper p:createXmlMapper = true Advanced Customizations with MVC Java Config As you can see from the above examples, MVC Java config and the MVC namespace provide higher level constructs that do not require deep knowledge of the underlying beans created for you. Instead it helps you to focus on your application needs. However, at some point you may need more fine-grained control or you may simply wish to understand the underlying configuration. The first step towards more fine-grained control is to see the underlying beans created for you. In MVC Java config you can see the javadocs and the Bean methods in WebMvcConfigurationSupport . The configuration in this class is automatically imported through the EnableWebMvc annotation. In fact if you open EnableWebMvc you can see the Import statement. The next step towards more fine-grained control is to customize a property on one of the beans created in WebMvcConfigurationSupport or perhaps to provide your own instance. This requires two things — remove the EnableWebMvc annotation in order to prevent the import and then extend from DelegatingWebMvcConfiguration , a subclass of WebMvcConfigurationSupport . Here is an example: Configuration public class WebConfig extends DelegatingWebMvcConfiguration { Override public void addInterceptorsInterceptorRegistry registry{ ... } Override Bean public RequestMappingHandlerAdapter requestMappingHandlerAdapter { Create or let super create the adapter Then customize one of its properties } } Note An application should have only one configuration extending DelegatingWebMvcConfiguration or a single EnableWebMvc annotated class, since they both register the same underlying beans. 4.3.9.RELEASE Spring Framework 563 Modifying beans in this way does not prevent you from using any of the higher-level constructs shown earlier in this section. WebMvcConfigurerAdapter subclasses and WebMvcConfigurer implementations are still being used. Advanced Customizations with the MVC Namespace Fine-grained control over the configuration created for you is a bit harder with the MVC namespace. If you do need to do that, rather than replicating the configuration it provides, consider configuring a BeanPostProcessor that detects the bean you want to customize by type and then modifying its properties as necessary. For example: Component public class MyPostProcessor implements BeanPostProcessor { public Object postProcessBeforeInitializationObject bean, String name throws BeansException { if bean instanceof RequestMappingHandlerAdapter { Modify properties of the adapter } } } Note that MyPostProcessor needs to be included in an component scan in order for it to be detected or if you prefer you can declare it explicitly with an XML bean declaration. 4.3.9.RELEASE Spring Framework 564

23. View technologies

23.1 Introduction

One of the areas in which Spring excels is in the separation of view technologies from the rest of the MVC framework. For example, deciding to use Groovy Markup Templates or Thymeleaf in place of an existing JSP is primarily a matter of configuration. This chapter covers the major view technologies that work with Spring and touches briefly on how to add new ones. This chapter assumes you are already familiar with Section 22.5, “Resolving views” which covers the basics of how views in general are coupled to the MVC framework.

23.2 Thymeleaf

Thymeleaf is a good example of a view technology fitting perfectly in the MVC framework. Support for this integration is not provided by the Spring team but by the Thymeleaf team itself. Configuring Thymeleaf for Spring usually requires a few beans defined, like a ServletContextTemplateResolver , a SpringTemplateEngine and a ThymeleafViewResolver . Please refer to the Thymeleaf+Spring documentation section for more details.

23.3 Groovy Markup Templates

The Groovy Markup Template Engine is another view technology, supported by Spring. This template engine is a template engine primarily aimed at generating XML-like markup XML, XHTML, HTML5, … , but that can be used to generate any text based content. This requires Groovy 2.3.1+ on the classpath. Configuration Configuring the Groovy Markup Template Engine is quite easy: Configuration EnableWebMvc public class WebConfig extends WebMvcConfigurerAdapter { Override public void configureViewResolversViewResolverRegistry registry { registry.groovy; } Bean public GroovyMarkupConfigurer groovyMarkupConfigurer { GroovyMarkupConfigurer configurer = new GroovyMarkupConfigurer; configurer.setResourceLoaderPath WEB-INF ; return configurer; } } The XML counterpart using the MVC namespace is: mvc:annotation-driven mvc:view-resolvers mvc:groovy mvc:view-resolvers mvc:groovy-configurer resource-loader-path = WEB-INF