SpringMVC 中整合JSONXML视图一.docx

上传人:b****6 文档编号:4404020 上传时间:2022-12-01 格式:DOCX 页数:26 大小:41.81KB
下载 相关 举报
SpringMVC 中整合JSONXML视图一.docx_第1页
第1页 / 共26页
SpringMVC 中整合JSONXML视图一.docx_第2页
第2页 / 共26页
SpringMVC 中整合JSONXML视图一.docx_第3页
第3页 / 共26页
SpringMVC 中整合JSONXML视图一.docx_第4页
第4页 / 共26页
SpringMVC 中整合JSONXML视图一.docx_第5页
第5页 / 共26页
点击查看更多>>
下载资源
资源描述

SpringMVC 中整合JSONXML视图一.docx

《SpringMVC 中整合JSONXML视图一.docx》由会员分享,可在线阅读,更多相关《SpringMVC 中整合JSONXML视图一.docx(26页珍藏版)》请在冰豆网上搜索。

SpringMVC 中整合JSONXML视图一.docx

SpringMVC中整合JSONXML视图一

SpringMVC中整合JSON、XML视图一

SpringMVC中整合了JSON、XML的视图,可以通过这些视图完成Java对象到XML、JSON的转换。

转换XML提供了MarshallingView,开发者只需用注入相应的marshaller、和属性配置,即可自动完成Java的Model对象中的数据到XML的编组。

Email:

hoojo_@

Blog:

一、 准备工作

1、本次程序会涉及到Jackson、xStream、Jibx、Jaxb2、castor等技术,如果你对这些技术还不是很了解。

建议阅读:

这篇文章中涉及到的内容应该对你有不少帮助。

2、jar包下载

spring各版本jar下载地址:

相关的依赖包也可以在这里找到:

3、至少需要以下jar包

4、当前工程的web.xml配置

xmlversion="1.0"encoding="UTF-8"?

>

xmlns="

xmlns:

xsi="http:

//www.w3.org/2001/XMLSchema-instance"

xsi:

schemaLocation="

<--配置Spring核心控制器-->

dispatcher

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

/WEB-INF/dispatcher.xml

1

dispatcher

*.do

<--解决工程编码过滤器-->

characterEncodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

UTF-8

characterEncodingFilter

/*

index.jsp

5、WEB-INF中的dispatcher.xml配置

xmlversion="1.0"encoding="UTF-8"?

>

//www.springframework.org/schema/beans"

xmlns:

mvc="http:

//www.springframework.org/schema/mvc"

xmlns:

context="http:

//www.springframework.org/schema/context"

xmlns:

util="http:

//www.springframework.org/schema/util"

xmlns:

xsi="http:

//www.w3.org/2001/XMLSchema-instance"

xsi:

schemaLocation="http:

//www.springframework.org/schema/beans

http:

//www.springframework.org/schema/beans/spring-beans-3.0.xsd

http:

//www.springframework.org/schema/mvc

http:

//www.springframework.org/schema/mvc/spring-mvc-3.0.xsd

http:

//www.springframework.org/schema/context

http:

//www.springframework.org/schema/context/spring-context-3.0.xsd

http:

//www.springframework.org/schema/util

http:

//www.springframework.org/schema/util/spring-util-3.0.xsd">

<--注解探测器-->

component-scanbase-package="com.hoo.controller"/>

<--视图解析器,根据视图的名称newModelAndView(name),在配置文件查找对应的bean配置-->

启动后,可以看到index.jsp没有出现异常或错误。

那么当前SpringMVC的配置就成功了。

二、 利用Jaxb2编组XML

1、Jaxb2可以完成XML和Java的相互转换,在WebService中用得较多。

前面也介绍过Jaxb2的用法。

在线博文:

Forcnblogs:

Forcsdn:

2、首先在dispatcher.xml中配置Jaxb2的marshaller的视图,配置如下:

<--xml视图,Jaxb2Marshaller,需要配置对象和对象添加Annotationxml注解,不需要添加额外的jar包-->

com.hoo.entity.User

com.hoo.entity.AccountBean

com.hoo.entity.MapBean

com.hoo.entity.ListBean

Jaxb2的jar在jdk中已经包含,所以不需要添加额外的jar包。

详细信息你可以参考1中的博文。

上面的jaxb2MarshallingView视图的class是MarshallingView,它有一个构造器需要传递一个Marshaller。

Marshaller主要完成编组,即将Java对象转换成XML的这么一个东东。

我们在这个构造器中注入了Jaxb2Marshaller这个类,这个bean有一个classesToBeBound属性,这个属性是一个数组。

需要将即将转换成XML的Java对象配置在这里。

而且这些对象需要进行Annotation注解。

3、创建Jaxb2MarshallingViewController,完成Java对象到XML的转换

单个JavaBean的转换,代码如下:

packagecom.hoo.controller;

 

importjava.util.ArrayList;

importjava.util.Date;

importjava.util.List;

importorg.springframework.stereotype.Controller;

importorg.springframework.web.bind.annotation.RequestMapping;

importorg.springframework.web.servlet.ModelAndView;

importcom.hoo.entity.AccountBean;

importcom.hoo.entity.Brithday;

importcom.hoo.entity.DifferBean;

importcom.hoo.entity.ListBean;

importcom.hoo.entity.MoreBean;

importcom.hoo.entity.User;

 

/**

*function:

Jaxb2MarshallingView视图,利用Jaxb2进行Java对象到XML的转换技术

*@authorhoojo

*@createDate2011-4-27下午03:

20:

23

*@fileJaxb2MarshallingViewController.java

*@packagecom.hoo.controller

*@projectSpringMVC4View

*@blog

*@emailhoojo_@

*@version1.0

*/

@Controller

@RequestMapping("/jaxb2/view")

publicclassJaxb2MarshallingViewController{

/*

*MarshallingViewJaxb2Marshaller需要配置转换成xml的java对象的Annotation

*/

@RequestMapping("/doXMLJaxb2")

publicModelAndViewdoXMLJaxb2View(){

System.out.println("#################ViewControllerdoXMLJaxb2View##################");

ModelAndViewmav=newModelAndView("jaxb2MarshallingView");

AccountBeanbean=newAccountBean();

bean.setAddress("address");

bean.setEmail("email");

bean.setId

(1);

bean.setName("haha");

Brithdayday=newBrithday();

day.setBrithday("2010-11-22");

bean.setBrithday(day);

mav.addObject(bean);

returnmav;

}

}

上面的代码的ModelAndView配置了jaxb2MarshallingView这个视图,就表示结果集会通过这个视图进行编组后显示。

上面需要转换的AccountBean和Birthday对象,这些对象需要配置annotation,前面已经讲到annotation对JavaBean转换XML的作用。

我们来看看AccountBean对象代码:

packagecom.hoo.entity;

 

importjavax.xml.bind.annotation.XmlElement;

importjavax.xml.bind.annotation.XmlRootElement;

 

@XmlRootElement(name="account")

publicclassAccountBean{

privateintid;

privateStringname;

privateStringemail;

privateStringaddress;

privateBrithdaybrithday;

@XmlElement

publicBrithdaygetBrithday(){

returnbrithday;

}

publicvoidsetBrithday(Brithdaybrithday){

this.brithday=brithday;

}

@XmlElement

publicintgetId(){

returnid;

}

publicvoidsetId(intid){

this.id=id;

}

@XmlElement

publicStringgetName(){

returnname;

}

publicvoidsetName(Stringname){

this.name=name;

}

@XmlElement

publicStringgetEmail(){

returnemail;

}

publicvoidsetEmail(Stringemail){

this.email=email;

}

@XmlElement

publicStringgetAddress(){

returnaddress;

}

publicvoidsetAddress(Stringaddress){

this.address=address;

}

@Override

publicStringtoString(){

returnthis.name+"#"+this.id+"#"+this.address+"#"+this.brithday+"#"+this.email;

}

}

在getter方法都有部分注解,如果你想了解更多的jaxb2的相关技术,参考:

Brithday

packagecom.hoo.entity;

 

publicclassBrithday{

privateStringbrithday;

publicBrithday(){}

publicBrithday(Stringbrithday){

this.brithday=brithday;

}

publicStringgetBrithday(){

returnbrithday;

}

 

publicvoidsetBrithday(Stringbrithday){

this.brithday=brithday;

}

}

Brithday是AccountBean中的一个属性,在AccountBean中已经注解过。

这里就不需要进行注解配置。

通过浏览器请求:

http:

//localhost:

8080/SpringMVC4View/jaxb2/view/doXMLJaxb2.do

结果如下:

xmlversion="1.0"encoding="UTF-8"standalone="yes"?

>

address
2010-11-22

email1haha

4、转换带List属性的JavaEntity

/**

*function:

转换带有List属性的JavaBean

*@authorhoojo

*@createDate2011-4-27下午05:

32:

22

*@return

*/

@RequestMapping("/doListXMLJaxb2")

publicModelAndViewdoListXMLJaxb2View(){

System.out.println("#################ViewControllerdoListXMLJaxb2View##################");

ModelAndViewmav=newModelAndView("jaxb2MarshallingView");

Listbeans=newArrayList();

for(inti=0;i<3;i++){

AccountBeanbean=newAccountBean();

bean.setAddress("address#"+i);

bean.setEmail("email"+i+"@12"+i+".com");

bean.setId(1+i);

bean.setName("haha#"+i);

Brithdayday=newBrithday();

day.setBrithday("2010-11-2"+i);

bean.setBrithday(day);

beans.add(bean);

Useruser=newUser();

user.setAddress("chinaGuangZhou#"+i);

user.setAge(23+i);

user.setBrithday(newDate());

user.setName("jack#"+i);

user.setSex(Boolean.parseBoolean(i+""));

beans.add(user);

}

ListBeanlist=newListBean();

list.setList(beans);

mav.addObject(list);

returnmav;

}

ListBean注解过的代码

packagecom.hoo.entity;

 

importjava.util.List;

importjavax.xml.bind.annotation.XmlElement;

importjavax.xml.bind.annotation.XmlElements;

importjavax.xml.bind.annotation.XmlRootElement;

 

@SuppressWarnings("unchecked")

@XmlRootElement

publicclassListBean{

privateStringname;

privateListlist;

@XmlElements({

@XmlElement(name="user",type=User.class),

@XmlElement(name="account",type=AccountBean.class),

})

publicListgetList(){

returnlist;

}

 

publicvoidsetList(Listlist){

this.list=list;

}

 

publicStringgetName(){

returnname;

}

 

publicvoidsetName(Stringname){

this.name=name;

}

}

通过上面的注解可以看出List中只能存储User、AccountBean对象,关于User对象的代码和AccountBean对象的是一样的,只是类名不同而已。

读者可以自己添加。

在WebBrowser中请求:

http:

//localhost:

8080/SpringMVC4View/jaxb2/view/doListXMLJaxb2.do

结果如下:

xmlversion="1.0"encoding="UTF-8"standalone="yes"?

>

展开阅读全文
相关搜索

当前位置:首页 > 高中教育 > 初中教育

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1