dwrはspringと一緒に使う方法です.

13868 ワード

文章の内容を抜粋します.http://www.cnblogs.com/linjiqin/archive/2011/03/28/1998125.html
 
dwr与spring一起使用的方法
 
 
<?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">
    
    <!-- Spring    -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:resource/app*.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <!--   DWR      -->
    <servlet>
        <servlet-name>dwrServlet</servlet-name>
        <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
        <!--        -->
        <init-param>
            <param-name>config</param-name>
            <!--       ","   -->
            <param-value>
                /WEB-INF/classes/config/dwr.xml                
            </param-value>
        </init-param>
        <init-param>
            <param-name>debug</param-name>
            <param-value>true</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>dwrServlet</servlet-name>
        <url-pattern>/dwr/*</url-pattern>
    </servlet-mapping>
</web-app>
 
 
 
 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://getahead.org/dwr/dwr20.dtd">
<!--   dwr   -->
<dwr>
    <allow>
        <!--   JS  ,           JS      -->
        <create javascript="helloSrv" creator="new">
            <param name="class" value="services.HelloServices"></param>
        </create>
        <!--  Spring   Java   -->
        <create javascript="deptSrv" creator="spring">
            <param name="beanName" value="deptServices"></param>
            <!--      -->
            <exclude method="deleteDept" />
        </create>
        <create javascript="loginSrv" creator="spring">
            <param name="beanName" value="loginSrv"></param>
        </create>
        <!--               -->
        <convert match="entity.*" converter="bean"></convert>
        <convert match="java.lang.Throwable" converter="bean">
            <param name="include" value="message"></param>
        </convert>
    </allow>
</dwr>
 
 
creator=「spring」は、作成対象の方式を「spring」が管理すると説明しています.
<param name=「beanName」value=「deptServices」>
springで作成するクラスの名前を表します.「deptServices」に基づいてspringを一意に識別するプロファイルを検索して、対応する関係を形成します.
<convert match=「entity.*」converter=「bean」
ここでconvert="bean"は、伝達の対象と戻るオブジェクトの変換を説明するためにbeanを使用する方法であり、この中の値は変わらない.
 
 
<?xml version="1.0" encoding="UTF-8"?>
<!--
            
 -->
<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"
    >            
    <bean id="deptServices" class="services.DeptServices"></bean>
    <bean id="loginSrv" class="services.LoginService"></bean>
</beans>
 
 
 
package entity;

public class Dept {
    private Long id;
    private String name;

    public Dept() {

    }

    public Dept(Long id, String name) {
        this.id = id;
        this.name = name;
    }

    public Long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

}
 
 
 
package services;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import entity.Dept;

@SuppressWarnings("unchecked")
public class DeptServices {

    public List findDept() {
        throw new RuntimeException("    !");
    }

    public void deleteDept(Long id) {
        System.out.println("Delete dept " + id);
    }

    public List getDeptsForPo() {
        List depts = new ArrayList();
        depts.add(new Dept(1l, "   "));
        depts.add(new Dept(2l, "   "));
        depts.add(new Dept(3l, "   "));
        depts.add(new Dept(4l, "   "));
        return depts;
    }

    
    public void saveDept(List<Dept> depts) {
        // System.out.println(dept.getId() + ":" + dept.getName());
        System.out.println(depts);
    }

    public List getDepts() {
        List depts = new ArrayList();
        Map map = new HashMap();
        map.put("id", "01");
        map.put("name", "   ");
        depts.add(map);

        map = new HashMap();
        map.put("id", "02");
        map.put("name", "   ");
        depts.add(map);

        map = new HashMap();
        map.put("id", "03");
        map.put("name", "   ");
        depts.add(map);

        map = new HashMap();
        map.put("id", "04");
        map.put("name", "   ");
        depts.add(map);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return depts;
    }
    
    public void test(){
    	System.out.println("tests");
    }
    
    public void testPar(String s){
    	System.out.println("testPar " + s);
    }
    
    public void testObj(Dept d){
    	System.out.println(d.getId());
    	System.out.println(d.getName());
    }
}
 
 
 
package services;

/**
 * @author dlwu
 *
 */
public class HelloServices {
	
    public String sayHello(String name){
        System.out.println("Hello now!");
        return "Hello " + name + "!";
    }
}
 
 
 
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'hello.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">

  </head>
  <!--     js,    : http://localhost:8083/dwrweb/dwr/ -->
  <script type="text/javascript" src="dwr/engine.js"></script>
  <script type="text/javascript" src="dwr/interface/deptSrv.js"></script>
  <script type="text/javascript" src="dwr/util.js"></script>
  <script type="text/javascript">
          
          function test(){
              //  dept  
             
              deptSrv.test();
          }
          function test1(){
              //       
              var temp = "test parameter String";
              deptSrv.testPar(temp);
          }
          function test2(){
              //    
              var obj = {id:"1234",name:"huangbiao"};
              deptSrv.testObj(obj);
          }
          function test3(){
              //    
              deptSrv.getDepts(function(data){
				for(var i = 0; i < data.length; i++ ){
					alert(data[i].id);
				}
              });
          }

          
  </script>
  <body>
      <input value="spring  " type="button" onclick="test();"/>
      <input value="       " type="button" onclick="test1();"/>
      <input value="      " type="button" onclick="test2();"/>
      <input value="      " type="button" onclick="test3();"/>
  </body>
</html>
 
 
 
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'hello.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
  </head>
  <!--     js,    : http://localhost:8083/dwrweb/dwr/ -->
  <script type="text/javascript" src="dwr/engine.js"></script>
  <script type="text/javascript" src="dwr/interface/helloSrv.js"></script>
  <script type="text/javascript" src="dwr/util.js"></script>
  <script type="text/javascript">
          function hello(){
              //   
              //          
            /*var fn = function(result){
                $("msg").innerHTML = result;
            }
            helloSrv.sayHello($("name").value, fn);*/
            
            //   
            helloSrv.sayHello($("name").value, function(result){
                $("msg").innerHTML=result;
            });
            
            //   
            //        :        js        
            //     : dwr    , web.xml   , : <url-pattern>/dwr/*</url-pattern>        
            //     : dwr java       , dwr.xml           
            //     :       
            //     :       ,          
            //     :             
            //dwr.engine._execute("dwr", 'helloSrv', 'sayHello', $("name").value, fn);
            
          }
  </script>
  <body>
      <div id="msg"></div>
    <input type="text" id="name" />
    <input type="button" value="Hello" onclick="hello();" />
  </body>
</html>
 
 要求を同期に設定する方法――DWREngine.set Aync(true);非同期