加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

dwr与其他框架的整合

发布时间:2020-12-16 01:59:19 所属栏目:百科 来源:网络整理
导读:1.DWR与Servlet整合 在DWR与Servlet整合中需要用到两个Java类,分别是WebContext和WebContextFactory。其中WebContext是接口。 这两个类给与了访问标准HttpServlet对象的入口,这些对象包括HttpServletRequest、HttpServletResponse、HttpSession、ServletCo

1.DWR与Servlet整合

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

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

使用WebContext的方法为:

[c-sharp] view plain copy
  1. packagecom.yjpeng.dwr;
  2. importjavax.servlet.ServletConfig;
  3. importjavax.servlet.ServletContext;
  4. importjavax.servlet.http.HttpServletRequest;
  5. importjavax.servlet.http.HttpServletResponse;
  6. importjavax.servlet.http.HttpSession;
  7. importorg.directwebremoting.WebContext;
  8. importorg.directwebremoting.WebContextFactory;
  9. publicclassDwrService{
  10. publicStringsayHello(Stringmessage){
  11. WebContextwebContext=null;
  12. webContext=WebContextFactory.get();
  13. HttpServletRequestrequest=webContext.getHttpServletRequest();
  14. HttpServletResponseresponse=webContext.getHttpServletResponse();
  15. HttpSessionsession=webContext.getSession();
  16. ServletContextcontext=webContext.getServletContext();
  17. ServletConfigconfig=webContext.getServletConfig();
  18. return"欢迎使用DWR"+message;
  19. }
  20. }

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中的回调函数。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读