Home Spring Spring3.2 ContentNegotiatingViewResolver



ContentNegotiatingViewResolver
Posted on: January 10, 2013 at 12:00 AM
In this section, you will learn about Spring MVC view resolver : ContentNegotiatingViewResolver.

ContentNegotiatingViewResolver

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 :

  • Using a distinct file extension for each type of URI. For example: http://www,roseindia.net/java/tutorial_beginner.pdf requesting PDF representation and http://www,roseindia.net/java/tutorial_beginner.xml requesting XML representation.

  • By setting  Accept HTTP request header to list the media types that it can translate. For example, a request for http://www.roseindia.net/java/tutorial_beginner with an Accept header set to 'application/pdf' requests a PDF representation.

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"/>



Related Tags for ContentNegotiatingViewResolver:


More Tutorials from this section

Ask Questions?    Discuss: ContentNegotiatingViewResolver  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

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.