- 浏览: 59816 次
- 性别:
- 来自: 四川
最新评论
DWR 2.0 增加了一个很有趣的新特性,Annotation,因此可以摆脱了dwr.xml里面的配置.同时也可以方便的和spring整合.
从官方网站下载dwr.jar包。然后将它放在你webapp的WEB-INF/lib目录下。 修改web.xml
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (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-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)
public class Book{
@RemoteProperty
private Stringname;
@RemoteProperty
private Stringauthor;
public StringgetAuthor(){
return author;
}
public void setAuthor(Stringauthor){
this .author = author;
}
public StringgetName(){
return name;
}
public void setName(Stringname){
this .name = name;
}
}
public class Book{
@RemoteProperty
private Stringname;
@RemoteProperty
private Stringauthor;
public StringgetAuthor(){
return author;
}
public void setAuthor(Stringauthor){
this .author = author;
}
public StringgetName(){
return name;
}
public void setName(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{};
}
@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
{
}
@Retention(RetentionPolicy.RUNTIME)
public@interfaceRemoteProperty
{
}
如果使用dwr.xml配置,可以这样:
<convertconverter="bean"match="com.beans.Book">
<paramname="include"value="name,author"/>
</convert>
<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;
}
}
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>
<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>
<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>
<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>
<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中的一些函数.
发表评论
-
struts2 中获取 web资源的方式
2016-01-08 17:02 466在struts2中获取 web资源的方式大致可分为获取扩展 ... -
sql server、db2、oracle 存储过程动态sql语句示例
2015-01-31 19:00 1786Oracle CREATE OR REPLACE PRO ... -
ie9 中出现不明的异常(参数是必选项 (Argument not optional)、尚未实现)等
2015-01-27 23:57 1266<script type="text/jav ... -
SAXParseException: The content of element type "configuration" must match
2014-09-15 23:57 1923在mybatis的配置文件中新增加<databaseI ... -
java.lang.ClassNotFoundException: Cannot find class: DB_VENDOR
2014-09-15 23:45 1924在mybatis 中使用databaseIdProvide ... -
spring mvc3 + fastjson 转换 REST 参数以及输出
2014-03-27 00:16 4187spring 3可以支持Rest风格参数,其内置了jack ... -
eclipse中编辑log4j 的xml配置文件时,自动提示
2014-03-27 00:05 838方法1. 配置log4j.dtd文件: Windows - ... -
java json 转换之 Jackson 框架
2013-08-01 21:50 1104转自:http://www.cnblogs.com/hooj ... -
eclipse/myeclise 自定义注释中的变量名称
2013-07-01 23:18 932eclipse/myeclipse中自带了 ... -
Eclipse Class Decompiler——Java反编译插件
2012-11-08 22:41 1201Eclipse Class Decompiler,整 ... -
struts+spring+hibernate整合问题解决方法 陆续更新中....
2009-01-01 10:33 839在整合ssh过程中出现的问题和解决方法: 一、 严重: Ser ... -
div嵌套页面 div加载页面 (其中获取目标页面的内容是用dwr框架连接java程序做的)
2009-03-15 16:58 625前段时间因为有需要,要在div中加载一个页面。但是以前没做过, ... -
tapestry autocomplete 更改样式及定位
2009-07-09 19:02 675版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文 ... -
Java中的main()方法详解
2009-07-12 16:23 606源文作者:leizhimin 源文链接:http:/ ... -
tapestry5 集成 spring 的事务管理
2009-07-22 15:43 553说明:使用的是tapestry5.1.0.5 和spring ... -
tapestry5 布局 参数的处理
2009-07-23 17:24 372<?xml version="1.0" ... -
tapestry不支持 等html特殊符号的解决方法
2009-07-23 17:34 475在使用tapestry时,偶然发现使用 &a ... -
WebService大讲堂之Axis2(1):用POJO实现0配置的WebService
2009-07-28 15:01 396转自:http://space.itpub.net ... -
WebService大讲堂之Axis2(2):复合类型数据的传递
2009-07-28 15:25 465转自:http://space.itpub.net/12921 ... -
WebService大讲堂之Axis2(3):使用services.xml文件发布WebService
2009-07-28 15:28 474转自:http://space.itpub.net/12921 ...
相关推荐
struts2.1 spring2.5 ibatis2.3 dwr3 annotation配置 集成 此中例子不完整. 如要下载:http://download.csdn.net/source/2138885
spring使用annotation整合dwr笔记 spring使用annotation整合dwr笔记 spring使用annotation整合dwr笔记
spring2.5 ibatis2.3 struts2.1 dwr3 annotation集成配置
DWR示例 包括DWR基本配置 DWR传递对象 DWR 反转ajax DWR与spring集成
dwr与 spring整合 传对象的例子
dwr+hibernate+spring 集成 实例 dwr+hibernate+spring 集成 实例
dwr+spring集成配置 dwr采用annotation自动扫描的方式
dwr与spring集成的两种方式,来之网上搜索的文档。。
DWR+Struts+spring+hibernate的订货系统,自己添加的dwr功能
配置整合DWR3.0和Spring2.5使用annotation注解
精心整理的,dwr与目前流行框架spring和struts2的整合,值得一学
将dwr集成到spring mvc(dwr的配置是基于xml)
DWR学习笔记和与spring整合练习,DWR学习笔记和与spring整合练习,DWR学习笔记和与spring整合练习,DWR学习笔记和与spring整合练习,
《疯狂Ajax讲义:Prototype/jQuery+DWR+Spring+Hibernate整合开发》是《基于J2EE的Ajax宝典》的第二版。《基于J2EE的Ajax宝典》面市近2年,作为Ajax领域最全面、实用的图书,一直深受读者的好评。全书主要分为三个...
使用dwr+spring实现消息推送 。
dwr与spring整合的相关资料收集dwr与spring整合的相关资料收集dwr与spring整合的相关资料收集dwr与spring整合的相关资料收集
Dwr3+Spring3 全注解 annotation 方式实现
dwr与spring集成,spring jdbc 数据库集成,spring注入例子
NULL 博文链接:https://json20080301.iteye.com/blog/1892385
包含基本的Dwr的使用和Spring的整合处理