ContentNegotiatingViewResolver

Ads
 

ContentNegotiatingViewResolver

In this section, you will learn about Spring MVC view resolver : ContentNegotiatingViewResolver.

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 :

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



Ads

Ads