Dwr 2.0

Direct Web Remoting
DWR allows Javascript in a browser to interact with Java on a server and helps you manipulate web pages with the results.
DWR is Open Source, available under the Apache Software License v2.


本实例参考dwr 2.0官方文档


1).web.xml
<?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 ">
 <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>
 </servlet>
 <servlet-mapping>
  <servlet-name>dwr-invoker</servlet-name>
  <url-pattern>/dwr/*</url-pattern>
 </servlet-mapping>
</web-app>
2).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>
  <create creator="new" javascript="HW">
   <param name="class" value="com.xmddl.dwr.demo.HelloWorld" />
  </create>
  <create creator="new" javascript="People" scope="script">
        <param name="class" value="com.xmddl.dwr.demo.People"/>
     </create>
     <convert converter="bean" match="com.xmddl.dwr.demo.Person"/>
  <convert converter="bean" match="com.xmddl.dwr.demo.bo.UserInfo"/>
 </allow>
</dwr>
3).index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="
http://java.sun.com/jsf/html " prefix="h"%>
<%@ taglib uri="
http://java.sun.com/jsf/core " prefix="f"%>
<html>
 <head>
  <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->
  <script type='text/javascript' src='dwr/engine.js'></script>
  <script type='text/javascript' src='dwr/util.js'></script>
  <script type='text/javascript' src='dwr/interface/HW.js'></script>
  <script type='text/javascript' src='js/hello.js'></script>
 </head>
 <body>
  This is my JSP page.
  <br>
  <input type="text" id="name" size="20" />
  <input type="button" id="test" value="click me" οnclick="OnSave()" />
  <p>
   <input value="Include Page" type="button" οnclick="forward()" />
   <br />
   Included Page:
  </p>
  <div id="forward"></div>
  <ul>
    <li> LE16 7TR </li>
    <li> NR14 7SL </li>
    <li> B92 7TT </li>
    <li> E17 8YT </li>
    <li> SN4 8QS </li>
    <li> NN5 7HT  </li>
  </ul>
  <table>
   <tr>
    <td>
     Zipcode/Postcode:
    </td>
    <td>
     <input id="postcode" type="text" οnchange="fillAddress()" />
    </td>
   </tr>
   <tr>
    <td>
     House name/number:
    </td>
    <td>
     <input id="house" type="text" />
    </td>
   </tr>
   <tr>
    <td>
     Line 2:
    </td>
    <td>
     <input id="line2" type="text" />
    </td>
   </tr>
   <tr>
    <td>
     Line 3:
    </td>
    <td>
     <input id="line3" type="text" />
    </td>
   </tr>
   <tr>
    <td>
     Line 4:
    </td>
    <td>
     <input id="line4" type="text" />
    </td>
   </tr>
  </table>
 </body>
</html>
4).index.html
<html>
 <head>
  <title>DWR - Test Home</title>
  <script type='text/javascript' src='dwr/engine.js'></script>
  <script type='text/javascript' src='dwr/util.js'></script>
  <script type='text/javascript' src='dwr/interface/People.js'></script>
  <script type='text/javascript' src='js/table.js'></script>
 </head>
 <body>
  <h3>
   All People
  </h3>
  <table border="1">
   <thead>
    <tr>
     <th>
      Id
     </th>
     <th>
      Name
     </th>
     <th>
      Address
     </th>
     <th>
      Salary
     </th>
     <th>
      Actions
     </th>
    </tr>
   </thead>
   <tbody id="peoplebody">
    <tr id="pattern" style="display: none;">
     <td>
      <span id="tableId">Id</span>
      <br />
     </td>
     <td>
      <span id="tableName">Name</span>
      <br />
     </td>
     <td>
      <span id="tableAddress">Address</span>
     </td>
     <td>
      <span id="tableSalary">Salary</span>
     </td>
     <td>
      <input id="edit" type="button" value="Edit"
       οnclick="editClicked(this.id)" />
      <input id="delete" type="button" value="Delete"
       οnclick="deleteClicked(this.id)" />
     </td>
    </tr>
   </tbody>
  </table>
  <h3>
   Edit Person
  </h3>
  <table class="plain">
   <tr>
    <td>
     Name:
    </td>
    <td>
     <input id="name" type="text" size="30" />
    </td>
   </tr>
   <tr>
    <td>
     Salary:
    </td>
    <td>
     $
     <input id="salary" type="text" size="20" />
    </td>
   </tr>
   <tr>
    <td>
     Address:
    </td>
    <td>
     <input type="text" id="address" size="40" />
    </td>
   </tr>
   <tr>
    <td colspan="2" align="right">
     <small>(ID=<span id="id">-1</span>)</small>
     <input type="button" value="Save" οnclick="writePerson()" />
     <input type="button" value="Clear" οnclick="clearPerson()" />
    </td>
   </tr>
  </table>
 </body>
</html>
5).hello.js
function OnSave() {
 //var user=document.getElementById("name").value;
 //var user=DWRUtil.getValue("name");
 //var user=$("name").value;
 var user=dwr.util.getValue("name")+"china";
 HW.test(user,callback);
}
function callback(data) {
 //DWRUtil.setValue("name", data);
 $("name").value=data;
}
function searchData()
{
 HW.returnH(function(data){DWRUtil.setValue("name", data);});
}
function forward() {
  HW.getInclude(function(data) {
    DWRUtil.setValue("forward", data, { escapeHtml:false });
  });
}
function fillAddress() {
  var postcode = dwr.util.getValue("postcode");
  HW.fillAddress(postcode, function(address) {
    dwr.util.setValues(address);
  });
}
6).table.js
function init() {
 fillTable();
}
var peopleCache = {};
var viewed = -1;
function fillTable() {
 People.getPersonList(callback);
}
function callback(people) {
 dwr.util.removeAllRows("peoplebody", {filter:function (tr) {
  return (tr.id != "pattern");
 }});
 people.sort(function (p1, p2) {
  return p1.name.localeCompare(p2.name);
 });
 var person, id;
 for (var i = 0; i < people.length; i++) {
  person = people[i];
  id = person.id;
  dwr.util.cloneNode("pattern", {idSuffix:id});
  dwr.util.setValue("tableId" + id, person.id);
  dwr.util.setValue("tableName" + id, person.name);
  dwr.util.setValue("tableSalary" + id, person.salary);
  dwr.util.setValue("tableAddress" + id, person.address);
  $("pattern" + id).style.display = "block";
  peopleCache[id] = person;
 }
}
function editClicked(eleid) {
  // we were an id of the form "edit{id}", eg "edit42". We lookup the "42"
 var person = peopleCache[eleid.substring(4)];
 dwr.util.setValues(person);
}
function deleteClicked(eleid) {
  // we were an id of the form "delete{id}", eg "delete42". We lookup the "42"
 var person = peopleCache[eleid.substring(6)];
 if (confirm("Are you sure you want to delete " + person.name + "?")) {
  dwr.engine.beginBatch();
  People.deletePerson(person);
  fillTable();
  dwr.engine.endBatch();
 }
}
function writePerson() {
 var person = {id:viewed, name:null, address:null, salary:null};
 dwr.util.getValues(person);
 dwr.engine.beginBatch();
 People.savePerson(person,function(data) {alert(data);});
 fillTable();
 dwr.engine.endBatch();
}
function showData(data) {
   alert(data);
}      
function clearPerson() {
 viewed = -1;
 dwr.util.setValues({id:-1, name:null, address:null, salary:null});
}
7).HelloWorld.java
package com.xmddl.dwr.demo;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.util.LocalUtil;
public class HelloWorld {
    private static final String LINE4 = "line4";
    private static final String LINE3 = "line3";
    private static final String LINE2 = "line2";
    public Map<String, String> fillAddress(String origpostcode)
    {
        Map<String, String> reply = new HashMap<String, String>();
        String postcode = LocalUtil.replace(origpostcode, " ", "");
        if (postcode.equalsIgnoreCase("LE167TR"))
        {
            reply.put(LINE2, "Church Lane");
            reply.put(LINE3, "Thorpe Langton");
            reply.put(LINE4, "MARKET HARBOROUGH");
        }
        else if (postcode.equalsIgnoreCase("NR147SL"))
        {
            reply.put(LINE2, "Rectory Lane");
            reply.put(LINE3, "Poringland");
            reply.put(LINE4, "NORWICH");
        }
        else if (postcode.equalsIgnoreCase("B927TT"))
        {
            reply.put(LINE2, "Olton Mere");
            reply.put(LINE3, "Warwick Road");
            reply.put(LINE4, "SOLIHULL");
        }
        else if (postcode.equalsIgnoreCase("E178YT"))
        {
            reply.put(LINE2, "");
            reply.put(LINE3, "PO Box 43108 ");
            reply.put(LINE4, "LONDON");
        }
        else if (postcode.equalsIgnoreCase("SN48QS"))
        {
            reply.put(LINE2, "Binknoll");
            reply.put(LINE3, "Wootton Bassett");
            reply.put(LINE4, "SWINDON");
        }
        else if (postcode.equalsIgnoreCase("NN57HT"))
        {
            reply.put(LINE2, "Heathville");
            reply.put(LINE3, "");
            reply.put(LINE4, "NORTHAMPTON");
        }
        else
        {
            reply.put(LINE2, "Postcode not found");
            reply.put(LINE3, "");
            reply.put(LINE4, "");
        }
        return reply;
    }
    public String returnH() {
      return "Hello World";
    }
    public String test(String name) {
      System.out.println(name);
      return "Hello " + name;
    }
    public String getInclude() throws ServletException, IOException {
        WebContext wctx = WebContextFactory.get();
        return wctx.forwardToString("/index.html");
    }
}
8).People.java
package com.xmddl.dwr.demo;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class People {
 private static List<Person> personList = new ArrayList<Person>();
 private static final Map<Integer, Person> storeMap = new LinkedHashMap<Integer, Person>();
 public static List<Person> getPersonList() {
  personList.clear();
  Iterator<Entry<Integer, Person>> it = storeMap.entrySet().iterator();
  while (it.hasNext()) {
   Entry<Integer, Person> next = (Entry<Integer, Person>) it.next();
   personList.add((Person)next.getValue());
  }
  return personList;
 }
 public String savePerson(Person person) {
  if("".equals(person.getName()))
  {
   return "用户名不能为空!";
  }
  else
  {
   if (storeMap.keySet().contains(person.getId()))
    storeMap.put(Integer.parseInt(String.valueOf(person.getId())),
      person);
   else {
    person.setId(personList.size() + 1);
    storeMap.put(Integer.parseInt(String.valueOf(person.getId())),
      person);
   }
   return "保存成功!";
  }
 }
 public void deletePerson(Person person) {
  storeMap.remove(Integer.parseInt(String.valueOf(person.getId())));
 }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值