DWR通过Annotation与spring整合

DWR 2.0 增加了一个很有趣的新特性,Annotation,因此可以摆脱了dwr.xml里面的配置.同时也可以方便的和spring整合.从官方网站下载dwr.jar包。然后将它放在你webapp的WEB-INF/lib目录下。 修改web.xml

<!--<br><br>CodehighlightingproducedbyActiproCodeHighlighter(freeware)<br>http://www.CodeHighlighter.com/<br><br>--><servlet>

<servlet-name>dwr-invoker</servlet-name>

<servlet-class>

org.directwebremoting.servlet.DwrServlet

</servlet-class>

<init-param>

<param-name>debug</param-name>

<param-value>true</param-value>

</init-param>

<init-param>

<param-name>logLevel</param-name>

<param-value>DEBUG</param-value>

</init-param>

<init-param>

<param-name>classes</param-name>

<param-value>

com.spring.User,com.beans.Book

</param-value>

</init-param>

</servlet>

<servlet-mapping>

<servlet-name>dwr-invoker</servlet-name>

<url-pattern>/dwr/*</url-pattern>

</servlet-mapping>

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>

/WEB-INF/beans.xml

</param-value>

</context-param>

<listener>

<listener-class>

org.springframework.web.context.ContextLoaderListener

</listener-class>

</ listener >

beans.xml是spring的配置文件.org.springframework.web.context.ContextLoaderListener是一个监听器.

< init-param >

<param-name>classes</param-name>

<param-value>

com.spring.User,com.beans.Book

</param-value>

</init-param>

param-value:参数为被注解的类.

编写相关类

com.beans.Book:
@DataTransferObject(converter=BeanConverter.class)

publicclassBook{

@RemoteProperty

privateStringname;

@RemoteProperty

privateStringauthor;

publicStringgetAuthor(){

returnauthor;

}

publicvoidsetAuthor(Stringauthor){

this.author=author;

}

publicStringgetName(){

returnname;

}

publicvoidsetName(Stringname){

this.name=name;

}

}

@DataTransferObject: 标注在客户端和服务器之间转换类.对应dwr.xml中的<convert>标签.注解的源代码:

@Target(ElementType.TYPE)

@Retention(RetentionPolicy.RUNTIME)

public@interfaceDataTransferObject

{

/**

*Converterthatconvertsinstanceoftheclass(default:beanconverter).

*/

Class<?extendsConverter>converter()defaultBeanConverter.class;

/**

*Parametersfortheconverter.

*/

Param[]params()default{};

}

关于annotation可以看这篇文章,java元数据

@RemoteProperty:标注在类中需要转换的属性.

源代码:
@Target({ElementType.FIELD,ElementType.METHOD})

@Retention(RetentionPolicy.RUNTIME)

public@interfaceRemoteProperty

{

}

如果使用dwr.xml配置,可以这样:

<convertconverter="bean"match="com.beans.Book">

<paramname="include"value="name,author"/>

</convert>

User:

@RemoteProxy(name="user",creator=SpringCreator.class,

creatorParams={

@Param(name="beanName",value="user")

})

@DataTransferObject(converter=BeanConverter.class)

publicclassUser{

@RemoteProperty

privateStringwelcome;

@RemoteProperty

privateStringusername;

@RemoteProperty

privateStringaddress;

@RemoteProperty

privateList<Book>books;

@RemoteProperty

privateintage;

publicStringgetAddress(){

returnaddress;

}

publicvoidsetAddress(Stringaddress){

this.address=address;

}

publicintgetAge(){

returnage;

}

publicvoidsetAge(intage){

this.age=age;

}

@RemoteMethod

publicStringgetUsername(){

returnusername;

}

publicvoidsetUsername(Stringusername){

this.username=username;

}

publicStringgetWelcome(){

returnwelcome;

}

publicvoidsetWelcome(Stringwelcome){

this.welcome=welcome;

}

@RemoteMethod

publicList<Book>getBooks(){

returnbooks;

}

publicvoidsetBooks(List<Book>books){

this.books=books;

}

@RemoteMethod

publicUsergetUser(Stringwelcome){

this.welcome=welcome;

returnthis;

}

}

@RemoteProxy:标注要给远程调用的类.

RemoteProxy的name设置创造出来的对象的名字,creator指定使用那种创造器,例子中使用SpringCreator.creatorParams指定创造器的其他参数.不同的创造器参数是不同的.

@RemoteMethod:标注给远程调用的方法

4.修改beans.xml

<beanid="remote"class="com.spring.Remote"></bean>

<beanid="user"class="com.spring.User"

p:username="windfree"p:address="anhuihefei"p:age="25">

<propertyname="books">

<list>

<refbean="C"/>

<refbean="java"/>

</list>

</property>

</bean>

<beanid="java"class="com.beans.Book"p:name="java"p:author="mypure"></bean>

<beanid="C"class="com.beans.Book"p:name="C"p:author="zgliu"></bean>

其中p为spring2.0中提供的标签.5.html页面

<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">

<html>

<head>

<title>SpringUserInfo.html</title>

<scripttype='text/javascript'src='/DWRExample/dwr/interface/user.js'></script>

<scripttype='text/javascript'src='/DWRExample/dwr/engine.js'></script>

<scripttype='text/javascript'src='/DWRExample/dwr/util.js'></script>

<scripttype="text/javascript">

functiontest(){

user.getUser("Hello",callback);

}

varcellFuncs=[

function(data){returndata.name;},

function(data){returndata.author;},

];

functioncallback(user){

//alert(user.books)

DWRUtil.setValue('result',"欢迎你!"+"姓名:"+user.username+",年龄:"+user.age+",住址:"+user.address);

DWRUtil.addRows('tableInfo',user.books,cellFuncs,

{escapeHtml:false,

rowCreator:function(options){

varrow=document.createElement("tr");

varindex=options.rowIndex*50;

row.style.color="rgb("+index+",0,0)";

returnrow;

},

cellCreator:function(options){

vartd=document.createElement("td");

varindex=255-(options.rowIndex*50);

td.style.backgroundColor="rgb("+index+",255,255)";

td.style.fontWeight="bold";

returntd;

}

})

}

</script>

</head>

<body>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

<inputid="jbutton"type="button"value="取得信息"onclick="test()"/>

<br>

<divid="result"></div><br>

<tableborder="1">

<thead><tr>

<th>书名</th><th>姓名</th>

</tr></thead>

<tbodyid="tableInfo">

</tbody>

</table>

</body>

</html>

其中使用了util.js中的一些函数.

相关推荐