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">