您的位置:首页 > 编程语言 > Java开发

springMVC整合xStream

2016-04-02 09:28 495 查看
一. 简单介绍:

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: