一下分别是dwr的四个文件内容
1.jsp页面内容
<html> <head> <title>First DWR</title><!-- 这里的interface是固定的,DWRDemo是java类地名字,原理处会详细说--> <script type="text/javascript" src="dwr/interface/DWRDemo.js"></script> <script type="text/javascript" src="dwr/engine.js"></script> <script type="text/javascript"> window.onload=init; function init(){ //这个是类.方法(实参,回调函数) DWRDemo.say("Coder",callBackHandler); } //回调函数的写法,date为java方法返回来的值,可以起任何名字 var callBackHandler=function(date){ alert(date); }; </script> </head></html>
2.web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"><!-- dwr servlet--> <!-- The DWR servlet. --> <servlet> <servlet-name>dwr-invoker</servlet-name> <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class> <init-param> <param-name>debug</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>crossDomainSessionSecurity</param-name> <param-value>false</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dwr-invoker</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping> <!-- Default page to load in context. --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>
3 后端的java
package ml;public class DWRDemo { public String say(String name){ return name+"测试成功"; }}
以及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 creator="new" javascript="MathDelegate"> <param name="class" value="app.MathDelegate" /> </create> </allow></dwr>
来源:https://www.cnblogs.com/lianzi/archive/2011/07/30/2122181.html