1.软件版本
Spring-Version: 2.0.7 http://www.springframework.org/
DWR-version: 2.0.2 http://getahead.org/dwr
IDE: Eclipse 3.2 + MyEclipse 6.0
2.新建工程:dwrsping
加入对spring的支持,并把dwr的jar包dwr.jar复制到WEB-INF\lib下
需要的jar包为:
3.编辑web.xml加入对spring和dwr的支持
<!-- DWR servlet 配置 -->
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>
org.directwebremoting.spring.DwrSpringServlet
</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>
<!-- 设置Spring监听器 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
说明:
org.directwebremoting.spring.DwrSpringServlet这个类是dwr专门为整合spring提供的一个servlet,加入这个之后,dwr的配置就可以写入到spring的配置文件applicationContext.xml中,省掉了dwr.xml
4.编辑jsp页面 index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>DWR+SPRING</title>
<script type="text/javascript" src="dwr/engine.js"></script>
<script type="text/javascript" src="dwr/util.js"></script>
<script type="text/javascript" src="dwr/interface/UserAjaxHelper.js"></script>
<script language="javascript">
function checkUser(widget){
var username = widget.value;
//将username传入到checkUsername方法,返回后调用showUsermsg的js方法
UserAjaxHelper.checkUsername(username,showUsermsg);
}
//showUsermsg的方法定义,data为checkUsername的返回值
var showUsermsg = function(data){
if(data){
DWRUtil.setValue("usermsg","<font color='red'>此用户名已被使用</font>", { escapeHtml:false });
}
else{
DWRUtil.setValue("usermsg","<font color='green'>此用户名可以使用</font>", { escapeHtml:false });
}
}
</script>
</head>
<body onload='dwr.util.useLoadingMessage()'>
<form action="#">
<table border="0">
<tr><td>用户名:</td><td><input type="text" name="username" id="username" value="" onkeyup="checkUser(this);"/><span id="usermsg"></span></td></tr>
<tr><td>密码:</td><td><input type="password" name="userpwd" id="userpwd" value=""></td></tr>
<tr><td><input type="submit" value="注册"/></td><td><input type="reset" value="清空"/></td></tr>
</table>
</form>
</body>
</html>
说明:
<script type="text/javascript" src="dwr/engine.js"></script>
<script type="text/javascript" src="dwr/util.js"></script>
这两个是dwr默认要调用的js文件,必须写到文件里
UserAjaxHelper.js就是通过applicationContext.xml文件配置的AJAX操作类
调用的写法为:
UserAjaxHelper.checkUsername(username,showUsermsg);
var showUsermsg = function(data){......}
有时写成:
UserAjaxHelper.checkUsername(username,showUsermsg());
function showUsermsg (data){......}
这样就会出问题,showUsermsg就会得不到返回值data,不知道为什么
5. 编辑spring的配置文件applicationContext.xml,加入对dwr的配置
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-2.0.xsd">
<!-- DWR 配置开始 -->
<dwr:configuration></dwr:configuration><!-- 必须要configuration -->
<dwr:controller id="dwrController" debug="true" />
<!-- DWR 配置结束 -->
<!-- spring bean配置 -->
<bean id="userService" class="test.bcndyl.service.impl.UserServiceImpl">
</bean>
<bean id="userAjaxHelper" class="test.bcndyl.dwr.UserAjaxHelper">
<property name="userService" ref="userService" />
<!-- 定义调用的js文件 -->
<dwr:remote javascript="UserAjaxHelper"></dwr:remote>
</bean>
<!-- end spring bean配置 -->
</beans>
说明:
要加入dwr的定义
xmlns:dwr=http://www.directwebremoting.org/schema/spring-dwr
还有http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-2.0.xsd
所有dwr的定义要加dwr的前缀,由于只是传入string类型的username,所以就没有配置convert
6.dwr调用的操作类UserAjaxHelper
package test.bcndyl.dwr;
import test.bcndyl.service.IUserService;
public class UserAjaxHelper {
private IUserService userService;
public IUserService getUserService() {
return userService;
}
public void setUserService(IUserService userService) {
this.userService = userService;
}
public boolean checkUsername(String username){
boolean returnVal = this.getUserService().isUserExist(username);
return returnVal;
}
}
用户操作的service接口IUserService
package test.bcndyl.service;
public interface IUserService {
//用户是否存在
public boolean isUserExist(String username);
}
用户操作的service接口IUserService的实现类UserServiceImpl
package test.bcndyl.service.impl;
import test.bcndyl.service.IUserService;
public class UserServiceImpl implements IUserService {
@Override
public boolean isUserExist(String username) {
/*
* 添加访问数据库代码
* 或者调用Hibernate方法
* 返回用户是否存在
* true--用户存在,false-用户不存在
*/
//测试代码,检测用户名是否为hello,是hello返回true,不是hello返回false
if("hello".equals(username))
return true;
return false;
}
}
7.检测dwr是否正确工作的方法,可以在浏览器地址栏输入http://localhost:8080/appName/dwr
可以看到可供dwr调用的方法
8. 此例子程序运行结果