首页 > 代码库 > 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=http://www.mamicode.com/"com.dwr.test.TestDwr" />>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=http://www.mamicode.com/"/testMaven/dwr/engine.js"></script>>
DWR简单实例
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。