一. 简单介绍:

xStream能够轻易的将Java对象转换成xml、JSON。本篇博客将使用springMVC整合利用xStream转换xml。

关于xStream使用的博文:http://blog.csdn.net/zdp072/article/details/39054197

二. 实例:

1. 代码结构图:

2. 实体类:

(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.address + "#" + this.birthday + "#" + this.email;
}
}

(2)User

public class User {
private String name;
private int age;
private Boolean sex;
private String address;
private Birthday birthday; // 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配置:

<?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.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 加入注解驱动 -->
<mvc:annotation-driven /> <!-- 默认扫描的包路径 -->
<context:component-scan base-package="com.zdp" /> <!-- 视图解析器 -->
<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视图,XStreamMarshaller,能够转换不论什么形式的java对象 -->
<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.zdp.domain.Account" />
</map>
</property> <!-- 基本属性别名 -->
<property name="fieldAliases">
<map>
<!-- Account中的brithday这个属性 -->
<entry key="com.zdp.domain.Account.birthday" value="birthday" />
</map>
</property>
</bean>
</property>
</bean>
</beans>

4. XstreamController

/**
* 利用xStream进行Java对象到XML的转换技术
*/
@Controller
@RequestMapping("/xstream/view")
public class XStreamController { // 普通JavaBean转换成XML
// url: http://localhost:8080/springmvc_xStream/xstream/view/doXMLXstream
@RequestMapping("/doXMLXstream")
public ModelAndView doXMLJaxb2View() {
ModelAndView mav = new ModelAndView("xStreamMarshallingView");
Account account = new Account();
account.setAddress("address");
account.setEmail("email");
account.setId(1);
account.setName("haha");
Birthday day = new Birthday();
day.setBirthday("2010-11-22");
account.setBirthday(day);
mav.addObject(BindingResult.MODEL_KEY_PREFIX, account);
return mav;
} // 转换带List属性的JavaBean
// url: http://localhost:8080/springmvc_xStream/xstream/view/doListXMLXstream
@RequestMapping("/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("北京#" + 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 广州# " + i);
user.setAge(23 + i);
user.setBirthday(birthday);
user.setName("jack#" + i);
user.setSex(Boolean.parseBoolean(i + ""));
list.add(user);
} mav.addObject(list);
return mav;
} // 转换带有Map属性的JavaBean
// url: http://localhost:8080/springmvc_xStream/xstream/view/doMapXMLXstream
@RequestMapping("/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(23);
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;
} // 转换数组
// 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("上海");
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;
}
}

源代码下载:http://download.csdn.net/detail/zdp072/7866271

原文:http://blog.csdn.net/ibm_hoojo/article/details/6371647

springMVC整合xStream的更多相关文章

  1. 六:Dubbo与Zookeeper、SpringMvc整合和使用

    DUBBO与ZOOKEEPER.SPRINGMVC整合和使用 互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架 ...

  2. (转)Dubbo与Zookeeper、SpringMVC整合和使用

    原文地址: https://my.oschina.net/zhengweishan/blog/693163 Dubbo与Zookeeper.SpringMVC整合和使用 osc码云托管地址:http: ...

  3. SSM整合(三):Spring4与Mybatis3与SpringMVC整合

    源码下载 SSMDemo 上一节整合了Mybatis3与Spring4,接下来整合SpringMVC! 说明:整合SpringMVC必须是在web项目中,所以前期,新建的就是web项目! 本节全部采用 ...

  4. Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)

    互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的.现在核心业务抽取出来,作为独立的服务,使 ...

  5. springmvc整合fastjson

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  6. 【转】Dubbo_与Zookeeper、SpringMVC整合和使用(负载均衡、容错)

    原文链接:http://blog.csdn.net/congcong68/article/details/41113239 互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服 ...

  7. 160906、Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)

    互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的.现在核心业务抽取出来,作为独立的服务,使 ...

  8. Springmvc整合tiles框架简单入门示例(maven)

    Springmvc整合tiles框架简单入门示例(maven) 本教程基于Springmvc,spring mvc和maven怎么弄就不具体说了,这边就只简单说tiles框架的整合. 先贴上源码(免积 ...

  9. SpringMVC整合Tiles框架

    SpringMVC整合Tiles框架 Tiles组件 tiles-iconfig.xml Tiles是一个JSP布局框架. Tiles框架为创建Web页面提供了一种模板机制,它能将网页的布局和内容分离 ...

随机推荐

  1. ASP.NET的六种验证控件的使用

    C# 中的验证控件分为一下六种 :1 CompareValidator:比较验证,两个字段的值是否相等,比如判断用户输入的密码和确认密码是否一致,则可以用改控件: 2 CustomValidator ...

  2. js变量作用域

    for(var i =0;i<100;i++) { } alert(i);//100 if(true){ var i="91d"; } alert(i);//91d func ...

  3. JSP文件上传代码

    一.首先建立一个上传的界面,取名为a.jsp,代码如下 <%@ page contentType="text/html; charset=utf-8" language=&q ...

  4. UVA 12436-Rip Van Winkle's Code(线段树的区间更新)

    题意: long long data[250001]; void A( int st, int nd ) { for( int i = st; i \le nd; i++ ) data[i] = da ...

  5. HDU 5628 Clarke and math Dirichlet卷积+快速幂

    题意:bc round 72 中文题面 分析(官方题解): 如果学过Dirichlet卷积的话知道这玩意就是g(n)=(f*1^k)(n), 由于有结合律,所以我们快速幂一下1^k就行了. 当然,强行 ...

  6. [原创]使用squish打包与混淆cocos2d-x的lua脚本

    squish是一个开源的用于打包lua脚本的小工具,它的主要功能是将多个lua文件整合成一个文件,并在此基础上做压缩和混淆等处理,混淆和压缩后的代码可以直接被执行而不需要先做解压还原等操作. 它的gi ...

  7. [解决]ASP.NET MVC 4/5 源码调试(source code debug)

    ========================ASP.NET MVC 4============================ ASP.NET MVC 4 source code download ...

  8. algorithm@ find kth smallest element in two sorted arrays (O(log n time)

    The trivial way, O(m + n): Merge both arrays and the k-th smallest element could be accessed directl ...

  9. Ubuntu 固态硬盘 4K对齐及启用 Trim,及其验证方法

    因为之前一个移动硬盘因为坏道蔓延导致没办法继续使用,我略冲动地跑去买了一块 120GB 的三星840 固态硬盘回来.为了使用起来更方便,还去弄了个光驱位硬盘托架,把固态硬盘接在了光驱位与原本的笔记本硬 ...

  10. git http方式时保存密码

    一直使用ssh方式,但是git@osc的ssh只能pull,不能push  - -|||    htts方式保存密码老是忘记,每次提交代码都要输入密码烦死了.找到文章备忘: 转自:http://git ...