dwr 简单例子

无聊在电脑上乱点.点出一个DWR的例子.于是乎开始学习DWR

拿出自己的练习晒下;

在做练习的时候遇到一个问题。jbuilder的web-inf下不生成classes文件夹,也不生成.class文件

经过研究解决办法为:右键点击webmodule   选择properties,再选择Content,选择Include All classes and  resources  确定。OK,


看配置

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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" version="2.4">
  <display-name>WebModule1</display-name>
<!--插入dwr配置-->
  <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>
</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>
    <!-- javascript的对象是jsmethodname,create="new"是DWR自己创建类 -->
        <create javascript="jsmethodname" creator="new" scope="application">
            <!-- value是java中的类名 -->
            <param name="class" value="org.li.dwr.Hello"></param>
<!--<include>标签指定要公开给javascript的方法。不指定的话就公开所有方法。
  <exclude>标签指定要防止被访问的方法。-->
           <include method="alertStr"/>
           <include method="alertJSStr"/>
           <include method="returnAl"/>
           <include method="getList"/>
           <include method="getObj"/>
        </create>

  <!--转换java类型   -->
       <convert converter="bean" match="org.li.dwr.InputRecord">
      </convert>
    </allow>
</dwr>




jsp页面:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>DWR</title>
    <meta http-equiv="content-type" content="text/html; charset=GBK">
    <script type='text/javascript' src='dwr/interface/jsmethodname.js'></script>
    <script type='text/javascript' src='dwr/engine.js'></script>
    <script type='text/javascript' src='dwr/util.js'></script>
    <script type="text/javascript" src="hello.js"></script>
   <!--其中jsmethodname.js是dwr根据配置文件自动生成的,engine.js和util.js是dwr自带的脚本文件。
 -->
  </head>
  <body>
    <input id="jbutton" type="button" value="Hello World" onclick="jbClick()"/>
    <div id="jdiv"></div>
    <div id="jdiv2"></div>
   
    <div id="uid"></div>
    <div id="uname"></div>
    <div id="usex"></div>
    <div id="uaddress"></div>

  </body>
</html>



js :hello.js





 

/*
function load()//载入的时候调用
{
    var jbutton = document.getElementById("jbutton");
    jbutton.onclick=function(event)//注册按钮点击事件
    {
        jbClick();
    };
}
*/
function jbClick()//按钮点击事件
{
  //  jshello.getHelloWorld(callback);//没有参数传递,只传递回调函数就行
 //   jsmethodname.alertStr(callback1);//无参数
    var data="js传入java中的值";
  //  jsmethodname.alertJSStr(data,callback);//有参数。第一个为参数,第二个为回调方法接收返回值
  jsmethodname.getList(callbackList);  //无参数,返回list
  //jsmethodname.getObj(callbackObj);
}
function callback1(msg)//回调函数 ,调用java方法的javascript函数
{
     //msg就是java方法的返回值
     alert(msg);
}

function callback(msg)//回调函数
{
    //这里可以进行参数处理DWRUtil 的 setValue() 方法会将传回的 msg 设定给指定 id 的 DOM
    DWRUtil.setValue('jdiv',msg);
}
function callbackList(data){

for(var i=0;i<data.length;i++){
  DWRUtil.setValue("uid", data[i].id);
  DWRUtil.setValue("uname", data[i].name);
  DWRUtil.setValue("usex", data[i].sex);
  DWRUtil.setValue("uaddress", data[i].address);
}
}


function callbackObj(data){
 
 //知道属性
  DWRUtil.setValue("uid", data.id);
  DWRUtil.setValue("uname", data.name);
  DWRUtil.setValue("usex", data.sex);
  DWRUtil.setValue("uaddress", data.address);
 
 /**
 * 不知道属性
for(var property in data){
  //alert("property:"+property);
  alert(property+":"+data[property]);
  }
 */

}



java类

Hello.java




 

package org.li.dwr;

import java.util.Date;
import java.util.ArrayList;
import java.util.List;

public class Hello {
  public String getHelloWorld() {
    return "现在的时间为:" + new Date();
  }

  public String alertStr() {
    return "DWR框架:无参数";
  }

  public String alertJSStr(String jsvalue) {
    return "DWR框架:" + jsvalue;
  }

  public ArrayList returnAl() {
    ArrayList al = new ArrayList();
    al.set(1, "a");
    al.set(2, "b");
    al.set(3, "c");
    al.set(4, "d");
    return al;
  }
 
  public List getList() {
  List list = new ArrayList();
  for(int i=0;i<5;i++){
    InputRecord ir = new InputRecord();
    ir.setId(1);
    ir.setName("依韵");
    ir.setSex("男");
    ir.setAddress("北京市");
    list.add(ir);
  }
  return list;
 }
 
 public InputRecord getObj() {
 InputRecord ir = new InputRecord();
 ir.setId(2);
 ir.setName("白色黄昏");
 ir.setSex("女");
 ir.setAddress("河南");
 return ir;
 }


}



InputRecord.java




package org.li.dwr;

public class InputRecord {
  private int id;
  private String name;
  private String sex;
  private String address;
  public String getAddress() {
    return address;
  }

  public int getId() {
    return id;
  }

  public String getName() {
    return name;
  }

  public String getSex() {
    return sex;
  }

  public void setSex(String sex) {
    this.sex = sex;
  }

  public void setName(String name) {
    this.name = name;
  }

  public void setAddress(String address) {
    this.address = address;
  }

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


你可能感兴趣的:(JavaScript,java,xml,DWR,JBuilder)