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页面提供了一种模板机制,它能将网页的布局和内容分离 ...
随机推荐
- CSS3实现jquery的特效
实现 “慕课网” 的图片滑过缩放的效果 技术点:css3—— -webkit-transform:scale(1.2); .course-list-img .img_1:hover{ -webki ...
- Hbase 建表基本命令总结
访问hbase,以及操作hbase,命令不用使用分号 hbase shell 进入hbase list 查看表 hbase shell -d hbase(main):024:0> scan '. ...
- Parallel for-each loops in .NET C# z
An IEnumerable object An Action of T which is used to process each item in the list List<string&g ...
- string 常用 方法
boost::array< char, 16 > header; string(header.begin(),header.end()) std::vector<uchar> ...
- WebOb的简单介绍
在之前的文章中我写了有关于如何使用PasteDeploy生成WSGI的Application.在Openstack的源码中,除了 PasteDeploy外,还有一个和WSGI密切相关的工具包WebOb ...
- Bzoj 4591: [Shoi2015]超能粒子炮·改 数论,Lucas定理,排列组合
4591: [Shoi2015]超能粒子炮·改 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 178 Solved: 70[Submit][Stat ...
- 【转】getopt分析命令行参数
(一) 在Linux中,用命令行执行可执行文件时可能会涉及到给其加入不同的参数的问题,例如: ./a.out -a1234 -b432 -c -d 程序会根据读取的参数执行相应的操作,在C语言中,这个 ...
- OpenCV在Debug和Release两种模式下布恩那个同时运行的问题
首先,可以肯定的说,两者是可以随时切换进行运行的,若不能运行,必定是配置出了问题 以Debugx64和Releasex64为例: 在Releasex64模式下,我配置好了各种路径: 于是乎,我切换到D ...
- 访问修饰符(access modifier)。
1)public(公共的):被 public所修饰的属性和方法可以被有类访问. 所修饰的属性和方法可以被有类访问. 2)protected (受保护的):被protected所修饰的属性和方法可以在类 ...
- leetcode@ [129] Sum Root to Leaf Numbers (DFS)
https://leetcode.com/problems/sum-root-to-leaf-numbers/ Given a binary tree containing digits from 0 ...