1.首先,在网站上下载相关的jar包,dwr.jar,把jar包放在工程目录lib下
2.写dao,service
dao中方法:
/* * 根据bsoid查询加盟店信息 */ public JfFranInfo findByBsoid(Long bsoid){ return (JfFranInfo)getHibernateTemplate().get(JfFranInfo.class,bsoid ); }
service中的方法:
/* * DWR使用,根据bsoid查询查询当前用户的余额 */ public double findInfoByBsoid(long bsoid){ JfFranInfo franinfo= jfFranInfoDAO.findByBsoid(bsoid); double balance=franinfo.getFranBalance(); return balance; }
3.就是dwr的配置文件了,注意名字叫dwr.xml放在WEB-INF目录下
<?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 creator="spring" javascript="PaymentManagerService" > <param name="beanName" value="PaymentManagerService"/> <include method="findInfoByBsoid"/> </create> </allow></dwr>
4.JSP中应该引入
<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/interface/PaymentManagerService.js"></script><script type="text/javascript" src="<%=request.getContextPath()%>/dwr/engine.js"></script><script type="text/javascript" src="<%=request.getContextPath()%>/dwr/util.js"></script>5.要执行操作的有事件触发
js中的写法:
function checkBalance(){ var bsoid=$('bsoid').value; PaymentManagerService.findInfoByBsoid(bsoid,callback);}function callback(msg){ DWRUtil.setValue('balance',msg);}
页面中加入JS的引用
<script type="text/javascript" src="<%=request.getContextPath()%>/script/findBalance.js"></script>在页面中触发事件的位置加上οnclick="checkBalance()"。就OK了
本人表达能力十分差劲,如果表述不明白请原谅