DWR Java API
There are only 2 Java classes that you commonly need to depend on within DWR as a user - WebContext
and WebContextFactory
. In DWR 1.x these are in the uk.ltd.getahead.dwr
package, for DWR 2.0 onwards they are in org.directwebremoting
. These classes give you to access to the standard HTTP servlet objects:
- HttpServletRequest
- HttpServletResponse
- HttpSession
- ServletContext
- ServletConfig
You use WebContext
like this:
import uk.ltd.getahead.dwr.WebContext;
import uk.ltd.getahead.dwr.WebContextFactory;
///
WebContext ctx = WebContextFactory.get();
req = ctx.getHttpServletRequest();
It is important that you treat the HTTP request and response as read-only. While HTTP headers might get through OK, there is a good chance that some browsers will ignore them (IE ignores cache pragmas for example) Any attempt to change the HTTP body WILL cause DWR errors.
WebContext uses a ThreadLocal variable so you can use the line above anywhere in your code (so long as it has been fired off by DWR).
See also the JavaDoc for DWR in general, or the specific page for WebContext.
WebContext replaces ExecutionContext which is deprecated as of DWR 1.1.
Alternative Method
It is possible to get access to the HTTP servlet objects without writing code that depends on DWR - just have the needed parameter (i.e. HttpServletRequest, HttpServletResponse, HttpSession, ServletContext or ServletConfig) declared on your method. DWR will not include it on the generated stub and upon a call of the method it will fill it in automagically.
For example if you have remoted a class like this:
public class Remote {
public void method(int param, ServletContext cx, String s) { ... }
}
Then you will be able to access it from Javascript just as though the ServletContext
parameter was not there:
Remote.method(42, "test", callback);
DWR will do the work of filling in the parameter for you.
There is one slight caveat with this method. You should ensure you are not using the 'callback function as first parameter' idiom, instead use the 'callback as last parameter' or 'callback in meta-data object' idioms. See the scripting introduction
相关推荐
springboot整合dwr实现js调用java方法,页面支持freemarker和jsp
DWR中各种java方法的调用,DWR应用
DWR实现java类转换成JavaScript类的小例子。
怎么将利用DWR调用JAVA类方法所返回的ArrayList数组 赋值给 logiciterate 迭代器?.txt怎么将利用DWR调用JAVA类方法所返回的ArrayList数组 赋值给 logiciterate 迭代器?.txt怎么将利用DWR调用JAVA类方法所返回的...
dwr 的简单应用,包括前台调用后台,后台调用js
dwr dwr项目 js 调用 java 后台 方法 js调用java后台方法 后台方法 异步
DWR是一个Java库,能够使浏览器中的JavaScript和服务器上的Java进行互动,并尽可能简单地调用Java的方法。本文档讲一下关于这个框架的入门配置及应用,服务器采用tomcat
dwr调用带集合返回值的方法,dwr调用带集合返回值的方法
自己总结的一个的dwr的使用说明,附有源码,以及代码的详细解释,配置文件的详细解释。完全可以运行。同时介绍了与其类似用法的dotnet版本的ajax组建。需要自己下载dwr.jar 3.0版本。或者到我的空间下载dwr.jar 3.0...
这是一个DWR的案例,通过DWR框架调用java代码的实例!有空拿过去看看!
该工程是dwr推送以及javascript动态调用java后台代码的实例
ext js配合dwr在java中的用法
利用DWR可以在客户端利用JavaScript直接调用服务端的Java方法并返回值给JavaScript就好像直接本地客户端调用一样(DWR根据Java类来动态生成JavaScrip代码). 它供给那些想要以一种简单的方式使用AJAX和XMLHttpRequest...
dwr 各种调用方法 不同类型
dwr简单例子,调用后台java类返回List,List中存放自定义的javabean
该压缩包中包含了dwr的使用及配置,和dwr的jar包; DWR采取了一个类似AJAX的新方法来动态生成基于JAVA类的JavaScript代码.这样WEB开发人员就可以在JavaScript里使用Java代码就像它们是浏览器的本地代码(客户端代码)...
dwr, java ajax 应用框架,可以将Java对象直接export到ie客户端,成为javascript对象,非常方便js和服务器端的交互,如果你还没有用,就是一下。太方便了呀。
通过DWR实现在HTML和Java Script中直接调用服务端的Java Bean
Packt.DWR.Java.AJAX.Applications