In this section, you will learn about Spring MVC view resolver : ContentNegotiatingViewResolver.
Spring introduces ContentNegotiatingViewResolver to provide multiple representations of a resource. The ContentNegotiatingViewResolver seeks other view resolver's help for resolving view rather resolve view itself. It choose the view according to the requested representation. Spring introduces ContentNegotiatingViewResolver to resolve view based on Accept header of the HTTP request or file extension.
There are two strategy exists for requesting representation from the server :
One problem with the Accept header is that it is not possible to set Accept header in a web browser within HTML.
Sample configuration of the ContentNegotiatingViewResolver is given below :
ass="prettyprint lang-java" style="width: 580px"> <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> <property name="mediaTypes"> <map> <entry key="atom" value="application/atom+xml"/> <entry key="html" value="text/html"/> <entry key="json" value="application/json"/> </map> </property> <property name="viewResolvers"> <list> <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </list> </property> <property name="defaultViews"> <list> <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" /> </list> </property> </bean> <bean id="format" class="net.roseindia.ExampleContentFormat"/>
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.