SPRINGMVC Integrated XStream

Source: Internet
Author: User

I. Introduction:

XStream can easily convert Java objects to XML, JSON. This blog will use SPRINGMVC integration to transform XML using XStream.

Blog posts about XStream use: http://blog.csdn.net/zdp072/article/details/39054197


Two. Example:

1. Code structure diagram:


2. Entity classes:

(1) Account

public class Account {    private int id;    private String name;    private String Email;    Private String address;    Private Birthday Birthday;    Getter and setter    @Override public    String toString () {        return this.name + "#" + this.id + "#" + this.addr ESS + "#" + This.birthday + "#" + This.email;    }}

(2) User

public class User {private string name;private int age;private Boolean sex;private string address;private Birthday Birthda y;//getter and Setter@override public    String toString () {        return this.name + "#" + This.age + "#" + This.sex + "# "+ this.address +" # "+ This.birthday.getBirthday ();    }}

(3) Birthday

public class Birthday {private String birthday;public Birthday () {}//getter and setter}

3. Spring Configuration:

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context" Xmlns:mvc= "Http://www.springframework.org/schema/mvc" xsi:schemalocation= "Http://www.springframework.org/schema /MVC Http://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/beans/HTTP Www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context/HTTP Www.springframework.org/schema/context/spring-context.xsd "><!--add note driver--><mvc:annotation-driven/ ><!--Default Scanned package path--><context:component-scan base-package= "COM.ZDP"/><!--view resolver--><bean class= "Org.springframework.web.servlet.view.BeanNameViewResolver" ><property name= "Order" value= "1"/></bean ><bean class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" ><property name= "vieWclass "value=" Org.springframework.web.servlet.view.JstlView "/></bean><!--XML view, Xstreammarshaller, You can convert any form of Java object--><bean name= "Xstreammarshallingview" class= " Org.springframework.web.servlet.view.xml.MarshallingView "><property name=" Marshaller "><bean class=" Org.springframework.oxm.xstream.XStreamMarshaller "><!--enable annotation--><property name=" Autodetectannotations "value=" true "/><!--class name alias--><property name=" aliases "><map><!-- The alias of the account class becomes Mybeans, then the converted XML is Mybeans--><entry key= "Mybeans" value= "Com.zdp.domain.Account"/> </map></property><!--Basic Property aliases--><property name= "fieldaliases" ><map><!-- The Brithday attribute in account--><entry key= "Com.zdp.domain.Account.birthday" value= "Birthday"/></map>< /property></bean></property></bean></beans>

4. Xstreamcontroller

/** * Using XStream for Java object to XML conversion technology */@Controller @requestmapping ("/xstream/view") public class Xstreamcontroller {// Ordinary JavaBean converted into xml//url:http://localhost:8080/springmvc_xstream/xstream/view/doxmlxstream@requestmapping ("/ Doxmlxstream ") Public Modelandview Doxmljaxb2view () {Modelandview Mav = new Modelandview (" Xstreammarshallingview "); Account Account = new account ("Address"), Account.setemail ("email"), Account.setid (1); account.setaddress; Account.setname ("haha"); Birthday day = new Birthday ();d ay.setbirthday ("2010-11-22"), Account.setbirthday (day); Mav.addobject ( Bindingresult.model_key_prefix, account); return MAV;} Convert the javabean//url:http://localhost:8080/springmvc_xstream/xstream/view/dolistxmlxstream@requestmapping with the list property ("/dolistxmlxstream") public Modelandview Dolistxmlxstreamview () {Modelandview Mav = new Modelandview (" Xstreammarshallingview "); list<object> list = new arraylist<object> (); for (int i = 0; i < 3; i++) {Account account = new account (); Account.setaddress ("Beijing #" + i); Account.setemail ("email" + i + "@12" + i + ". com"), Account.setid (1 + i); Account.setname ("haha#" + i); Birthday Birthday = new Birthday () birthday.setbirthday ("2010-11-2" + i); Account.setbirthday (Birthday); List.add ( account); User user = new user (); user.setaddress ("China GuangZhou Guangzhou #" + i); User.setage (+ i); User.setbirthday (birthday); user.se Tname ("jack#" + i); User.setsex (Boolean.parseboolean (i + ")"); List.add (user); Mav.addobject (list); return MAV;} Convert javabean//url:http://localhost:8080/springmvc_xstream/xstream/view/domapxmlxstream@requestmapping with the map attribute ( "/domapxmlxstream") public Modelandview Dodifferxmlxstreamview () {Modelandview Mav = new Modelandview (" Xstreammarshallingview "); Account Account = new Account ("Account.setaddress"), Account.setemail ("email"), Account.setid (1); Account.setname ("haha"); Birthday Birthday = new Birthday () birthday.setbirthday ("2010-11-22"); Account.setbirthday (Birthday); User user = new user (); user.setaddress ("China GuangZhou"); user.setage; user. Setbirthday (birthday); User.setname ("Jack"); User.setsex (true); map<string, object> map = new hashmap<string, object> (); Map.put ("account", account); Map.put ("User", user); Mav.addobject (map); return MAV;} Convert array//url:http://localhost:8080/springmvc_xstream/xstream/view/doarrayxmlxstream@requestmapping ("/ Doarrayxmlxstream ") Public Modelandview Doarrayxmlxstreamview () {Modelandview Mav = new Modelandview (" Xstreammarshallingview "); account[] Accountarr = new account[2]; Account Account = new Account ("Account.setaddress"), Account.setemail ("email"), Account.setid (1); Account.setname ("haha"); Birthday Birthday = new Birthday () birthday.setbirthday ("2010-11-22"); Account.setbirthday (Birthday); accountarr[0] = Account;account = new Account (), account.setaddress ("Shanghai"), Account.setemail ("email"); Account.setid (1); Account.setname ("haha"); birthday = new Birthday (); Birthday.setbirthday ("2014-11-22"); Account.setbirthday (Birthday ); Accountarr[1] = Account;mav.addobject (Accountarr); return MAV;}}

source Download: http://download.csdn.net/detail/zdp072/7866271

Original: http://blog.csdn.net/ibm_hoojo/article/details/6371647




SPRINGMVC Integrated XStream

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.