在本人的这篇文章的基础上http://chenzheng8975.iteye.com/blog/1842080深入学习:
dwr工具类:<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/util.js"></script>
dwr转化对象和异常:
<convert converter="bean" match="com.cz.model.User"/>
<convert converter="bean" match="com.cz.model.Group"/>
<convert match="java.lang.Exception" converter="exception"/>
<convert converter="bean" match="java.lang.StackTraceElement"/>
MyDwr.java:
package com.cz.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; } }
MyException.java:
package com.cz.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 } }
*************************************************************** MyDwr.load(loadUser); function loadUser(user) { alert(user.id+","+user.username+","+user.group.name); } ************************************************************* MyDwr.list(listUser); function listUser(users) { for(var i=0;i<users.length;i++) { alert(users[i].username+","+users[i].group.name); } } ****************************************************** var user = {id:1,username:"李四",group:{id:2,name:"网络中心"}}; MyDwr.add(user); ****************************************************** 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) { } ********************************************************
<%@ 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>
<%@ 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>