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>
具体调用代码略