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"?
>
address2010-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");
List
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"?
>