springMVC整合xStream
一. 简单介绍:
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的更多相关文章
- 六:Dubbo与Zookeeper、SpringMvc整合和使用
DUBBO与ZOOKEEPER.SPRINGMVC整合和使用 互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架 ...
- (转)Dubbo与Zookeeper、SpringMVC整合和使用
原文地址: https://my.oschina.net/zhengweishan/blog/693163 Dubbo与Zookeeper.SpringMVC整合和使用 osc码云托管地址:http: ...
- SSM整合(三):Spring4与Mybatis3与SpringMVC整合
源码下载 SSMDemo 上一节整合了Mybatis3与Spring4,接下来整合SpringMVC! 说明:整合SpringMVC必须是在web项目中,所以前期,新建的就是web项目! 本节全部采用 ...
- Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)
互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的.现在核心业务抽取出来,作为独立的服务,使 ...
- springmvc整合fastjson
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- 【转】Dubbo_与Zookeeper、SpringMVC整合和使用(负载均衡、容错)
原文链接:http://blog.csdn.net/congcong68/article/details/41113239 互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服 ...
- 160906、Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)
互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的.现在核心业务抽取出来,作为独立的服务,使 ...
- Springmvc整合tiles框架简单入门示例(maven)
Springmvc整合tiles框架简单入门示例(maven) 本教程基于Springmvc,spring mvc和maven怎么弄就不具体说了,这边就只简单说tiles框架的整合. 先贴上源码(免积 ...
- SpringMVC整合Tiles框架
SpringMVC整合Tiles框架 Tiles组件 tiles-iconfig.xml Tiles是一个JSP布局框架. Tiles框架为创建Web页面提供了一种模板机制,它能将网页的布局和内容分离 ...
随机推荐
- 在Silverlight中的DispatcherTimer的Tick中使用基于事件的异步请求
需求:在silverlight用户界面上使用计时器定时刷新数据. 在 Silverlight 中的 DispatcherTimer 的 Tick 事件 中使用异步请求数据时,会出现多次请求的问题,以下 ...
- bzoj1015:1015: [JSOI2008]星球大战starwar
应该是全部读入之后再添加边用并查集就可以了. yyl用空间换时间.u[]v[]等将边预存起来. #include<cstdio> #include<cstring> #incl ...
- NOI2002robot
这题又是纯数论题…… 独立数就是欧拉函数,政客和军人的含义已经说的很清楚了,学者是最多的…… 首先,如果我们知道了政客和军人的答案,那就只要用n的所有因子的欧拉函数值减去这两个值,然后取模就行了. 但 ...
- [POJ 1674] Sorting by Swapping
Sorting by Swapping Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9514 Accepted: 50 ...
- Bsie(鄙视IE)
http://www.bootcss.com/p/bsie/ 欢迎,这是bsie项目主页. 简介 bsie弥补了Bootstrap对IE6的不兼容.Bootstrap是 twitter.com 推出的 ...
- (九)学习CSS之margin属性
参考: 所有浏览器都支持 margin 属性. 注释:任何的版本的 Internet Explorer (包括 IE8)都不支持属性值 "inherit". 定义和用法 margi ...
- JS组件Bootstrap实现弹出框和提示框效果代码
这篇文章主要介绍了JS组件Bootstrap实现弹出框和提示框效果代码,对弹出框和提示框感兴趣的小伙伴们可以参考一下 前言:对于Web开发人员,弹出框和提示框的使用肯定不会陌生,比如常见的表格新增和编 ...
- 设计模式_Visitor_访问者模式
形象例子: 情人节到了,要给每个MM送一束鲜花和一张卡片,可是每个MM送的花都要针 对她个人的特点,每张卡片也要根据个人的特点来挑,我一个人哪搞得清楚,还是找花店老板和礼品店老板做一下Visitor, ...
- FZU 2127 养鸡场
Problem Description Jason买来了n米长的竹篱笆,打算将n米长的竹篱笆全部用来围成一个三角形的养鸡场.为方便起见,养鸡场三条边的长度都为正整数.同时,他想让自己的养鸡场看起来更 ...
- SimpleHttpServer的学习之UML
如何分析一个稍微大点的源码呢? 静态分析 除了看代码,就是 uml图,UML虽然在书本类与类之间的关系很复杂,可能要一本书,但是最核心的其实很简单: (1)继承 extends (2)实现接口 imp ...