使用DWR的一个小程序

一,将两个jar包加到lib下dwr.jar,commons-logging-1.0.4.jar

二,修改web.xml,添加DWR的核心Servlet
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	
  <servlet>
  	<servlet-name>dwr-invoker</servlet-name>
  	<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
  	<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>
	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 此处的参数debug在开发阶段设为true,开发完成时要记得设为false,要不不安全。

三,编写处理类
package com.lbx.dwr.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;

import com.lbx.dwr.bean.Person;

public class MyBean {
	
	public void test1(){
		System.out.println("MyBean.test1()");
	}
	
	public String test2(){
		System.out.println("MyBean.test2()");
		return "libinxuan";
	}
	
	public String test3(int i, String str){
		return "nihao";
	}
	
	//以JavaBean作为参数
	public String sendObj(Person p){
		return p.getUsername();
	}
	
	//返回JavaBean实例的方法
	public Person getBean(String name){
		return new Person(8,"s","123456");
	}
	
	//返回集合对象
	public List<Person> getPersonList(){
		List<Person> persons = new ArrayList<Person>();
		persons.add(new Person(2,"chy","123456"));
		persons.add(new Person(3,"hhh","123456"));
		
		return persons;
	}
	
	//返回一个数组对象
	public Person[] getPersonArray(){
		Person[] result = new Person[3];
		
		result[0] = new Person(4,"d","d");
		result[1] = new Person(6,"ds","sd");
		result[2] = new Person(7,"dd","dd");
		
		return result;
	}
	
	//返回一个Map对象
	public Map<String, Person> getPersonMap(){
		Map<String, Person> result = new HashMap<String, Person>();
		result.put("first", new Person(1,"lbx","123456"));
		result.put("second", new Person(2,"chy","123456"));
		
		return result;
	}
	
	//远程方法的参数是集合
	public String sendList(List<Person> personlist){
		String result = "";
		
		for(Person p : personlist){
			result += p.getId() + "<br>";
		}
		
		return result;
	}
	
	//远程方法的参数是不是泛型的集合
	public String sendListNoGrneric(List personlist){
		String result = "";
		
		for(Object p : personlist){
			result += ((Person)p).getUsername() + "<br>";
		}
		
		return result;
	}
	
	//远程方法的参数是集合
	public String sendMap(Map<String , Person> pmap)
	{
		String result = "";
		for (String key : pmap.keySet())
		{
			result += "键" + key + " 其值为:" +
				pmap.get(key).getUsername() + "<br />";
		}
		return result;
	}
	
	//如何跟servlet的作用域(session,request,ServletContext)交互:
	public String test7(){
		WebContext wc = WebContextFactory.get();
//		wc.getServletContext();
//		wc.getHttpServletRequest();
		
		HttpSession session = wc.getSession();
		return session.getAttribute("a").toString();
	}
	
}
 相应的Model(Person)
package com.lbx.dwr.bean;

public class Person {
	
	private int id;
	private String username;
	private String password;
	
	public Person() {
		
	}

	public Person(int id, String username, String password) {
		super();
		this.id = id;
		this.username = username;
		this.password = password;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
}
 四,配置dwr.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN" "http://getahead.org/dwr/dwr30.dtd">

<dwr>
 
  <allow>

    <create creator="new" javascript="Demo">
      <param name="class" value="com.lbx.dwr.test.MyBean"/>
    </create>
    
    <convert match="com.lbx.dwr.bean.Person" converter="bean"></convert> <!--  javabean的转化 -->


  </allow>
  
  <signatures>
	  <![CDATA[
		import java.util.List;
		import com.lbx.dwr.test.MyBean;
		import com.lbx.dwr.bean.Person;
		Demo.sendListNoGrneric(List<Person>);
	 ]]>
	</signatures>

</dwr>
 五,客户端使用DWR
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">

		<title>My JSP 'index.jsp' starting page</title>
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
		<!-- 导入DWR为Demo对象动态生成的JavaScript代码库 -->
		<script type='text/javascript' src='dwr/interface/Demo.js'></script>
		<!-- 导入DWR引擎的核心JavaScript代码库 -->
		<script type='text/javascript' src='dwr/engine.js'></script>
		<!-- DWR的工具JavaScript文件,通过这工具的工具方法可以简化DOM操作 -->
		<script type='text/javascript' src='dwr/util.js'></script>
		<script><!--
		function testdwr(){
		
			Demo.test1();  //调用无返回值的java方法
			
			/*Demo.test2(function(result){
				alert(result);
			});*/
			
			
			/*Demo.test3(55,"dd",function(result){
				$("div1").innerHTML=result;
			});*/
			
			//以JavaBean作为参数的方法
			/*var person = {id:3,username:'fff',password:'123'};
			Demo.sendObj(person,function(p){
				alert(p);
			});*/
			
			//返回JavaBean实例的方法
			/*Demo.getBean("lbx",function(data){
				alert(data.username);
			});*/
			
			//返回集合对象
			/*Demo.getPersonList(function(data){
				var result='';
				//遍历每个数组元素
				for (var i = 0 ; i < data.length ; i ++)
				{
					result += data[i].username + "<br />";
				}
				document.getElementById("div1").innerHTML = result;
			});*/
			
			//返回一个数组对象
			/*Demo.getPersonArray(function(data){
				var result='';
				//遍历每个数组元素
				for (var i = 0 ; i < data.length ; i ++)
				{
					result += data[i].username + "<br />";
				}
				document.getElementById("div1").innerHTML = result;
			});*/
			
			//返回一个Map对象
			/*Demo.getPersonMap(function(data){
				var result='';
				for (var key in data)
				{
					result += "键为" + key + ",其值为:" + data[key].username + "<br />";
				}
				document.getElementById("div1").innerHTML = result;
			});*/
			
			//远程方法的参数是集合,客户端只要提供一个javaScript数组就可以了
			/*Demo.sendList([
							{id:1,username:"客户端aaa",password:"123456"},
							{id:2,username:"客户端bbb",password:"123456"},
							{id:3,username:"客户端ccc",password:"123456"}
						],function(data){
				document.getElementById("div1").innerHTML = data;
			});*/
			
			//远程方法的参数是不是泛型的集合(不是泛型就要在web.xml中修改东西)
			/*Demo.sendListNoGrneric([
							{id:1,username:"客户端aaa",password:"123456"},
							{id:2,username:"客户端bbb",password:"123456"},
							{id:3,username:"客户端ccc",password:"123456"}
						],function(data){
				document.getElementById("div1").innerHTML = data;
			});*/
			
			//远程方法的参数是集合
			/*Demo.sendMap({
					first:{id:1,username:"客户端aaa",password:"123456"},
					second:{id:2,username:"客户端bbb",password:"123456"},
					third:id:3,username:"客户端ccc",password:"123456"}
					},function(data){
					document.getElementById("div1").innerHTML = data;
			});*/
			/*
			 * 当传参是java对象的时候,先弄一个JavaScript对象,不过JavaScript对象的属性必须和Java对象保持一致
			*/
			/*
			var p1 = {id:5,username:'sss',password:'123456'};  
			Demo.test4(p1,function(p2){
				$("div1").innerHTML=p2.username;
			});*/
			
			
		}
	--></script>
	</head>

	<body>
		<div id="div1"></div>
		<a href="javascript:void(0);" onclick="testdwr();">测试dwr</a>
	</body>
</html>
 

你可能感兴趣的:(JavaScript,bean,servlet,javaee,DWR)