SpringMVC 中整合JSON.docx
- 文档编号:5931418
- 上传时间:2023-01-02
- 格式:DOCX
- 页数:39
- 大小:58.76KB
SpringMVC 中整合JSON.docx
《SpringMVC 中整合JSON.docx》由会员分享,可在线阅读,更多相关《SpringMVC 中整合JSON.docx(39页珍藏版)》请在冰豆网上搜索。
SpringMVC中整合JSON
SpringMVC中整合JSON、XML视图二
上篇文章介绍了程序整合的准备工作、结合MarshallingView视图完成jaxb2转换XML、xStream转换XML工作,这次将介绍castor、jibx转换XML。
还有MappingJacksonView用Jackson转换JSON,自己拓展AbstractView定义Jsonlib的视图完成JSON-lib转换JSON。
上一篇文章:
四、 用Castor转换XML
1、castor可以通过一个mapping.xml文件对即将转换的Java对象进行描述,然后可以将Java对象按照描述的情况输出XML内容。
利用castor转换xml需要添加如下jar包:
如果你还不清楚castor,可以阅读:
forcsblogs:
forcsdn:
2、你需要在dispatcher.xml中添加castor的相关视图,配置如下:
<--
继承MarshallingView,重写locateToBeMarshalled方法;
解决对象添加到ModelAndView中,转换后的xml是BindingResult信息的bug
-->
mapping.xml
mapping.xml配置
xmlversion="1.0"encoding="UTF-8"?
>
DOCTYPEmappingPUBLIC"-//EXOLAB/CastorMappingDTDVersion1.0//EN""http:
//castor.org/mapping.dtd">
关于mapping.xml配置的介绍,你可以参考
这篇文章的第三栏目。
3、在使用Spring的MarshallingView的时候,转换的xml结果有时候会带有BindingResult对象的信息。
所以解决办法是重写MarshallingView里面的locateToBeMarshalled方法,这样就可以解决了。
下面是重新MarshallingView的class代码:
packagecom.hoo.veiw.xml;
importjava.util.Map;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importorg.springframework.beans.BeansException;
importorg.springframework.oxm.Marshaller;
importorg.springframework.validation.BindingResult;
importorg.springframework.web.servlet.view.xml.MarshallingView;
/**
*function:
继承MarshallingView,重写locateToBeMarshalled方法;
*解决对象添加到ModelAndView中,转换后的xml是BindingResult信息的bug
*@authorhoojo
*@createDate2010-11-29下午05:
58:
45
*@fileOverrideMarshallingView.java
*@packagecom.hoo.veiw.xml
*@projectSpring3
*@blog
*@emailhoojo_@
*@version1.0
*/
publicclassOverrideMarshallingViewextendsMarshallingView{
privateMarshallermarshaller;
privateStringmodelKey;
publicOverrideMarshallingView(){
super();
}
publicOverrideMarshallingView(Marshallermarshaller){
super(marshaller);
this.marshaller=marshaller;
}
publicvoidsetMarshaller(Marshallermarshaller){
super.setMarshaller(marshaller);
this.marshaller=marshaller;
}
publicvoidsetModelKey(StringmodelKey){
super.setModelKey(modelKey);
this.modelKey=modelKey;
}
@Override
protectedvoidinitApplicationContext()throwsBeansException{
super.initApplicationContext();
}
@SuppressWarnings("unchecked")
@Override
protectedvoidrenderMergedOutputModel(Mapmodel,HttpServletRequestrequest,HttpServletResponseresponse)
throwsException{
super.renderMergedOutputModel(model,request,response);
}
@SuppressWarnings("unchecked")
@Override
protectedObjectlocateToBeMarshalled(Mapmodel)throwsServletException{
if(modelKey!
=null){
Objecto=model.get(modelKey);
if(!
this.marshaller.supports(o.getClass())){
thrownewServletException("Modelobject["+o+"]retrievedviakey["+modelKey
+"]isnotsupportedbytheMarshaller");
}
returno;
}
for(Objecto:
model.values()){
//解决对象添加到ModelAndView中,转换后的xml是BindingResult信息的bug
if(oinstanceofBindingResult){
continue;
}
if(this.marshaller.supports(o.getClass())){
returno;
}
}
returnnull;
}
}
4、下面来看看Castor来转换普通JavaBean
packagecom.hoo.controller;
importjava.util.ArrayList;
importjava.util.Date;
importjava.util.HashMap;
importjava.util.List;
importjava.util.Map;
importorg.springframework.stereotype.Controller;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.servlet.ModelAndView;
importcom.hoo.entity.Account;
importcom.hoo.entity.Brithday;
importcom.hoo.entity.ListBean;
importcom.hoo.entity.MapBean;
importcom.hoo.entity.User;
/**
*function:
利用MarshallingView视图,配置CastorMarshaller将Java对象转换XML
*@authorhoojo
*@createDate2011-4-28上午10:
14:
43
*@fileCastorMarshallingViewController.java
*@packagecom.hoo.controller
*@projectSpringMVC4View
*@blog
*@emailhoojo_@
*@version1.0
*/
@Controller
@RequestMapping("/castor/view")
publicclassCastorMarshallingViewController{
@RequestMapping("/doBeanXMLCastorView")
publicModelAndViewdoBeanXMLCastorView(){
System.out.println("#################ViewControllerdoBeanXMLCastorView##################");
ModelAndViewmav=newModelAndView("castorMarshallingView");
Accountbean=newAccount();
bean.setAddress("北京");
bean.setEmail("email");
bean.setId
(1);
bean.setName("haha");
Brithdayday=newBrithday();
day.setBrithday("2010-11-22");
bean.setBrithday(day);
Map
map.put("day",day);
map.put("account",bean);
mav.addObject(bean);//重写MarshallingView的locateToBeMarshalled方法
//mav.addObject(BindingResult.MODEL_KEY_PREFIX,bean);
//mav.addObject(BindingResult.MODEL_KEY_PREFIX+"account",bean);
returnmav;
}
}
Account在mapping配置文件中有进行配置描述。
在浏览器中请求http:
//localhost:
8080/SpringMVC4View/castor/view/doBeanXMLCastorView.do
就可以看到结果了,结果如下:
xmlversion="1.0"encoding="UTF-8"?
>
5、转换Map集合
@RequestMapping("/doMapXMLCastorView")
publicModelAndViewdoMapXMLCastorView(){
System.out.println("#################ViewControllerdoMapXMLCastorView##################");
ModelAndViewmav=newModelAndView("castorMarshallingView");
Accountbean=newAccount();
bean.setAddress("北京");
bean.setEmail("email");
bean.setId
(1);
bean.setName("haha");
Brithdayday=newBrithday();
day.setBrithday("2010-11-22");
bean.setBrithday(day);
Map
map.put("day",day);
map.put("account",bean);
MapBeandiffer=newMapBean();
differ.setMap(map);
mav.addObject(differ);
returnmav;
}
在浏览器中请求http:
//localhost:
8080/SpringMVC4View/castor/view/doMapXMLCastorView.do
结果如下:
xmlversion="1.0"encoding="UTF-8"?
>
6、转换List集合
@RequestMapping("/doListXMLCastorView")
publicModelAndViewdoListXMLCastorView(){
System.out.println("#################ViewControllerdoListXMLCastorView##################");
ModelAndViewmav=newModelAndView("castorMarshallingView");
List
for(inti=0;i<3;i++){
Accountbean=newAccount();
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);
}
ListBeanlistBean=newListBean();
listBean.setList(beans);
mav.addObject(listBean);
returnmav;
}
在WebBrowser中请求http:
//localhost:
8080/SpringMVC4View/castor/view/doListXMLCastorView.do
xmlversion="1.0"encoding="UTF-8"?
>
xsi="http: //www.w3.org/2001/XMLSchema-instance"xmlns: java="" age="23"sex="false"xsi: t
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- SpringMVC 中整合JSON 整合 JSON