DWR簡單一實例,dwr執行個體

來源:互聯網
上載者:User

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>



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.