posts - 165, comments - 198, trackbacks - 0, articles - 1
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

dwr 学习笔记

Posted on 2008-07-17 15:17 G_G 阅读(1708) 评论(0)  编辑  收藏 所属分类: javascriptjsonjavaGeneral

参考引用:
使用说明: lib添加dwr.jar
web.xml添加
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC
    "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd"
>
<web-app id="dwr">
  
<display-name>DWR (Direct Web Remoting)</display-name>
  
<description>A Simple Demo DWR</description>

  
<servlet>
    
<servlet-name>dwr-invoker</servlet-name>
    
<display-name>DWR Servlet</display-name>
    
<description>Direct Web Remoter Servlet</description>
    
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>

    
<!-- This should NEVER be present in live -->
    
<init-param>
      
<param-name>debug</param-name>
      
<param-value>true</param-value>
    
</init-param>

  
</servlet>
  <!-- 服务起来后 在地址中直接输入 http://.../dwr 就可以查看对外提供的服务类 -->
  
<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 1.0//EN"
    "http://www.getahead.ltd.uk/dwr/dwr10.dtd"
>
<dwr>
    
<allow>
        
<!-- 远程调用类 定义 updateItem,getItem 和 findItems 方法可用  -->
        
<create creator="new" javascript="CatalogDAO">
            
<param name="class" value="com.ajaxlab.ajax.CatalogDAO" />
            
<include method="getItem" />
            
<include method="findItems" />
           
<include method="updateItem" />
        </create>
        
        
<!-- 数据展现定义格式 (javascriot json 格式) 
            此定义 id name description 为可见 
                  price 隐藏 
            E.g -> 
                {
                  description:"中国制造.", 
                  id:"产品-", 
                  name:"新品-"
                }
        
-->
        
<convert converter="bean" match="com.ajaxlab.ajax.Item">
            
<param name="include"
                value
="id,name,description,formatted- Price" />
        
</convert>
    
</allow>
</dwr>



类说明
bean Item:
  private String id = "";
  private String name = "";
  private String description = "";
  private int price = 0;
  get();set();


dao
    public Item getItem(String id) {
        Item item 
= new Item("产品-"+id);
        item.setName(
"新品-"+id);
        item.setPrice(
100);
        item.setDescription(
"中国制造.");
        
return item;
    }

    
public List findItems(String expression) {
        List list 
= new ArrayList();
        Item item1 
= new Item("产品-001");
        item1.setName(
"新品-001");
        item1.setDescription(expression);
        item1.setPrice(
10);
        Item item2 
= new Item("产品-002");
        item2.setName(
"新品-002");
        item2.setDescription(expression);
        item2.setPrice(
15);
        Item item3 
= new Item("产品-003");
        item3.setName(
"新品-003");
        item3.setDescription(expression);
        item3.setPrice(
35);
        list.add(item1);
        list.add(item2);
        list.add(item3);
        
return list;

    }

   /* html input text ->
        {
              description:"中国制造.",
              id:"1",
              name:"刘凯毅"
        }
        return true ;
       
     */

    public boolean updateItem(Item item ){
        if( item!=null && item.getName().equals("刘凯毅") )
            return true;
       
        return false ;
    }



js 方法:
在 input 输入->>
method( !! )

bean(id,name...)
{id:1,name:'liukaiyi'}

当多参数 为 map
{dd:'liu',aa:'gg'}

js多参数
class.method(
    objectEval($("p00").value),
    objectEval($("p01").value),
reply0);


展现页:
<html>
<head>
  
<title>DWR Test</title>
  
<!-- These paths use .. so that they still work behind a path mapping proxy. The fully qualified version is more cut and paste friendly. -->
  
<script type='text/javascript' src='/testDwr/dwr/interface/CatalogDAO.js'></script>
  
<script type='text/javascript' src='/testDwr/dwr/engine.js'></script>
  
<script type='text/javascript' src='/testDwr/dwr/util.js'></script>
  
  
<script type='text/javascript'>
  //输入到方法中 参数 格式转换
  
function objectEval(text){
    text 
= text.replace(/\n/g, ' ');
    text 
= text.replace(/\r/g, ' ');
    
if (text.match(/^\s*\{.*\}\s*$/))
    {
      text 
= '[' + text + '][0]';
    }
    
return eval(text);
  }
 
  //本例 alert 展现

  
var reply = function(data){
      alert(dwr.util.toDescriptiveString(data, 
2));
  }

  
</script>


</head>
<body >

<li>
  findItems(    
<input  type='text'  value='""' id='p00' />  );
  
<input class='ibutton' type='button' onclick='CatalogDAO.findItems(objectEval($("p00").value), reply);' value='Execute'  />

</li>
<li>
  getItem(    
<input class='itext' type='text' size='10' value='""' id='p10' title='Will be converted to: java.lang.String'/>  );
  
<input class='ibutton' type='button' onclick='CatalogDAO.getItem(objectEval($("p10").value), reply);' value='Execute'  title='Calls CatalogDAO.getItem(). View source for details.'/>
</li>


<li>
  updateItem(   
<input class='itext' type='text' size='10' value='{}' id='p20'/>  );
  <input class='ibutton' type='button' onclick='CatalogDAO.updateItem(objectEval($("p20").value), reply);' value='Execute' />
</li>

</body></html>






只有注册用户登录后才能发表评论。


网站导航: