DWR简单实例

前端之家收集整理的这篇文章主要介绍了DWR简单实例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1. 首先需要的jar,如果是maven可以添加如下依赖

<dependency>
      <groupId>org.directwebremoting</groupId>
      <artifactId>dwr</artifactId>
      <version>2.0.1</version>
</dependency>

2. web.xml中添加如下配置项
<servlet>
      <servlet-name>dwr-invoker</servlet-name>
      <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
      <init-param>
           <param-name>classes</param-name>
           <param-value>java.lang.Object</param-value>
      </init-param>
      <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>

3. 在 与web.xml平级加入dwr.xml文件文件内容如下
<?xml version="1.0" encoding="UTF-8"?> 
    <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN"     
            "http://www.getahead.ltd.uk/dwr//dwr20.dtd">
    <dwr>
        <allow>
            <create creator="new" javascript="testDwr"><!--创建前端所需的js实体-->
                <param name="class" value="com.dwr.test.TestDwr" />
                <include method="method1" /><!--该实体可以访问的方法,多个就写多个include-->
                <include method="method2" />
            </create>
            <convert converter="bean" match="com.dwr.test.Student">
            </convert>  <!--javaBean对象传输配置转换器-->
        </allow>
    </dwr>

4. 测试的java后台
public class TestDwr {
     public String method1(String name){
         return "your name is "+name;
     }
 
     public Map<String,String> method2(String name){
          Map<String,String> tmp = new HashMap<String,String>();
          tmp.put("name",name);
          return tmp;
     }
 
     public Student method3(String name,int age){
          return new Student(name,age);
     }
}
package com.dwr.test;

public class Student {
	/**
	 * 
	 */
	private String name;
	private int age;
	
	public Student(){
	}
	
	public Student(String name,int age){
		this.name=name;
		this.age=age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
}

5. 前端页面代码
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<Meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<Meta http-equiv="Expires" CONTENT="0"> 
<Meta http-equiv="Cache-Control" CONTENT="no-cache"> 
<Meta http-equiv="Pragma" CONTENT="no-cache"> 
<Meta HTTP-RQUIV="Refresh" CONTENT="2">
<title>test</title>
<script type="text/javascript" src="/testMaven/dwr/engine.js"></script><!--必须添加引用的-->
<script type="text/javascript" src="/testMaven/dwr/util.js"></script><span style="font-family: 微软雅黑;"><!--可选添加引用的--></span>
<script type="text/javascript" src="/testMaven/dwr/interface/testDwr.js"></script><span style="font-family: 微软雅黑;"><!--必须添加引用的当前测试js实体--></span>
<script type="text/javascript">
	function testDwre1(){
		testDwr.method1("jack",function(data){
			alert(data);
		});
	}
	
	function testDwre2(){
		testDwr.method2("jack",function(data){
			alert(data.name);
		});
	}
	
	function testDwre3(){
		testDwr.method3('jack','18',function(ddd){
			alert(ddd.name+"'s age is "+ddd.age);
		});
	}

</script>
</head>
	<body>
		<a id="testDwr1" href="" class="btn4" onclick="testDwre1();">测试DWR1</a>
		<a id="testDwr2" href="" class="btn4" onclick="testDwre2();">测试DWR2</a>
		<a id="testDwr3" href="" class="btn4" onclick="testDwre3();">测试DWR3</a>
	</body>
</html>

猜你在找的Ajax相关文章