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