dwr与其他框架的整合

来源:互联网 发布:淘宝移动端是什么意思 编辑:程序博客网 时间:2024/05/04 21:08

1.DWR与Servlet整合

在DWR与Servlet整合中需要用到两个Java类,分别是WebContext和WebContextFactory。其中WebContext是接口。

这两个类给与了访问标准HttpServlet对象的入口,这些对象包括HttpServletRequest、HttpServletResponse、HttpSession、ServletContext、ServletConfig。

使用WebContext的方法为:

[c-sharp] view plaincopy
  1. package com.yjpeng.dwr;  
  2.   
  3. import javax.servlet.ServletConfig;  
  4. import javax.servlet.ServletContext;  
  5. import javax.servlet.http.HttpServletRequest;  
  6. import javax.servlet.http.HttpServletResponse;  
  7. import javax.servlet.http.HttpSession;  
  8.   
  9. import org.directwebremoting.WebContext;  
  10. import org.directwebremoting.WebContextFactory;  
  11.   
  12. public class DwrService {  
  13.     public String sayHello(String message){  
  14.         WebContext webContext = null;  
  15.         webContext = WebContextFactory.get();  
  16.         HttpServletRequest request = webContext.getHttpServletRequest();  
  17.         HttpServletResponse response = webContext.getHttpServletResponse();  
  18.         HttpSession session = webContext.getSession();  
  19.         ServletContext context = webContext.getServletContext();  
  20.         ServletConfig config = webContext.getServletConfig();  
  21.         return "欢迎使用DWR" + message;  
  22.     }  
  23.   
  24. }  

 

2.DWR与Spring整合

DWR与Spring整合需要用spring Creator,这个创造器会在spring beans.xml里查询beans,并且会使用Spring去穿件他们,要让DWR使用spring创造器去创建和远程调用beans,配置方法如下:

<allow>
<create creator="spring" javascript="service">
   <param name="beanName" value="AjaxService"/>
</create>
</allow>

在上面value="AjaxService"用到的配置内容如下:
<bean id="AjaxService" class="com.ajax.dwr.AjaxService" scop="prototype">
</bean>

 

3.DWR与Struts2整合

DWR也提供与Struts2框架的集成。借助于这种支持,可以远程访问自己的Struts2 Actions,就像任何其他类一样。使用DWR的这种特性需要两步。首先需要在dwr.xml文件中创建一些新的条目,配置方法如下:
<create creator="none" javascript="DWRAction">
<param name="class" value="org.directwebremoting.webwork.DWRAction"/>
<include method="execute"/>
</create>
<convert converter="bean" match="org.directwebremoting.webwork.ActionDefinition">
<param name="include" value="namespace,action,method,executeResult"/>
</convert>
<convert converter="bean" match="org.directwebremoting.webwork.AjaxResult"/>

如果自己的Action调用返回Action实例,而不是更典型的纯文本,则还需要在dwr.xml文件中添加一条配置信息:
<convert converter="bean" match="<your_action_package>.*"/>
Match属性的值会被返回的Action实例。一旦完成上述配置,就需要把常见的DWR JavaScript代码导入到执行Actions的JSP文件中。除此之外,还需要导入DWRActionUtil.js文件,它是需要与WebWork Actions一起工作的帮助代买。调用Action非常类似于调用其他任何远程类,差别是它通过DWRActionUtil对象实现,调用方法如下:
DWRActionUtil.execute(id,params,callback[,displayMessage]);
其中:
id,这个参数是Action URL,通常使用.action扩展名。也可能是一个action-DefinitionObject JavaScript对象。在这种情况下,这个对象必须指定如下域:
namespace(struts.xml文件中Action的命名空间)、action(struts.xml文件中Action的名字)、exectuteResult(可能是true或者false,说明如果方法调用返回一个Action实例,是直接返回这个实例,还是执行这个实例)。
params,如果不需要传递参数,这是一个空对象{}。这个参数可能是一个域的ID(某值会被传递给Action调用);也可能是一个表单的ID,这个时候这儿表单的所有值都会被传递。
callback,这个份DWR中的回调函数。

0 0
原创粉丝点击