浏览 1339 次
锁定老帖子 主题:dwr 的简单使用
该帖已经被评为新手帖
|
|
---|---|
作者 | 正文 |
发表时间:2008-07-18
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>dwr</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</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping> </web-app> 创建一个名叫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> <convert match="com.dwr.Student" converter="bean"></convert> <create javascript="stu" creator="new"> <param name="class" value="com.dwr.Test"></param> <!-- include 看名字就知道是干嘛的.就是包含进来的方法,没有被包含的就不能被调用.可以配多个, 在这里我没有把老师的方法包含进来.学生就不能调用老师的方法啦! 当然默认是包含了所有公用方法,只能访问公有的.其它都不行. --> <include method="getBeanList"/> <include method="getList"/> <include method="getStudent"/> </create> <!-- 郁闷.我调学生的方法.他怎么知道用学生的bean进行转换? --> <convert match="com.dwr.Teacher" converter="bean"></convert> <create javascript="tea" creator="new"> <param name="class" value="com.dwr.Test"></param> <!-- 这里老师没有写include包含什么方法?表示他能够调用所有的公用方法. 老师也可以调用学生的方法 --> </create> </allow> </dwr> java 后台代码 package com.dwr; import java.util.ArrayList; import java.util.List; public class Test { public List getList() { List list = new ArrayList(); List list1 = new ArrayList(); List list2 = new ArrayList(); list1.add("张三"); list1.add("李四"); list1.add("王五"); list2.add("豆"); list2.add("qing"); list2.add("computer"); list.add(list1); list.add(list2); return list; } public List getBeanList() { List list = new ArrayList(); Student stu = new Student(); stu.setName("dd"); stu.setPassword("1234"); stu.setSex("男"); Student stu1 = new Student(); stu1.setName("mm"); stu1.setPassword("5678"); stu1.setSex("女"); list.add(stu); list.add(stu1); return list; } public Student getStudent() { Student stu = new Student(); stu.setName("dd"); stu.setPassword("1234"); stu.setSex("男"); return stu; } public Teacher getTeacher() { Teacher tea = new Teacher(); tea.setName("米"); tea.setPassword("饭"); tea.setSex("男"); return tea; } } 实体bean的代码 package com.dwr; //学生的bean public class Student { private String name; private String password; private String sex; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } } //老师的bean package com.dwr; public class Teacher { private String name; private String password; private String sex; private String type; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getType() { return type; } public void setType(String type) { this.type = type; } } 界面如果调用的 只是小测试,没写得很漂亮 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!-- 引用 --> <script type='text/javascript' src='/dwr_test1/dwr/interface/stu.js'></script> <script type='text/javascript' src='/dwr_test1/dwr/interface/tea.js'></script> <script type='text/javascript' src='/dwr_test1/dwr/engine.js'></script> <script type='text/javascript' src='/dwr_test1/dwr/util.js'></script> <script type="text/javascript"> <!-- //拿到集合里的值 function getLists() { stu.getList(getListBack); } function getListBack(data) { content.innerHTML=data; } //获得集合 里面的对象值 function getBeanLists() { stu.getBeanList(getBeanListBack); } function getBeanListBack(data) { /* 他返回来的是一个data数组 里面放的是对象 拿到第一个对象.再去拿他的属性. */ for(var a=0;a<data.length;a++) { //alert(data[a].name); //alert(data[a].password); content.innerHTML+=data[a].name+" "+data[a].password+" "+data[a].sex+"<br>"; } } //拿出javabean 里面的值 function getBeans() { stu.getStudent(getBeanBack); } function getBeanBack(data) { //alert(data.name); //alert(data.password); content.innerHTML=data.name+" "+data.password+" "+data.sex } //拿出Teacher里面的值 function getTeacherBeans() { tea.getTeacher(getTeacherBack); } function getTeacherBack(data) { content.innerHTML=data.name+" "+data.password+" "+data.sex } //--> </script> <html> <body> <div id="content"></div> <input type="button" value="获得集体里的值" onclick="getLists()"> <input type="button" value="获得集合里对象的值" onclick="getBeanLists()"> <input type="button" value="获得对象的值" onclick="getBeans()"> <input type="button" value="获得老师对象的值" onclick="getTeacherBeans()"> </body> </html> 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |