dwr 下载地址: http://www.directwebremoting.org/dwr/downloads/index.html
另外还需要一个 commons-logging-1.2.jar,
将dwr.jar 和commons-logging-1.2.jar 放进lib里面。
先看个入门教材吧,
http://www.cnblogs.com/cyjch/archive/2012/02/16/2353758.html ,这个写的很简单,照着配置就可以实现。
1、实体User
package iwco.entity; public class User { //登陆ID,主键唯一 private String id; //姓名 private String name; //口令 private String password; //电子邮件 private String email; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
2、DWRUserAccess类
package iwco.dwrs; import iwco.dao.UserDAO; import iwco.entity.User; public class DWRUserAccess { UserDAO userDAO = new UserDAO(); public boolean save(User user) { return userDAO.save(user); } public User find(String id) { return userDAO.find(id); } }
<servlet> <display-name>DWR Servlet</display-name> <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> </servlet> <servlet-mapping> <servlet-name>dwr-invoker</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping>
4、在WEB-INF/下面增加dwr.xml 内容如下:
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN" "http://getahead.org/dwr/dwr30.dtd"> <dwr> <allow> <create creator="new" javascript="JDate"> <param name="class" value="java.util.Date"/> </create> <create creator="new" javascript="DWRUserAccess"> <param name="class" value="iwco.dwrs.DWRUserAccess"/> </create> <convert converter="bean" match="iwco.entity.User"/> </allow> </dwr>
5、test.html页面
<!DOCTYPE html> <html> <head> <title>test.html</title> <Meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <Meta http-equiv="description" content="this is my page"> <Meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> <script src="/dwrTest/dwr/engine.js"></script> <script src="/dwrTest/dwr/util.js"></script> <script src="/dwrTest/dwr/interface/DWRUserAccess.js"></script> <SCRIPT LANGUAGE="JavaScript"> function saveFun(data) { if (data) { alert("注册成功!"); } else { alert("登陆ID已经存在!"); } } function OnSave() { var userMap = {}; userMap.id = regForm.id.value; userMap.password = regForm.password.value; userMap.name = regForm.name.value; userMap.email = regForm.email.value; DWRUserAccess.save(userMap,saveFun); } function findFun(data) { if (data == null) { alert("无法找到用户:" + queryForm.id.value); return; } alert("找到用户,nid:" + data.id + ",npassword:" + data.password + ",nname:" + data.name + ",nemail:" + data.email); } function OnFind() { DWRUserAccess.find(queryForm.id.value,findFun); } //--> </SCRIPT> </head> <body> <B>用户注册</B> <br> ------------------------------------------------ <Br> <form name="regForm"> 登陆ID:<input type="text" name="id"><br> 口 令:<input type="password" name="password"><br> 姓 名:<input type="text" name="name"><br> 电子邮件:<input type="text" name="email"><br> <input type="button" name="submitBtn" value="提交" onclick="OnSave()"><br> </form> <br> <br> <B>用户查询</B> <br> ------------------------------------------------ <Br> <form name="queryForm"> 登陆ID:<input type="text" name="id"><br> <input type="button" name="submitBtn" value="提交" onclick="OnFind()"><br> </form> <br> </body> </html>项目组织图:
然后允许项目,打开浏览器进行测试即可。下面说说总结吧,我自己很早之前就用过,感觉挺简单,原理也知道。但前天有个同事问我,说让给配置一个,当时就想整一个出来,后面想想这东西入门教材很多了。但仔细想想这算是知识点了,记录下来以后别人问直接给连接地址就行了。
<script src="/dwrTest/dwr/engine.js"></script> <script src="/dwrTest/dwr/util.js"></script> <script src="/dwrTest/dwr/interface/DWRUserAccess.js"></script>前两个没说的dwr自带js,第三个才是我们自己的,刚接触的人肯定纳闷了,其实理解了就简单,我们在dwr里面有配置:
<create creator="new" javascript="DWRUserAccess"> <param name="class" value="iwco.dwrs.DWRUserAccess"/> </create>,第三个js的文件名就说javascript的值,且是dwr/interface开头的目录。
2、dwr.xml里面的 create 节点,creator可以配置好几种:
new:Java用“new”关键字创造对象
none:它不创建对象 (v1.1+)
scripted:通过BSF使用脚本语言创建对象,例如BeanShell或Groovy
spring:通过Spring框架访问Bean
struts:使用Struts的FormBean (v1.1+)
jsf:使用JSF的Bean (v1.1+)
pageflow:访问Weblogic或Beehive的PageFlow (v1.1+)
ejb3:使用EJB3 session bean (v2.0+)
上面的例子用的是new,现在很多项目都用Spring,所以我给一个spring的:<create creator="spring" javascript="backUpDocumentService"> <param name="beanName" value="testDWR1" /> <include method="modifyBackUpFiles"></include> <include method="isSafeCodeRight"></include> </create> <!-- 加include可以具体指定java类中关的方法,不加则默认类中全部为public的方法 --> <create creator="spring" javascript="testDWR2"> <param name="beanName" value="testDWR2" /> </create>
对应的spring文件里面添加:
<bean id="testDWR1" class="com.test.testDWR1"></bean> <bean id="testDWR2" class="com.test.testDWR2"></bean>当然注解的话自己处理下吧。