dwr的util的使用

来源:互联网 发布:软件开发标准规范 编辑:程序博客网 时间:2024/05/22 07:05

使用dwr.util可以非常方便的添加select的option和获取id的值,添加table的row等处理

1.首先需要在页面引入util.js文件

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

2.以一个完整的页面实现上述两个功能

<%@ 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>

3.相关联的几个类

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();}@Overridepublic 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();}@Overridepublic String toString() {return "Group [id=" + id + ", name=" + name + "]";}}


0 0