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页面提供了一种模板机制,它能将网页的布局和内容分离 ...
随机推荐
- 跟我学LFS LiveUSB制作
LFS LiveCD启动 插入U盘,查看U盘相应的设备名 $ sudo /sbin/fdisk -l ... Device Boot Start End Block ...
- Bootstrap 源码解析(转)
1.Bootstrap的作用域 2.Bootstrap的类定义 3.Bootstrap的插件定义 4.Bootstrap的事件代理 5.Bootstrap的对象数据缓存 6.Bootstrap的防冲突 ...
- HTML.ActionLink 和Html.Action和 Url.Action 的区别
1. html.ActionLink生成一个<a href=".."></a>标记..例如:@Html.ActionLink(“链接文本”.“someact ...
- 55人班37人进清华北大的金牌教师之32条教育建言! z
他带的一个55人的班,37人考进清华.北大,10人进入剑桥大学.耶鲁大学.牛津大学等世界名校并获全额奖学金,其他考入复旦.南开等大学.不仅 如此,校足球冠军.校运动会总冠军.校网页设计大赛总冠军等6项 ...
- uva11992-Fast Matrix Operations(区间增值、改值)
题意: r行c列的全0矩阵 有三种操作 1 x1 y1 x2 y2 v子矩阵(x1,y1,x2,y2)所有元素增加v 2 x1 y1 x2 y2 v子矩阵(x1,y1,x2,y2)所有元素设为v ...
- minicom 没有tx 信号
在minicom -s 的配置中:——> Serial port setup --> 选择F - Hardware Flow Control : No默认是yes, 但是没有tx信号输出 ...
- OpenCV - Operations on Arrays 对数组(矩阵)的一些操作
Function (函数名) Use (函数用处) add 矩阵加法,A+B的更高级形式,支持mask scaleAdd 矩阵加法,一个带有缩放因子dst(I) = scale * src1(I) + ...
- 洛谷 P1169 [ZJOI2007]棋盘制作
2016-05-31 14:56:17 题目链接: 洛谷 P1169 [ZJOI2007]棋盘制作 题目大意: 给定一块矩形,求出满足棋盘式黑白间隔的最大矩形大小和最大正方形大小 解法: 神犇王知昆的 ...
- Microsoft Dynamics CRM 数据库连接存储位置在哪里 是在注册表里
Microsoft Dynamics CRM 数据库连接存储位置是在注册表里
- Java序列化之transient和serialVersionUID的使用
package FileDemo; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IO ...