Spring 3.x MVC entry 3-1-multi-view example using content negotiation

Source: Internet
Author: User
Multi-view legend using content negotiation

Based on the introduction in the previous article, the example is provided here.

 

Configure XML

<Context: component-scan base-package ="Com. Controls"/>

<Context: annotation-config/>

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

<Entry key ="XML"Value ="Application/XML"/>

</Map>

</Property>

<Property name ="Defaultviews">

<List>

<Bean class ="Org. springframework. Web. servlet. View. JSON. mappingjacksonjsonview"> </Bean>

<Bean class ="Org. springframework. Web. servlet. View. xml. marshallingview">

<Constructor-Arg>

<Bean class ="Org. springframework. oxm. jaxb. jaxb2marshaller">

<Property name ="Classestobebound">

<List>

<Value> com. model. User </value>

</List>

</Property>

</Bean>

</Constructor-Arg>

</Bean>

</List>

</Property>

</Bean>

<! -- This view parser will be used if none of the above matches -->

<Bean class ="Org. springframework. Web. servlet. View. internalresourceviewresolver">

<Property name ="Order"Value ="2"/>

<Property name ="Prefix"Value =/WEB-INF/views /"/>

<Property name ="Suffix"Value =". Jsp"/>

<Property name ="Viewclass"Value ="Org. springframework. Web. servlet. View. jstlview"/>

</Bean>

 

 

Model

 

@ Xmlrootelement

Public ClassUser {

Private LongUserid;

PrivateString username;

PrivateDate birth;

 

PublicString GetUserName (){

ReturnUsername;

}

Public VoidSetusername (string username ){

This. Username = username;

}

PublicDate getbirth (){

ReturnBirth;

}

Public VoidSetbirth (date Birth ){

This. Birth = birth;

}

Public LongGetuserid (){

ReturnUserid;

}

Public VoidSetuserid (LongUserid ){

This. Userid = userid;

}

}

 

 

 

 

Contoller

@ Requestmapping (value = "/user/{userid }")

PublicString queryuser (@ pathvariable ("userid ")LongUserid, modelmap Model)

{

User u =NewUser ();

U. setuserid (userid );

U. setusername ("Zhaoyang ");

Model. addattribute ("user", U );

Return"User ";

}

 

 

If text/html is returned, you also need to create a JSP file

<Body>

Username: $ {requestscope. User. userid} <br/>

Age: $ {requestscope. User. Username}

</Body>

 

Test Results

JSON

 

XML

 

 

 

JSP

 

 

 

 

Related Article

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.