package com.hugui.dwr;
public class DWRTest {
public String hello(){
return "你好!";
}
}
package com.hugui.dwr;
public class DWRTest2 {
public String hello(String name, int age){
return "年龄"+age+"名字"+name;
}
}
package com.hugui.dwr;
import java.util.ArrayList;
import java.util.List;
public class DWRTest {
/*
public String[] hello(){
return new String[]{"一","二"};
}
*/
public List hello(){
List list = new ArrayList();
list.add("num1");
list.add("num2");
return list;
}
}
package com.hugui.dwr;
import java.util.HashMap;
import java.util.Map;
public class DWRTest4 {
public Map hello(){
Map map = new HashMap();
map.put("k1", "v1");
map.put("k2", "v2");
return map;
}
}
package com.hugui.dwr;
import java.util.ArrayList;
import java.util.List;
public class DWRTest5 {
public List hello(){
List list = new ArrayList();
list.add(new String[]{"value1", "value2"});
list.add(new String[]{"v1","v2"});
return list;
}
}
package com.hugui.dwr;
public class DWRTest6 {
public boolean hasPermission(String name, int permission){
if("胡贵".equals(name) && permission == 1){
return true;
}
return false;
}
}
package com.hugui.dwr;
public class DWRTest8 {
public String method1(){
return "调用方法1";
}
public String mehtod2(){
return "调用方法2";
}
}
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="test">
<param name="class" value="com.hugui.dwr.DWRTest"/>
</create>
<create creator="new" javascript="test2">
<param name="class" value="com.hugui.dwr.DWRTest2"/>
</create>
<create creator="new" javascript="test3">
<param name="class" value="com.hugui.dwr.DWRTest3"/>
</create>
<create creator="new" javascript="test4">
<param name="class" value="com.hugui.dwr.DWRTest4"/>
</create>
<create creator="new" javascript="test5">
<param name="class" value="com.hugui.dwr.DWRTest5"/>
</create>
<create creator="new" javascript="test6">
<param name="class" value="com.hugui.dwr.DWRTest6"/>
</create>
<create creator="new" javascript="test8">
<param name="class" value="com.hugui.dwr.DWRTest8"/>
</create>
</allow>
</dwr>
web.xml
<!-- dwr -->
<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>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
dwrtest.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!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=GB18030">
<script type="text/javascript" src="dwr/engine.js"></script>
<script type="text/javascript" src="dwr/util.js"></script>
<script type="text/javascript" src="dwr/interface/test.js"></script>
<title>Insert title here</title>
<script type="text/javascript">
/* 方法一: */
/*
function test1(){
test.hello(helloreturn);
}
function helloreturn(value){
alert(value);
}
*/
/*方法二: */
/*
function test1(){
test.hello()(
function (data){
alert(data);
}
);
}
*/
/* 方法三: */
function test1(){
test.hello(helloreturn);
}
var helloreturn = function(value){
alert(value);
}
</script>
</head>
<body>
<input type="button" value="测试" onclick="test1()"/>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!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=GB18030">
<script type="text/javascript" src="dwr/engine.js"></script>
<script type="text/javascript" src="dwr/util.js"></script>
<script type="text/javascript" src="dwr/interface/test2.js"></script>
<title>Insert title here</title>
<script type="text/javascript">
function test2(){
test2.hello("胡贵",21)(
function (data){
alert(data);
}
);
}
</script>
</head>
<body>
<input type="button" value="测试" onclick="test2()"/>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!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=GB18030">
<script type="text/javascript" src="dwr/engine.js"></script>
<script type="text/javascript" src="dwr/util.js"></script>
<script type="text/javascript" src="dwr/interface/test3.js"></script>
<title>Insert title here</title>
<script type="text/javascript">
function test3(){
test3.hello()(
function (datas){
for(int i=0; i<datas.length; i++){
alert(datas[i]);
}
}
);
}
</script>
</head>
<body>
<input type="button" value="测试" onclick="test3()"/>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!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=GB18030">
<script type="text/javascript" src="dwr/engine.js"></script>
<script type="text/javascript" src="dwr/util.js"></script>
<script type="text/javascript" src="dwr/interface/test4.js"></script>
<title>Insert title here</title>
<script type="text/javascript">
function test4(){
test3.hello()(
function (datas){
alert(datas.k1);
alert(datas["k1"]);
}
);
}
function test4_1(){
test3.hello()(
function (datas){
for(var i in datas){
alert("key="+i+"value"+datas[i]);
}
}
);
}
</script>
</head>
<body>
<input type="button" value="测试" onclick="test4()"/>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!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=GB18030">
<script type="text/javascript" src="dwr/engine.js"></script>
<script type="text/javascript" src="dwr/util.js"></script>
<script type="text/javascript" src="dwr/interface/test5.js"></script>
<title>Insert title here</title>
<script type="text/javascript">
function test5(){
test5.hello()(
function (datas){
for(int i=0; i<datas.length; i++){
for(int j=0; j<datas[i].length; j++){
alert(datas[i][j]);
}
}
}
);
}
</script>
</head>
<body>
<input type="button" value="测试" onclick="test5()"/>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!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=GB18030">
<script type="text/javascript" src="dwr/engine.js"></script>
<script type="text/javascript" src="dwr/util.js"></script>
<script type="text/javascript" src="dwr/interface/test6.js"></script>
<title>Insert title here</title>
<script type="text/javascript">
function test6(){
test6.hasPermission(
$("name").value,
$("permission").value,
function(datas){
alert(datas);
}
);
}
</script>
</head>
<body>
name: <input type="text" name="name"> <br>
permission: <input type="text" name="permission"><br>
<input type="button" value="测试是否有权限" onclick="test6()"/>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!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=GB18030">
<script type="text/javascript" src="dwr/engine.js"></script>
<script type="text/javascript" src="dwr/util.js"></script>
<script type="text/javascript" src="dwr/interface/test8.js"></script>
<title>Insert title here</title>
<script type="text/javascript">
function test8(){
/**设置同步编程模型:
如果为true的话,调用test6的时候,将会先执行mehotd2,
为false的时候,将会先执行method1
**/
dwr.engine.setAsync(false);
test8.method1(
function(data){
alert(data);
}
);
test8.method2(
function(data){
alert(data);
}
);
}
</script>
</head>
<body>
<input type="button" value="测试" onclick="test8()"/>
</body>
</html>