1 dwr.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://getahead.org/dwr/dwr20.dtd">
<dwr>
<allow>
<create javascript="DWRUser" creator="spring">
<param name="beanName" value="userBiz"></param>
<include method="loadById"/><!--需要对外暴露的方法-->
</create>
</allow>
</dwr>
creator属性的值可以是new,struts,spring...... spring类型允许你通过spring来创建实例.因为此处是整合spring来做的,所以设置成“spring”,javascript="DWRUser" 表示实例转换成javascript语言后以DWRUser来命名,前台页面可以通过代码(<script type='text/javascript' src='../../dwr/interface/DWRUser.js'></script>)来调用。param元素的name属性值可以是class,beanName等,此处用beanName,value的值是定义在applicationContext.xml中某个bean的id值。
<bean id="userBiz" class="com.shaccp.biz.UserBizImpl">
<property name="userDAO" ref="userDAO"></property>
</bean>
2 实体类 userDAO是spring注入进来
public class UserBizImpl implements UserBiz {
private UserDAO userDAO;
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
public List getByPage(String name, int pageNo, int pageSize) {
return userDAO.findByPage(name, pageNo, pageSize);
}
public void add(User u) {
userDAO.add(u);
}
public User loadById(int id) {
return userDAO.getUserById(id);
}
public void update(User user) {
userDAO.update(user);
}
public void delete(int uid) {
userDAO.delete(uid);
}
}
3 在视图页面中调用
先导入javascript,每个文件的功能这里不再详述
<script type='text/javascript' src='/userssh/dwr/interface/DWRUser.js'></script>
<script type='text/javascript' src='/userssh/dwr/engine.js'></script>
<script type='text/javascript' src='/userssh/dwr/util.js'></script><script type='text/javascript'>
function regUserChked(){
var username=$("username").value; DWRUser.isExist(username,showData);//showData回调函数
}
function showData(data){
if(data){
DWRUtil.setValue("userMsg","该用户名已经被注册,请更换您的用户名!");
}else{
DWRUtil.setValue("userMsg","恭喜您,该用户名尚未被注册!");
}
}
</script> <input type="text" name="username" value="" οnblur="regUserChked()" /><div id="userMsg"></div>