1. 下载jar包:dwr.jar
2.WEB-INF下的web.xml文件:
<servlet>
<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>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
3.被操作的类的方法:
@Service
public class ArticleService extends BaseService<Article> implements IArticleService {
public String withDrawEssay(int id){
//doSomething...
return "ok";
}
}
4.WEB-INF下dwr.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN" "http://getahead.org/dwr/dwr30.dtd">
<dwr>
<allow>
<create creator="spring" javascript="atcService">
<param name="beanName" value="articleService"/>
<include method="withDrawEssay"/>
</create>
</allow>
</dwr>
<script src="${ctx}/dwr/engine.js"></script> //固定
<script src="${ctx}/dwr/util.js"></script> //固定
<script src="${ctx}/dwr/interface/atcService.js"></script> //dwr.xml中的 javascript="atcService"
6.js代码:
$(function(){
var id = ${article.articleID};
$(".withDraw").click(function(){
atcService.withDrawEssay(id,aaa);//aaa是回调函数
});
});
function aaa(data){//回调函数
if(data){
alert("成功!");
}else{
alert("失败!");
}
}