In my previous post I introduced the concept of content negotiation and the three strategies Spring MVC uses to determine the content requested.
In this post I want to extend the concept specifically to supporting multiple views for different content-types using the
ContentNegotiatingViewResolver (or CNVR).
Since we already know how to setup content-negotiation from the previous post, using it to select between multiple views is very straightforward. Simply define a CNVR like this:
<!-- // View resolver that delegates to other view resolvers based on the // content type --> <bean class="org.springframework.web.servlet.view. ContentNegotiatingViewResolver"> <!-- All configuration now done by manager - since Spring V3.2 --> <property name="contentNegotiationManager" ref="cnManager"/> </bean> <!-- // Setup a simple strategy: // 1. Only path extension is taken into account, Accept headers // are ignored. // 2. Return HTML by default when not sure. --> <bean id="cnManager" class="org.springframework.web.accept. ContentNegotiationManagerFactoryBean"> <property name="ignoreAcceptHeader" value="true"/> <property name="defaultContentType" value="text/html" /> </bean>