DWR配置

二.开发步骤

   NO1:从 http://getahead.ltd.uk/dwr/ 下载dwr.jar
   NO2:在web.xml 文件中 DWR Servlet的配置
     
        <servlet-name>dwr-invoker</servlet-name>
         <servlet-class>uk.ltd.getahead.dwr.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>
              DWRServlet 的 debug参数的选项:
                     1.true : 在调试时设为true
                      2.false :部属到实际应用时记得要设置为false
   NO3: 编写后台的java类
        package com.rbi.stady;
              public class DwrTestServiceImpl {
              public String getMessage(String name){
                 return "Hello "+name;
              }
             }
   NO4:DWR配置文件
        默认文件: WEB-INF\下的dwr.xml
        <dwr>
              <allow>
                   <create creator="new" javascript="dwrTest">
                    <param name="class" value="com.rbi.stady.DwrTestServiceImpl" />
                    </create>
               </allow>
        </dwr>
   NO5:HTML添加DWR的js和自动生成的后台代码
           1.<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/engine.js"></script>
           2.<scripttype="text/javascript" src="<%=request.getContextPath()%>/dwr/until.js"></script>
           2.<scripttype="text/javascript" src="<%=request.getContextPath()%>/dwr/interface/dwrTest.js"></script>
   NO6:js的调用
            dwrTest.getMessage(‘Jack’,getDataFun)
            function getDataFun(data){
                …………
        }
  
  
   三. DWR配置文件
       1. 类的配置:<create></create>
          1.远程调用类的配置
             <create creator="new" javascript="dwrTest" scope="request">
                 <param name="class" value="com.rbi.stady.DwrTestServiceImpl" />
                 <include method="checkUser" />
                 <!--<exclude method="..."/>-->
             </create>
            
             A:<create> 标签:
                a).creator (必须项)值范围:new,spring,scripted 新版本后新增 none, jsf,struts,pageflow

                b).scope (可选项)值范围 : "application", "session", "request" 和"page"。默认page
             B:<param>标签:
                  <create>标签为”new”时值为“class” value值为类路径
                  <create>标签为“spring”时值为“beanName” value值为bean的配置名称
                  <include> 和<exclude>标签:method=”方法名”有此标签或二者选其一
                              无: 所有方法都可访问
                             <include>: 此列表的方法可访问,不在其内的不可访问
                             <exclude>:此列表的方法不可访问,不在其内的可访问
                                 安全上比较好的设计是使用" include "而不是" exclude"。
          
        2.Bean的配置<convert></convert>
              <convert converter="bean" match="example.Fred"/>
                 <param name="include"  value="property1, property2"/>
                 <!--<param name="exclude" value="property1, property2"/> -->
              </convert>
                      <param>标签设定限制属性同方法只不过用逗号隔开.如果要允许转换一个包或者子包下面的所有类,可以这样写:
              <convert converter="bean" match="your.full.package.*"/>显而易见,这样写是允许转换所有的JavaBean:
                 <convert converter="bean" match="*"/>
             DWR 可以convert转换数据类型:
                    1. 基本的类型:
                          boolean, byte, short,
                          int, long, float, double,
                          char, java.lang.Boolean, java.lang.Byte,
                          java.lang.Short, java.lang.Integer, java.lang.Long,
                          java.lang.Float, java.lang.Double, java.lang.Character,
                          java.math.BigInteger, java.math.BigDecimal 和 java.lang.String
                    2. 日期类型:与javascript里的Date对应.包括:
                          java.util.Date, java.sql.Date, java.sql.Times or java.sql.Timestamp
                    3. 自定义的Bean
                       <convert converter="bean" match="com.rbi.entity.User"/>
                    4. List,Set类型.
                         返回客户端为数组.若返回的为对象数组,该对象必须在配置文件中有定义Bean
                    5. Map 类型.
                        返回的类型如同对象
                        如: 服务器 返回的Map内容如下:Map map = new HashMap();
                         map.put(“no1”,user1);map.put(“no2”,user2);
                            客户端js对取得的data取内容里的值:
                             var data1 = data[‘no1’];
                             var data2 = data.no2;
                    6. 返回void类型时回调函数无参数
                              传入参数:
                                1. 基本类与日期型自定义Bean同上.
                                2. List,Set类型
                               
        3.<signatures> </signaturs>
               Bean 的属性还是,远程调用类的方法都要通过<signatures>标签标置
                 有其他的参数要有逗号隔开

                          <signatures>
                               <![CDATA[
                                  import java.util.List;
                                  import com.rbi.stady.DwrTestServiceImpl;
                                  import com.rbi.entity.User;
                                  import com.rbi.entity.Address;

                                 DwrTestServiceImpl.getUsersNames(List<User>);
                                 User.setAddresses(List<Address>);
                               ]]>
                          </signatures>
   四. Servlet参数传递
        做web应用的时候,常常要取得用户身份,
        或者取得一些Servlet上下文变量和应用环境等.
        我们通常获取ServletContext,request, response等来取得这些值。
           DWR提供了两个办法解决这些问题
                1、使用DWR的一个静态的API,不推荐。
                2. DWR会自动注入这些servlet变量,这些变量只包括以下的五个:
                       HttpServletRequest
                       HttpServletResponse
                       HttpSession
                       ServletContext
                       ServletConfig 
   
   五. 与其他框架结合的问题
      1.与spring框架相结合
         <servlet>
<servlet-name>context</servlet-name>
<servlet-class> org.springframework.web.context.ContextLoaderServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<listener>
<listener-class>
org.springframework.web.context
.ContextLoaderListener
</listener-class>
</listener>

配置context-param
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/…/application-….xml,
</param-value>
</context-param>

你可能感兴趣的:(JavaScript,java,bean,servlet,DWR)