首先使用Maven引入DWR的Jar包
org.directwebremoting
dwr
3.0.1-RELEASE
其次建立一个测试用的Java类
package com.k.ctc.web.dwr;
/**
* @author Kai
* @Date 2016-1-7 14:32:08
*/
public class TestDwr {
public String getName(String name) {
System.out.println("Call GetName Method!");
return "Hello:" + name;
}
}
然后配置dwr.xml
dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN" "http://directwebremoting.org/schema/dwr30.dtd">
配置web.xml
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
dwr-invoker
org.directwebremoting.servlet.DwrServlet
debug
true
crossDomainSessionSecurity
true
scriptSessionTimeout
1800000
maxCallCount
20
config-xml
/WEB-INF/conf/dwr.xml
dwr-invoker
/dwr/*
最后在HTML页面中使用
html>
Dwr Test Demovar reply = function (data) {
document.getElementById("result").innerText = data;
};
function btn_click() {
var name = document.getElementById("name").value;
test.getName(name, reply);
}
注意:js的路径需要引用正确,附带说明截图
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓写好后的测试页面 ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ DWR生成的模块列表页面↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ 模块详情页面和需要引用的js路径说明等↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓