Simple DWR instance and dwr instance

Source: Internet
Author: User

Simple DWR instance and dwr instance

1. First, add the following dependencies if maven is used.

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

2. Add the following configuration items to 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. Add the dwr. xml file at the same level as web. xml. The file content is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE dwr PUBLIC "-// GetAhead Limited // DTD Direct Web Remoting 2.0 //" http://www.getahead.ltd.uk/dwr//dwr20.dtd "> <dwr> <allow> <create creator =" new "javascript =" testDwr "> <! -- Create the js entity required for the front-end --> <param name = "class" value = "com. dwr. test. TestDwr"/> <include method = "method1"/> <! -- Methods that can be accessed by the object, write multiple include --> <include method = "method2"/> </create> <convert converter = "bean" match = "com. dwr. test. student "> </convert> <! -- JavaBean object transmission configuration converter --> </allow> </dwr>

4. Tested java background class
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. Front-end Page code
<Html xmlns = "http://www.w3.org/1999/xhtml"> 



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.