SpringMVC中整合了JSON、XML的視圖,可以通過這些視圖完成Java對象到XML、JSON的轉換。轉換XML提供了MarshallingView,開發者只需用注入相應的marshaller、和屬性配置,即可自動完成Java的Model對象中的數據到XML的編組。
Email:hoojo_@126.com
Blog:[http://blog.csdn.net/IBM_hoojo](http://blog.csdn.net/IBM_hoojo)
[http://hoojo.cnblogs.com/](http://hoojo.cnblogs.com/)
### 一、準備工作
1、 本次程序會涉及到Jackson、xStream、Jibx、Jaxb2、castor等技術,如果你對這些技術還不是很了解。建議閱讀:[http://www.cnblogs.com/hoojo/archive/2011/04/27/2030264.html](http://www.cnblogs.com/hoojo/archive/2011/04/27/2030264.html)
這篇文章中涉及到的內容應該對你有不少幫助。
2、 jar包下載
spring各版本jar下載地址:[http://ebr.springsource.com/repository/app/library/detail?name=org.springframework.spring](http://ebr.springsource.com/repository/app/library/detail?name=org.springframework.spring)
相關的依賴包也可以在這里找到:[http://ebr.springsource.com/repository/app/library](http://ebr.springsource.com/repository/app/library)
3、 至少需要以下jar包
[](http://hi.csdn.net/attachment/201104/29/0_1304048530oJvk.gif)
4、 當前工程的web.xml配置
~~~
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<-- 配置Spring核心控制器 -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<-- 解決工程編碼過濾器 -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
~~~
5、 WEB-INF中的dispatcher.xml配置
~~~
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<-- 注解探測器 -->
<context:component-scan base-package="com.hoo.controller"/>
<-- 視圖解析器,根據視圖的名稱new ModelAndView(name),在配置文件查找對應的bean配置 -->
<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>
</beans>
~~~
啟動后,可以看到index.jsp 沒有出現異常或錯誤。那么當前SpringMVC的配置就成功了。
### 二、利用Jaxb2編組XML
1、 Jaxb2可以完成XML和Java的相互轉換,在WebService中用得較多。前面也介紹過Jaxb2 的用法。
在線博文:
For cnblogs:[http://www.cnblogs.com/hoojo/archive/2011/04/26/2029011.html](http://www.cnblogs.com/hoojo/archive/2011/04/26/2029011.html)
For csdn:[http://blog.csdn.net/IBM_hoojo/archive/2011/04/26/6363491.aspx](http://blog.csdn.net/IBM_hoojo/archive/2011/04/26/6363491.aspx)
2、 首先在dispatcher.xml中配置Jaxb2的marshaller的視圖,配置如下:
~~~
<-- xml視圖,Jaxb2Marshaller,需要配置對象和對象添加Annotation xml注解,不需要添加額外的jar包 -->
<bean name="jaxb2MarshallingView" class="org.springframework.web.servlet.view.xml.MarshallingView">
<constructor-arg>
<bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<array>
<value>com.hoo.entity.User</value>
<value>com.hoo.entity.AccountBean</value>
<value>com.hoo.entity.MapBean</value>
<value>com.hoo.entity.ListBean</value>
</array>
</property>
</bean>
</constructor-arg>
</bean>
~~~
Jaxb2的jar在jdk中已經包含,所以不需要添加額外的jar包。詳細信息你可以參考1中的博文。上面的jaxb2MarshallingView視圖的class是MarshallingView,它有一個構造器需要傳遞一個Marshaller。Marshaller主要完成編組,即將Java對象轉換成XML的這么一個東東。我們在這個構造器中注入了Jaxb2Marshaller這個類,這個bean有一個classesToBeBound屬性,這個屬性是一個數組。需要將即將轉換成XML的Java對象配置在這里。而且這些對象需要進行Annotation注解。
3、 創建Jaxb2MarshallingViewController,完成Java對象到XML的轉換
單個JavaBean的轉換,代碼如下:
~~~
package com.hoo.controller;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.hoo.entity.AccountBean;
import com.hoo.entity.Brithday;
import com.hoo.entity.DifferBean;
import com.hoo.entity.ListBean;
import com.hoo.entity.MoreBean;
import com.hoo.entity.User;
/**
* function:Jaxb2MarshallingView 視圖,利用Jaxb2進行Java對象到XML的轉換技術
* @author hoojo
* @createDate 2011-4-27 下午03:20:23
* @file Jaxb2MarshallingViewController.java
* @package com.hoo.controller
* @project SpringMVC4View
* @blog http://blog.csdn.net/IBM_hoojo
* @email hoojo_@126.com
* @version 1.0
*/
@Controller
@RequestMapping("/jaxb2/view")
public class Jaxb2MarshallingViewController {
/*
* MarshallingView Jaxb2Marshaller 需要配置轉換成xml的java對象的Annotation
*/
@RequestMapping("/doXMLJaxb2")
public ModelAndView doXMLJaxb2View() {
System.out.println("#################ViewController doXMLJaxb2View##################");
ModelAndView mav = new ModelAndView("jaxb2MarshallingView");
AccountBean bean = new AccountBean();
bean.setAddress("address");
bean.setEmail("email");
bean.setId(1);
bean.setName("haha");
Brithday day = new Brithday();
day.setBrithday("2010-11-22");
bean.setBrithday(day);
mav.addObject(bean);
return mav;
}
}
~~~
上面的代碼的ModelAndView配置了jaxb2MarshallingView這個視圖,就表示結果集會通過這個視圖進行編組后顯示。上面需要轉換的AccountBean和Birthday對象,這些對象需要配置annotation,[前面已經講到annotation對JavaBean轉換XML的作用](http://www.cnblogs.com/hoojo/archive/2011/04/26/2029011.html)。我們來看看AccountBean對象代碼:
~~~
package com.hoo.entity;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "account")
public class AccountBean {
private int id;
private String name;
private String email;
private String address;
private Brithday brithday;
@XmlElement
public Brithday getBrithday() {
return brithday;
}
public void setBrithday(Brithday brithday) {
this.brithday = brithday;
}
@XmlElement
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@XmlElement
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlElement
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@XmlElement
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return this.name + "#" + this.id + "#" + this.address + "#" + this.brithday + "#" + this.email;
}
}
~~~
在getter方法都有部分注解,如果你想了解更多的jaxb2的相關技術,參考:[http://www.cnblogs.com/hoojo/archive/2011/04/26/2029011.html](http://www.cnblogs.com/hoojo/archive/2011/04/26/2029011.html)
Brithday
~~~
package com.hoo.entity;
public class Brithday {
private String brithday;
public Brithday() {}
public Brithday(String brithday) {
this.brithday = brithday;
}
public String getBrithday() {
return brithday;
}
public void setBrithday(String brithday) {
this.brithday = brithday;
}
}
~~~
Brithday是AccountBean中的一個屬性,在AccountBean中已經注解過。這里就不需要進行注解配置。
通過瀏覽器請求:[http://localhost:8080/SpringMVC4View/jaxb2/view/doXMLJaxb2.do](http://localhost:8080/SpringMVC4View/jaxb2/view/doXMLJaxb2.do)
結果如下:
~~~
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<account><address>address</address><brithday><brithday>2010-11-22</brithday></brithday>
<email>email</email><id>1</id><name>haha</name></account>
~~~
4、 轉換帶List屬性的JavaEntity
~~~
/**
* function:轉換帶有List屬性的JavaBean
* @author hoojo
* @createDate 2011-4-27 下午05:32:22
* @return
*/
@RequestMapping("/doListXMLJaxb2")
public ModelAndView doListXMLJaxb2View() {
System.out.println("#################ViewController doListXMLJaxb2View##################");
ModelAndView mav = new ModelAndView("jaxb2MarshallingView");
List<Object> beans = new ArrayList<Object>();
for (int i = 0; i < 3; i++) {
AccountBean bean = new AccountBean();
bean.setAddress("address#" + i);
bean.setEmail("email" + i + "@12" + i + ".com");
bean.setId(1 + i);
bean.setName("haha#" + i);
Brithday day = new Brithday();
day.setBrithday("2010-11-2" + i);
bean.setBrithday(day);
beans.add(bean);
User user = new User();
user.setAddress("china GuangZhou# " + i);
user.setAge(23 + i);
user.setBrithday(new Date());
user.setName("jack#" + i);
user.setSex(Boolean.parseBoolean(i + ""));
beans.add(user);
}
ListBean list = new ListBean();
list.setList(beans);
mav.addObject(list);
return mav;
}
~~~
ListBean注解過的代碼
~~~
package com.hoo.entity;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElements;
import javax.xml.bind.annotation.XmlRootElement;
@SuppressWarnings("unchecked")
@XmlRootElement
public class ListBean {
private String name;
private List list;
@XmlElements({
@XmlElement(name = "user", type = User.class),
@XmlElement(name = "account", type = AccountBean.class),
})
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
~~~
通過上面的注解可以看出List中只能存儲User、AccountBean對象,關于User對象的代碼和AccountBean對象的是一樣的,只是類名不同而已。讀者可以自己添加。在WebBrowser中請求:[http://localhost:8080/SpringMVC4View/jaxb2/view/doListXMLJaxb2.do](http://localhost:8080/SpringMVC4View/jaxb2/view/doListXMLJaxb2.do)
結果如下:
~~~
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<listBean>
<account><address>address#0</address><brithday><brithday>2010-11-20</brithday></brithday>
<email>email0@120.com</email><id>1</id><name>haha#0</name></account>
<user><address>china GuangZhou# 0</address><age>23</age>
<brithday>2011-04-27T17:02:38.028+08:00</brithday><name>jack#0</name><sex>false</sex></user>
<account><address>address#1</address><brithday><brithday>2010-11-21</brithday></brithday>
<email>email1@121.com</email><id>2</id><name>haha#1</name></account>
<user><address>china GuangZhou# 1</address><age>24</age>
<brithday>2011-04-27T17:02:38.028+08:00</brithday><name>jack#1</name><sex>false</sex></user>
<account><address>address#2</address><brithday><brithday>2010-11-22</brithday></brithday>
<email>email2@122.com</email><id>3</id><name>haha#2</name></account>
<user><address>china GuangZhou# 2</address><age>25</age>
<brithday>2011-04-27T17:02:38.028+08:00</brithday><name>jack#2</name><sex>false</sex></user>
</listBean>
~~~
5、 轉換帶有Map屬性的JavaBean,Jaxb2轉換Map有點復雜,先看看代碼:
~~~
/**
* function:轉換帶有Map屬性的JavaBean
* @author hoojo
* @createDate 2011-4-27 下午05:32:42
* @return
*/
@RequestMapping("/doMapXMLJaxb2")
public ModelAndView doMapXMLJaxb2View() {
System.out.println("#################ViewController doMapXMLJaxb2View##################");
ModelAndView mav = new ModelAndView("jaxb2MarshallingView");
MapBean mapBean = new MapBean();
HashMap map = new HashMap();
AccountBean bean = new AccountBean();
bean.setAddress("北京");
bean.setEmail("email");
bean.setId(1);
bean.setName("jack");
Brithday day = new Brithday();
day.setBrithday("2010-11-22");
bean.setBrithday(day);
map.put("NO1", bean);
bean = new AccountBean();
bean.setAddress("china");
bean.setEmail("tom@125.com");
bean.setId(2);
bean.setName("tom");
day = new Brithday("2011-11-22");
bean.setBrithday(day);
map.put("NO2", bean);
mapBean.setMap(map);
mav.addObject(mapBean);
return mav;
}
~~~
首先看看MapBean的代碼,代碼很簡單就一個Map的屬性。
~~~
package com.hoo.entity;
import java.util.HashMap;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import com.hoo.util.MapAdapter;
@XmlRootElement
public class MapBean {
private HashMap map;
@XmlJavaTypeAdapter(MapAdapter.class)
public HashMap getMap() {
return map;
}
public void setMap(HashMap map) {
this.map = map;
}
}
~~~
上面的代碼的getMap方法設置了XmlJavaTypeAdapter這個注解,注解里面的MapAdapter是我們自己實現的,而且還要構建一個MapElements數組元素。主要是繼承XmlAdapter重寫里面的幾個方法完成的。
MapAdapter代碼
~~~
package com.hoo.util;
import java.util.HashMap;
import java.util.Map;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import com.hoo.entity.AccountBean;
/**
* function:AccountBean 編組、解組的XmlAdapter
* @author hoojo
* @createDate 2011-4-25 下午05:03:18
* @file MyAdetper.java
* @package com.hoo.util
* @project WebHttpUtils
* @blog http://blog.csdn.net/IBM_hoojo
* @email hoojo_@126.com
* @version 1.0
*/
public class MapAdapter extends XmlAdapter> {
public MapElements[] marshal(Map arg0) throws Exception {
MapElements[] mapElements = new MapElements[arg0.size()];
int i = 0;
for (Map.Entry entry : arg0.entrySet())
mapElements[i++] = new MapElements(entry.getKey(), entry.getValue());
return mapElements;
}
public Map unmarshal(MapElements[] arg0) throws Exception {
Map r = new HashMap();
for (MapElements mapelement : arg0)
r.put(mapelement.key, mapelement.value);
return r;
}
}
~~~
MapElements代碼
~~~
package com.hoo.util;
import javax.xml.bind.annotation.XmlElement;
import com.hoo.entity.AccountBean;
/**
* function: MapElements
* @author hoojo
* @createDate 2011-4-25 下午05:04:04
* @file MyElements.java
* @package com.hoo.util
* @project WebHttpUtils
* @blog http://blog.csdn.net/IBM_hoojo
* @email hoojo_@126.com
* @version 1.0
*/
public class MapElements {
@XmlElement
public String key;
@XmlElement
public AccountBean value;
@SuppressWarnings("unused")
private MapElements() {
} // Required by JAXB
public MapElements(String key, AccountBean value) {
this.key = key;
this.value = value;
}
}
~~~
在瀏覽器中請求:[http://localhost:8080/SpringMVC4View/jaxb2/view/doMapXMLJaxb2.do](http://localhost:8080/SpringMVC4View/jaxb2/view/doMapXMLJaxb2.do)
結果如下:
~~~
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<mapBean><map><item><key>NO2</key><value><address>china</address>
<brithday><brithday>2011-11-22</brithday></brithday>
<email>tom@125.com</email><id>2</id><name>tom</name></value></item>
<item><key>NO1</key><value><address>北京</address><brithday><brithday>2010-11-22</brithday></brithday>
<email>email</email><id>1</id><name>jack</name></value></item></map>
</mapBean>
~~~
總結,如果你想將一些Java的基本類型轉換成XML。那么你得創建一個帶getter、setter方法的JavaBean。然后在Bean的getter方法進行相應的Annotation注解即可完成轉換。
### 三、利用xStream轉換XML
1、 xStream可以輕易的將Java對象轉換成XML、JSON,Spring整合利用xStream轉換xml。需要添加xStream相關的xstream-1.3.1.jar包。 如果你對xStream不上很了解,你可以先閱讀這篇文章:
For csblogs:[http://www.cnblogs.com/hoojo/archive/2011/04/22/2025197.html](http://www.cnblogs.com/hoojo/archive/2011/04/22/2025197.html)
For csdn:[http://blog.csdn.net/IBM_hoojo/archive/2011/04/22/6342386.aspx](http://blog.csdn.net/IBM_hoojo/archive/2011/04/22/6342386.aspx)
2、 然后在dispatcher.xml中添加如下配置
~~~
<-- xml視圖,XStreamMarshaller,可以轉換任何形式的java對象,需要添加xStream jar包 -->
<bean name="xStreamMarshallingView" class="org.springframework.web.servlet.view.xml.MarshallingView">
<property name="marshaller">
<bean class="org.springframework.oxm.xstream.XStreamMarshaller">
<-- 啟用annotation -->
<property name="autodetectAnnotations" value="true"/>
<-- 類名別名 -->
<property name="aliases">
<map>
<-- Account這個類的別名就變成了myBeans,那么轉換后的xml中就是myBeans -->
<entry key="myBeans" value="com.hoo.entity.Account"/>
</map>
</property>
<-- 基本屬性別名 -->
<property name="fieldAliases">
<map>
<-- Account中的brithday這個屬性 -->
<entry key="com.hoo.entity.Account.brithday" value="生日"/>
</map>
</property>
</bean>
</property>
</bean>
~~~
上次配置的參數有注釋描述,還要沒有配置的參數。如:annotatedClass、annotatedClasses是當沒有配置啟用annotation的時候,可以用這2個屬性進行配置你指定的class啟用annotation注解。streamDriver是配置驅動用的,默認可以不要驅動,你可以配置DomDriver、JSON相關的驅動。encoding是設置編碼,關于XStreamMarshaller還要更多的參數配置和上面xStream 的博文中講解的一樣使用,只是通過配置,而博文中是直接在代碼中寫的。當然也可以通過annotation進行注解哦;如果你想了解更多xStream的用法,請你仔細閱讀:[http://www.cnblogs.com/hoojo/archive/2011/04/22/2025197.html](http://www.cnblogs.com/hoojo/archive/2011/04/22/2025197.html)
3、 普通JavaBean轉換XML
~~~
package com.hoo.controller;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.hoo.entity.Account;
import com.hoo.entity.AccountArray;
import com.hoo.entity.AccountBean;
import com.hoo.entity.Brithday;
import com.hoo.entity.DifferBean;
import com.hoo.entity.ListBean;
import com.hoo.entity.MapBean;
import com.hoo.entity.MoreBean;
import com.hoo.entity.User;
/**
* function:Jaxb2MarshallingView 視圖,利用Jaxb2進行Java對象到XML的轉換技術
* @author hoojo
* @createDate 2011-4-27 下午03:20:23
* @file Jaxb2MarshallingViewController.java
* @package com.hoo.controller
* @project SpringMVC4View
* @blog http://blog.csdn.net/IBM_hoojo
* @email hoojo_@126.com
* @version 1.0
*/
@Controller
@RequestMapping("/jaxb2/view")
public class Jaxb2MarshallingViewController {
/*
* MarshallingView Jaxb2Marshaller 需要配置轉換成xml的java對象的Annotation
*/
@RequestMapping("/doXMLJaxb2")
public ModelAndView doXMLJaxb2View() {
System.out.println("#################ViewController doXMLJaxb2View##################");
ModelAndView mav = new ModelAndView("jaxb2MarshallingView");
AccountBean bean = new AccountBean();
bean.setAddress("address");
bean.setEmail("email");
bean.setId(1);
bean.setName("haha");
Brithday day = new Brithday();
day.setBrithday("2010-11-22");
bean.setBrithday(day);
mav.addObject(bean);
return mav;
}
}
~~~
AccountBean上面的代碼已經給出,這里就不重復了。值得說明的是xStream在ModelAndView中,直接用addObject方法添加時,有時候出現一些不是我們轉換的對象的信息,一般是BindingResult的xml信息。解決辦法就是設置addObject的key。Key設置為BindingResult.*MODEL_KEY_PREFIX*這樣就可以了,代碼上面已經給出。
在瀏覽器中請求:[http://localhost:8080/SpringMVC4View/xStream/view/doXMLXStream.do](http://localhost:8080/SpringMVC4View/xStream/view/doXMLXStream.do)
結果如下:
~~~
<com.hoo.entity.AccountBean><id>1</id><name>haha</name><email>email</email><address>北京</address>
<brithday><brithday>2010-11-22</brithday></brithday></com.hoo.entity.AccountBean>
~~~
4、 轉換對象數組
代碼如下:
~~~
/**
* function:轉換對象數組
* @author hoojo
* @createDate 2011-4-27 下午06:19:40
* @return
*/
@RequestMapping("/doMoreXMLXStream")
public ModelAndView doMoreXMLXStreamView() {
System.out.println("#################ViewController doMoreXMLXStreamView##################");
ModelAndView mav = new ModelAndView("xStreamMarshallingView");
Account[] accs = new Account[2];
Account bean = new Account();
bean.setAddress("北京");
bean.setEmail("email");
bean.setId(1);
bean.setName("haha");
Brithday day = new Brithday();
day.setBrithday("2010-11-22");
bean.setBrithday(day);
accs[0] = bean;
bean = new Account();
bean.setAddress("上海");
bean.setEmail("email");
bean.setId(1);
bean.setName("haha");
day = new Brithday();
day.setBrithday("2010-11-22");
bean.setBrithday(day);
accs[1] = bean;
mav.addObject(accs);
return mav;
}
~~~
在WebBrowser中請求[http://localhost:8080/SpringMVC4View/xStream/view/doMoreXMLXStream.do](http://localhost:8080/SpringMVC4View/xStream/view/doMoreXMLXStream.do)
結果如下:
~~~
<myBeans-array><myBeans><id>1</id><name>haha</name><email>email</email>
<address>北京</address><生日><brithday>2010-11-22</brithday></生日></myBeans>
<myBeans><id>1</id><name>haha</name><email>email</email><address>上海</address>
<生日><brithday>2010-11-22</brithday></生日></myBeans></myBeans-array>
~~~
結果中的myBeans、生日就是在dispatcher配置文件中重命名的對象屬性名稱。
5、 轉換Map集合
~~~
/**
* function:轉換Map對象
* @author hoojo
* @createDate 2011-4-27 下午06:19:48
* @return
*/
@RequestMapping("/doDifferXMLXStream")
public ModelAndView doDifferXMLXStreamView() {
System.out.println("#################ViewController doDifferXMLXStreamView##################");
ModelAndView mav = new ModelAndView("xStreamMarshallingView");
Account bean = new Account();
bean.setAddress("廣東");
bean.setEmail("email");
bean.setId(1);
bean.setName("haha");
Brithday day = new Brithday();
day.setBrithday("2010-11-22");
bean.setBrithday(day);
User user = new User();
user.setAddress("china GuangZhou");
user.setAge(23);
user.setBrithday(new Date());
user.setName("jack");
user.setSex(true);
Map map = new HashMap();
map.put("bean", bean);
map.put("user", user);
mav.addObject(map);
return mav;
}
~~~
在WebBrowser中請求[http://localhost:8080/SpringMVC4View/xStream/view/doDifferXMLXStream.do](http://localhost:8080/SpringMVC4View/xStream/view/doDifferXMLXStream.do)
結果如下:
~~~
<map><entry>
<string>bean</string><myBeans><id>1</id><name>haha</name><email>email</email>
<address>廣東</address><生日><brithday>2010-11-22</brithday></生日></myBeans>
</entry><entry><string>user</string><com.hoo.entity.User><name>jack</name><age>23</age><sex>true</sex>
<address>china GuangZhou</address><brithday>2011-04-27 19:02:13.747 CST</brithday></com.hoo.entity.User>
</entry></map>
~~~
6、 轉換List集合
~~~
/**
* function:轉換List對象
* @author hoojo
* @createDate 2011-4-27 下午06:20:02
* @return
*/
@RequestMapping("/doListXMLXStream")
public ModelAndView doListXMLXStreamView() {
System.out.println("#################ViewController doListXMLXStreamView##################");
ModelAndView mav = new ModelAndView("xStreamMarshallingView");
List<Object> beans = new ArrayList<Object>();
for (int i = 0; i < 3; i++) {
Account bean = new Account();
bean.setAddress("北京#" + i);
bean.setEmail("email" + i + "@12" + i + ".com");
bean.setId(1 + i);
bean.setName("haha#" + i);
Brithday day = new Brithday();
day.setBrithday("2010-11-2" + i);
bean.setBrithday(day);
beans.add(bean);
User user = new User();
user.setAddress("china GuangZhou 廣州# " + i);
user.setAge(23 + i);
user.setBrithday(new Date());
user.setName("jack#" + i);
user.setSex(Boolean.parseBoolean(i + ""));
beans.add(user);
}
mav.addObject(beans);
return mav;
}
~~~
在WebBrowser中請求[http://localhost:8080/SpringMVC4View/xStream/view/doListXMLXStream.do](http://localhost:8080/SpringMVC4View/xStream/view/doListXMLXStream.do)
結果如下:
~~~
<list>
<myBeans><id>1</id><name>haha#0</name><email>email0@120.com</email><address>北京#0</address><生日>
<brithday>2010-11-20</brithday></生日></myBeans>
<com.hoo.entity.User><name>jack#0</name><age>23</age><sex>false</sex><address>china GuangZhou 廣州# 0</address>
<brithday>2011-04-27 19:08:40.106 CST</brithday></com.hoo.entity.User>
<myBeans><id>2</id><name>haha#1</name><email>email1@121.com</email><address>北京#1</address><生日>
<brithday>2010-11-21</brithday></生日></myBeans>
<com.hoo.entity.User><name>jack#1</name><age>24</age><sex>false</sex><address>china GuangZhou 廣州# 1</address>
<brithday>2011-04-27 19:08:40.106 CST</brithday></com.hoo.entity.User>
<myBeans><id>3</id><name>haha#2</name><email>email2@122.com</email><address>北京#2</address><生日>
<brithday>2010-11-22</brithday></生日></myBeans>
<com.hoo.entity.User><name>jack#2</name><age>25</age><sex>false</sex><address>china GuangZhou 廣州# 2</address>
<brithday>2011-04-27 19:08:40.106 CST</brithday></com.hoo.entity.User></list>
~~~
總結,xStream相對jaxb2要簡單些。而且相對比較靈活,可以輕易的轉換Java普通類型。
下次會介紹castor轉換XML、jibx轉換XML、Jackson轉換JSON 以及自定義Jsonlib視圖轉換Json。