一、导入dwr包【dwr.jar】并在web.xml中添加如下代码
二、编写service类,如下
三、在WEB-INF目录下创建dwr.xml文件,如下
四、编写页面脚本测试【dwr.jsp】,如下
最后访问【dwr.jsp】即可测试!
[url=http://coconut-zhang.iteye.com/blog/1447787]dwr反推技术[/url]
<listener>
<listener-class>org.directwebremoting.servlet.DwrListener</listener-class>
</listener>
<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>
二、编写service类,如下
package cn.com.songjy.service;
import org.springframework.stereotype.Service;
@Service("dwrService")
public class DwrService {
public String hello(String name){
return "你好," + name;
}
public String hello_1(String name){
return "Hello," + name;
}
public String getName(String name){
return name;
}
}
三、在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>
<!-- DwrServiceJs是页面引用时要用到的名称 -->
<create creator="spring" javascript="DwrServiceJs">
<!-- dwrService是spring注入cn.com.songjy.service.DwrService类时名称 -->
<param name="beanName" value="dwrService" />
<!-- 对外公布方法列表,当须全部公布时,则无需配置include -->
<include method="hello"/>
<include method="hello_1"/>
</create>
</allow>
</dwr>
四、编写页面脚本测试【dwr.jsp】,如下
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/engine.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/interface/DwrServiceJs.js"></script>
</head>
<script type="text/javascript">
DwrServiceJs.hello("张三", function(data){
alert(data);
});
</script>
<body>
</body>
</html>
最后访问【dwr.jsp】即可测试!
[url=http://coconut-zhang.iteye.com/blog/1447787]dwr反推技术[/url]