SPRINGMVC Contentnegotiatingviewresolver--based on the request Resource Name Selection View _SPRINGMVC principle analysis

Source: Internet
Author: User

The previous article describes how to automatically select XML or JSON using the @responsebody and accept property implementations in the HTTP Request header to return results.

Here's another way to use Contentnegotiatingviewresolver, which allows you to use the same content data to present different views.
It supports three ways, as described below:
1) using the extension
Http://localhost:8080/employees/nego/Jack.xml returns the result to XML
Http://localhost:8080/employees/nego/Jack.json returns the result to JSON
Http://localhost:8080/employees/nego/Jack Use default view rendering, such as JSP

2 Accept in HTTP Request header

3) Using Parameters
Http://localhost:8080/employees/nego/Jack?format=xml returns the result to XML
Http://localhost:8080/employees/nego/Jack?format=json returns the result to JSON


We use the way to achieve.

1) Add Controller method

[Java] View plain copy/** * jack.xml--return XML * Jack.json--return JSON * Jack-return to JSP:employee.jsp/@RequestM Apping (value= "/nego/{name}", Method=requestmethod.get) public string getemployeenegotiating (@PathVariable string       Name, Modelmap model) {Model.addattribute ("emp", New Employeex (name, 16000));   Return "Employee"; }

Employee.jsp is as follows

[Java] View plain copy <%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" > 2) Add the following configuration in Spring-mvc.xml

[html]  View Plain  copy <!-- ViewResolver -->       < bean class= "Org.springframework.web.servlet.view.ContentNegotiatingViewResolver" >            <property name= "Order"  value= "1"  />           <property name= "Favorparameter"  value= "false"  />           <property name= " Ignoreacceptheader " value=" true " />               <property name= "MediaTypes" >                <map>                    <entry key= "JSON"  value= "Application/json"  />    &NBSP;&NBSP;&NBSP;&Nbsp;            <entry key= "xml"   Value= "Application/xml"  />                </map>           </property>               <property name= "Defaultviews" >                <list>                   <!--  json view -->                    <bean class= "Org.springframework.web.servlet.view.json.MappingJackson2JsonView " />                    <!--&NBsp jaxb xml view -->                    <bean class= " Org.springframework.web.servlet.view.xml.MarshallingView ">                        <constructor-arg>                             <bean class= " Org.springframework.oxm.jaxb.Jaxb2Marshaller ">                                 <property name= "Classestobebound" >                            & NBsp;       <list>                                         <value>learning.webapp.model.employeex </value>                                     </list>                                </property>                             </bean>                        </constructor-arg>                    </bean>               </list>            </property>       </bean>           <!-- If no extension matched, use  jsp view -->       <bean class= " Org.springframework.web.servlet.view.InternalResourceViewResolver ">            <property name= "Order"  value= "2"  />            <property name= "Viewclass"  value= " Org.springframework.web.servlet.view.JstlView " />   &Nbsp;       <property name= "prefix"  value= "/WEB-INF/views/jsp/ " />           <property name=" suffix "  Value= ". jsp"  />       </bean>  
There are several important parameters:

<property name= "Favorpathextension" value= "true" ></property> whether extension support is enabled, default is True

<property name= "Favorparameter" value= "false" ></property> whether parameter support is enabled, default is True

<property name= "Ignoreacceptheader" value= "true" ></property> whether to ignore the Accept header, the default is False

We use the extension, so turn off the back two.


3 Add the following 2 dependencies in Pom add

[HTML] View plain copy <dependency> <groupId>com.fasterxml.jackson.core</groupId> <arti factid>jackson-databind</artifactid> <version>2.6.1</version> </dependency> <depen       Dency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId> <version>${spring.version}</version> </dependency>
Open the browser to access the following URL:



Resources

1) http://www.cnblogs.com/zhaoyang/archive/2012/01/07/2315428.html

2) http://www.mkyong.com/spring-mvc/spring-3-mvc-contentnegotiatingviewresolver-example/

Contentnegotiatingviewresolver process flow chart in data 1



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.