SPRINGMVC Support for multiple views

Source: Internet
Author: User

1, add in the Springmvc.xml
1 <!--support for multiple views -2         <Beanclass= "Org.springframework.web.servlet.view.ContentNegotiatingViewResolver">3             < Propertyname= "Contentnegotiationmanager">4                 <Beanclass= "Org.springframework.web.accept.ContentNegotiationManagerFactoryBean"/>5             </ Property>6             < Propertyname= "MediaTypes">7                 <Map>8                     <entryKey= "JSON"value= "Application/json"/>9                     <entryKey= "xml"value= "Application/xml"/>Ten                 </Map> One             </ Property> A             < Propertyname= "Defaultviews"> -                 <List> -                     <Beanclass= "Org.springframework.web.servlet.view.json.MappingJacksonJsonView"/> the                     <Beanclass= "Org.springframework.web.servlet.view.xml.MarshallingView"> -                         <Constructor-arg> -                             <Beanclass= "Org.springframework.oxm.jaxb.Jaxb2Marshaller"> -                                 < Propertyname= "Classestobebound"> +                                     <List> -                                         <value>Com.xiaostudy.domain.User</value> +                                     </List> A                                 </ Property> at                             </Bean> -                         </Constructor-arg> -                     </Bean> -                 </List> -             </ Property> -         </Bean>
2. Add annotations to classes that support multiple views @xmlrootelement
1  PackageCom.xiaostudy.domain;2 3 Importjavax.xml.bind.annotation.XmlRootElement;4 5 @XmlRootElement6  Public classUser {7 8     Private intID;9     PrivateString username;Ten     PrivateString password; One     Private intAge ; A  -      Public intgetId () { -         returnID; the     } -  -      Public voidSetId (intID) { -          This. ID =ID; +     } -  +      PublicString GetUserName () { A         returnusername; at     } -  -      Public voidSetusername (String username) { -          This. Username =username; -     } -  in      PublicString GetPassword () { -         returnpassword; to     } +  -      Public voidSetPassword (String password) { the          This. Password =password; *     } $ Panax Notoginseng      Public intGetage () { -         returnAge ; the     } +  A      Public voidSetage (intAge ) { the          This. Age =Age ; +     } -  $ @Override $      PublicString toString () { -         return"User [id=" + ID + ", username=" + Username + ", password=" + password + ", age=" + Age + "]"; -     } the  -}
3. Use
1  PackageCom.xiaostudy.controller;2 3 ImportOrg.springframework.stereotype.Controller;4 Importorg.springframework.web.bind.annotation.RequestMapping;5 6 ImportCom.xiaostudy.domain.User;7 8@Controller//<bean class= "Com.xiaostudy.controller.MyController"/>9@RequestMapping (value= "/mycontroller")//When you access the method of this class, there are many more such a path aheadTen  Public classMycontroller { One      A@RequestMapping ("MultiView") -      PublicUser MultiView () { -User User =NewUser (); theUser.setid (2018); -User.setusername ("Xiaostudy"); -User.setpassword ("123456"); -User.setage (23); +         returnuser; -     } +  A}
4. Visit

SPRINGMVC Support for multiple views

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.