DWR的安装使用和与spring的整合

标签: dwr spring | 发表时间:2013-03-12 11:42 | 作者:devilzy2656
出处:http://blog.csdn.net


1.安装配置

1.1拷贝dwr文件包到lib目录下

1.2编辑web.xml文件,加入如下代码:

  <listener>
    <listener-class>org.directwebremoting.servlet.DwrListener</listener-class>
  </listener>
  
  <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>
  
    <load-on-startup>1</load-on-startup>
  </servlet>
   
  <servlet-mapping>
    <servlet-name>dwr-invoker</servlet-name>
    <url-pattern>/dwr/*</url-pattern>
  </servlet-mapping>	

1.3拷贝dwr.xml文件到web.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>

    <!--
    <filter class="com.example.dwr.monitor.MonitoringAjaxFilter"/>
    <filter class="org.directwebremoting.filter.ExtraLatencyAjaxFilter">
      <param name="delay" value="200"/>
    </filter>
    -->

    <!-- intro - for the test on index.html -->
    <create creator="new">
      <param name="class" value="org.konghao.dwr.model.MyDwr"/>
    </create>
    <convert converter="bean" match="org.konghao.dwr.model.User"/>
    <convert converter="bean" match="org.konghao.dwr.model.Group"/>
    
    <convert match="java.lang.Exception" converter="exception"/>
    <convert converter="bean" match="java.lang.StackTraceElement"/>


  </allow>

</dwr>

1.4在需要使用的页面中引入js文件(第二个是自己定义的java文件的名字)

<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/engine.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/interface/MyDwr.js"></script>

2.dwr的应用(以下几个类是下面的一些操作需要用到的测试类,可以自行修改)

MyDwr.java

package org.konghao.dwr.model;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;

public class MyDwr {
	public String hello(String world) {
		System.out.println("hello "+world);
		return "hello "+world;
	}
	
	public User load() {
		User u = new User(1,"张三",new Group(1,"财务处"));
		return u;
	}
	
	public List<User> list() {
		List<User> users = new ArrayList<User>();
		users.add(new User(1,"张三",new Group(1,"财务处")));
		users.add(new User(2,"李四",new Group(2,"科技处")));
		users.add(new User(3,"王五",new Group(3,"宣传部")));
		return users;
	}
	
	public void add(User user) {
		System.out.println(user);
	}

	public void deleteUser() {
		throw new MyException("在删除用户的时候有错");
	}
	
	public int add(int a,int b) {
		return a+b;
	}
	
	public String upload(InputStream is,String filename) throws IOException {
		//WebContext可以获取HttpServlet的对象
		WebContext wc = WebContextFactory.get();
		HttpServletRequest req = wc.getHttpServletRequest();
		String realpath = req.getSession().getServletContext().getRealPath("upload");
		String fn = FilenameUtils.getName(filename);
		String filepath = realpath+"/"+fn;
		FileUtils.copyInputStreamToFile(is, new File(filepath));
		return filepath;
	}
}
User.java

package org.konghao.dwr.model;

public class User {
	private int id;
	private String username;
	private Group group;
	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 Group getGroup() {
		return group;
	}
	public void setGroup(Group group) {
		this.group = group;
	}
	public User(int id, String username, Group group) {
		super();
		this.id = id;
		this.username = username;
		this.group = group;
	}
	public User() {
		super();
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", group=" + group
				+ "]";
	}
}


Group.java

package org.konghao.dwr.model;

public class Group {
	private int id;
	private String name;
	
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Group(int id, String name) {
		super();
		this.id = id;
		this.name = name;
	}
	public Group() {
		super();
	}
	@Override
	public String toString() {
		return "Group [id=" + id + ", name=" + name + "]";
	}
	
	
}


MyException.java

package org.konghao.dwr.model;

public class MyException extends RuntimeException {

	public MyException() {
		super();
		// TODO Auto-generated constructor stub
	}

	public MyException(String message, Throwable cause) {
		super(message, cause);
		// TODO Auto-generated constructor stub
	}

	public MyException(String message) {
		super(message);
		// TODO Auto-generated constructor stub
	}

	public MyException(Throwable cause) {
		super(cause);
		// TODO Auto-generated constructor stub
	}

}

3.dwr的初步使用:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/engine.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/interface/MyDwr.js"></script>
<script type="text/javascript">
	MyDwr.hello("世界",function(data){
		alert(data);
	});
</script>
</head>
<body>

</body>
</html>

4.dwr调用方法,并使用对象转换功能,进行对象的属性访问

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/engine.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/interface/MyDwr.js"></script>
<script type="text/javascript">
	MyDwr.load(loadUser);
	function loadUser(user) {
		alert(user.id+","+user.username+","+user.group.name);
	}
</script>
</head>
<body>

</body>
</html>

5.dwr调用list方法,并循环输出对应的属性

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/engine.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/interface/MyDwr.js"></script>
<script type="text/javascript">
	MyDwr.list(listUser);
	function listUser(users) {
		for(var i=0;i<users.length;i++) {
			alert(users[i].username+","+users[i].group.name);
		}
	}
</script>
</head>
<body>

</body>
</html>

6.dwr使用json数据格式进行对象的添加操作

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/engine.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/interface/MyDwr.js"></script>
<script type="text/javascript">
	var user = {id:1,username:"李四",group:{id:2,name:"网络中心"}};
	MyDwr.add(user);
</script>
</head>
<body>

</body>
</html>

7.dwr中异常的转换后,进行处理的操作(可以具体查看dwr官方文档中的exception处理)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/engine.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/util.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/interface/MyDwr.js"></script>
<script type="text/javascript">
	MyDwr.deleteUser({
		callback:deleteUser,
		errorHandler:function(msg,exception) {
			alert(msg);
			/*for(var ea in exception) {
				alert(ea);
			}
			alert(exception.stackTrace);*/
			alert(dwr.util.toDescriptiveString(exception,1));
		}
	});
	function deleteUser(data) {
	}
</script>
</head>
<body>

</body>
</html>

8.dwr的无刷新操作,添加选项操作,添加表格数据

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/engine.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/util.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/interface/MyDwr.js"></script>
<script type="text/javascript">
function calAdd() {
	var a = dwr.util.getValue("a");
	var b = dwr.util.getValue("b");
	MyDwr.add(parseInt(a),parseInt(b),function(data){
		alert(data);
	});
}

function addAddress() {
	var a = dwr.util.getValue("addressName");
	var data = [{id:a,name:a}];
	dwr.util.addOptions("address",data,"id","name");
}
function initAddress() {
	dwr.util.removeAllOptions();
	var data = [{id:1,name:"北京"},{id:2,name:"天津"},{id:3,name:"上海"}];
	dwr.util.addOptions("address",data,"id","name");
}

function initUser() {
	MyDwr.list(function(data){
		dwr.util.addRows("user",data,cellFuncs,{ escapeHtml:false });
	});
}
var cellFuncs=[
	function(data){return data.id},
	function(data){return data.username},
	function(data){return data.group.name}
];
</script>
</head>
<body>
	<input type="text" id="a"/>+<input type="text" id="b"/><input type="button" value="获取" onclick="calAdd()">
	<br/>
	<select id="address">
	
	</select>
	<input type="button" value="初始化地址" onclick="initAddress()"/>
	<input type="text" id="addressName"/><input type="button" value="添加" onclick="addAddress()"/>
	<table width="600" border="1">
		<thead>
			<tr>
				<td>ID</td>
				<td>username</td>
				<td>groupName</td>
			</tr>
		</thead>
		<tbody id="user">
		</tbody>
	</table>
	
	<input type="button" value="初始化用户" onclick="initUser()"/>
</body>
</html>

9.dwr实现的无刷新文件上传功能

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/engine.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/util.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/interface/MyDwr.js"></script>
<script type="text/javascript">
function upload() {
	var file = dwr.util.getValue("myfile");
	alert(file.value);
	alert(file);
	MyDwr.upload(file,file.value,function(data){
		alert(data);
	});
}
</script>
</head>
<body>
	<input type="file" id="myfile"/>	
	<input type="button" value="上传文件" onclick="upload()"/>
</body>
</html>

10.dwr与spring的整合:



作者:devilzy2656 发表于2013-3-12 11:42:05 原文链接
阅读:0 评论:0 查看评论

相关 [dwr spring] 推荐:

DWR的安装使用和与spring的整合

- - CSDN博客架构设计推荐文章
1.1拷贝dwr文件包到lib目录下. 1.2编辑web.xml文件,加入如下代码:. 1.3拷贝dwr.xml文件到web.xml文件的目录下:. 1.4在需要使用的页面中引入js文件(第二个是自己定义的java文件的名字). 2.dwr的应用(以下几个类是下面的一些操作需要用到的测试类,可以自行修改).

DWR实现无刷新上传

- - CSDN博客推荐文章
这里需要用到commons-fileupload-1.2.2和commons-io-2.3. . .

Spring详解

- - CSDN博客架构设计推荐文章
Spring是一个开源的控制反转(Inversion of Control ,IoC)和面向切面(AOP)的容器框架.它的主要目的是简化企业开发.. PersonDaoBean 是在应用内部创建及维护的. 所谓控制反转就是应用本身不负责依赖对象的创建及维护,依赖对象的创建及维护是由外部容器负责的.

Spring定时

- - 行业应用 - ITeye博客
spring的定时任务配置分为三个步骤:. . . . . .

简单Spring+hessian

- - Web前端 - ITeye博客
简单的Spring+hessian. dist\modules里面的 spring-webmvc.jar . lib\caucho 里面的hessian-3.1.3.jar. 里面有个接口interface:. 建立一个model层:(实现Serializable接口). 在WEB-INF下面创建一个remoting-servlet.xml:.

Spring MVC 和 Struts2

- - CSDN博客架构设计推荐文章
Web层面的框架学习了三个Struts1和2,SpringMVC,那他们之间肯定存在一个优劣和适用的环境,Struts1和2的异同点我已经做过对比《 Struts1和Struts2》,这篇将对比下Struts2和SpringMVC的异同,下面数据基本来源于网络,本人是搜集整理所得,供大家参考. 一个项目使用什么样的技术,决定的因素很多,我所能想到的有:对系统的性能、开发的效率、团队学习的成本、业务场景等,下面尽量从这几个方面入手,来分析比较下他们之间存在的优劣.

Spring AOP详解

- - Java - 编程语言 - ITeye博客
        最近项目中遇到了以下几点需求,仔细思考之后,觉得采用AOP来解决. 一方面是为了以更加灵活的方式来解决问题,另一方面是借此机会深入学习Spring AOP相关的内容. 例如,以下需求不用AOP肯定也能解决,至于是否牵强附会,仁者见仁智者见智. 1.对部分函数的调用进行日志记录,用于观察特定问题在运行过程中的函数调用情况.

spring roo 入门

- - 企业架构 - ITeye博客
Spring官网下载STS(如果没有STS). 创建Spring Roo基础项目. 根 据ROO的提示输入jpa setup再按ctrl+space,很遗憾这个快捷键已经被输入法切换占用,不能借助提示输入命令,但我们可以打开ROO命令向导,这里我们输入jpa 可以查到这条命令的用法,根据提示增加provider和database选项来完成命令.

Spring Rmi配置

- - 企业架构 - ITeye博客
现在远程调用一般用RPC,webservice或者Rmi,而目前用的比较多的是webservice和Rmi. webservice和rmi的最主要的区别,rmi的客户端和服务端都必须是java,webservice没有这个限制,webservice是在http协议上传递xml文本文件. 与语言和平台无关,rmi是在tcp协议上传递可序列化的java对象,只能用在java虚拟机上,绑定语言.

Spring+memcached整合

- - 行业应用 - ITeye博客
1)  下载memcached服务端memcached-1.2.6-win32-bin.zip,地址:http:. 2)  下载java版客户端 java_memcached-release_2.6.1.zip. 3)  解压缩memcached-1.2.6-win32-bin.zip到指定目录,例如:D:\memcached-1.2.6-win32 ,.