ContentNegotiatingViewResolver

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

Ads

Tutorials   
Spring 3.2 MVC insert and retrieve blob from the database The hidden tag The errors tag net.roseindia.dao net.roseindia.service net.roseindia.model net.roseindia.controller Spring 3.2 MVC Hibernate Example Spring 3.2 MVC, Upload File in a specific folder Spring 3.2 MVC Form Handling The textarea tag The options tag The option tag The select tag The radiobuttons tag The radiobutton tag The checkboxes tag mvc:default-servlet-handler Static Resources Configuration View Controllers Configuration View resolvers Spring 3.2 MVC Hello World Example Content Negotiation Configuration Interceptors Configuration using Java or XML Customizing the MVC Java config or XML Namespace MVC Java Config or the MVC XML Namespace Servlet container initialization through code Support for ETag The checkbox tag Autogenerated logical view name through RequestToViewNameTranslator Modification in conventional ModelAndView ControllerClassNameHandlerMapping class for handling convention mapping The password tag The input tag The form tag Form Tag library configuration Customizing the Default Error Page @ExceptionHandler & @ResponseStatus annotation Multipart support for file upload in Spring MVC Themes and Theme resolvers in Spring MVC Locales in Spring MVC ContentNegotiatingViewResolver Redirecting and forwarding to views Chaining of Multiple view resolvers View Resolving through ViewResolver interface Requests Intercepting through a HandlerInterceptor Defining handler methods using @RequestMapping annotation Spring 3.2 Asynchronous Request Processing @RequestMapping annotation for mapping requests @Controller annotation for defining new controller
Ads

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



Advertisements

Share on Google+Share on Google+

ContentNegotiatingViewResolver

Posted on: January 10, 2013 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: ContentNegotiatingViewResolver  

Post your Comment


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

Ads

 

Ads