首页 技术 正文
技术 2022年11月14日
0 收藏 683 点赞 4,120 浏览 3161 个字

GWT RPC

GWT RPC
Remote Procedure Calls

GWT:

  Google Web Toolkit的缩写,有了 GWT可以使用 Java 编程语言编写 AJAX 前端,然后 GWT 会交叉编译到优化的JavaScript 中,而 JavaScript 可以自动在所有主要浏览器上运行。GWT允许开发人员使用 Java 编程语言快速构建和维护复杂但性能高的 JavaScript 前端应用程序,从而降低了开发难度,尤其是与 Eclipse Google 插件结合使用时,优势更明显

GWT 通过非常简单的 RPC 与服务器通信

  GWT支持一组开放的传输协议,例如 JSON 和 XML,但 GWT RPC 使所有 Java 通信都特别轻松且有效。类似于传统 JavaRMI,只需创建一个用于指定您要调用的远程方法的接口。从浏览器调用远程方法时,GWT RPC将自动串行化参数,并调用服务器上的适当方法,然后反串行化客户端代码的返回值。GWT RPC也将非常成熟,其可以处理多态类层次结构、对象图循环,甚至可以跨网抛出异常

创建GWT RPC连接的三个步骤:
1:创建接口继承RemoteServive接口,并且创建自定义的RPC接口方法—服务接口

2:创建指定Servlet服务,通过继承RemoteServiceServlet,并且实现1中创建的接口,并实现自定义方法

3:创建异步接口,与类名,方法名,参数相关的方式去创建接口方法,然后使用AsyncCallback<String> arg的方

式,给接口方法添加参数,用于回调函数.注意方法返回void

注意命名规范
服务接口与异步接口中类名必须符合规范
MyService —实现的Servlet命名为MyServiceImpl
MyServiceAsync –异步接口

在定义异步接口的方法时,必须保证方法名,参数名一致的情况下,在参数部分再添加一个AsyncCallback参数

远程的Servlet需要进行配置注解,以及需要在web.xml中进行配置Servlet,注意注解值需要和url-pattern一样
@RemoteServiceRelativePath(“myService”).

<servlet>
<servlet-name>myServiceImpl</servlet-name>
<servlet-class>com.example.foo.server.MyServiceImpl</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myServiceImpl</servlet-name>
<url-pattern>/com.example.foo.Foo/myService</url-pattern>
</servlet-mapping>

如果module在xml文件中使用了别名(rename-to attribute),那么可以不用包名

特别注意:需要将gwt-servlet.jar拷贝到web-inf/lib目录下,以及将java输出目录为/WEB-INF/classes.

编写客户端代码进行调用RPC:
创建的方式还是为GWT.create()

1:声明一个RPC接口对象的实例
MyEmailServiceAsync emailService = (MyEmailServiceAsync) GWT.create(MyEmailService.class);

2:编写对应回调方法的实例
AsyncCallback callback = new AsyncCallback() {
public void onSuccess(Void result) {
// do some UI stuff to show success
}

public void onFailure(Throwable caught) {
// do some UI stuff to show failure
}
};

3:调用接口实例的方法
emailService.emptyMyInbox(fUsername, fPassword, callback);

也可以直接使用参数中加入匿名实例的方式进行调用

使用集合时,需要使用@gwt.typeArgs <java.lang.String> 注释指明返回结果,参数集合类型

List reverseListAndConvertToStrings(List c);

监听RPC中的异常—包括 Checked Exceptions 和Unexpected Exceptions

手动发起异步的http请求—可用于json
Using HTTP in GWT

1:需要在xml文件加载<inherits name=”com.google.gwt.http.HTTP” />

2:RequestBuilder是用于HTTP请求的核心类

RequestBuilder使用的方法
1:创建一个对象的实例
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, URL.encode(url));
构造方法的参数为:请求方式,URL地址…(URL.encode()用于过滤URL)

2:创建对应的监听函数对象 Request对象
Request request = builder.sendRequest(null, new RequestCallback() {
public void onError(Request request, Throwable exception) {
// Couldn’t connect to server (could be timeout, SOP violation, etc.)
} //错误监听

public void onResponseReceived(Request request, Response response) {
if (200 == response.getStatusCode()) {
// Process the response in response.getText()
} else {
// Handle the error. Can get the status text from response.getStatusText()
}
} //接收到返回信息
});

接收后可以使用Response.getText(),来获取内容.如果是xml或者json可以使用类库进行转换

在RPC中调用时,如果有返回值,可以使用onSuccess()方法的重载来实现
calService.getPeople(startRow, maxRows, new AsyncCallback<Person[]>() {

// When the RPC returns, this code will be called if the RPC fails
public void onFailure(Throwable caught) {
statusLabel.setText(“Query failed: ” + caught.getMessage());
acceptor.failed(caught);
}

// When the RPC returns, this code is called if the RPC succeeds
public void onSuccess(Person[] result) {
lastStartRow = startRow;
lastMaxRows = maxRows;
lastPeople = result;
pushResults(acceptor, startRow, result);
statusLabel.setText(“Query reutrned ” + result.length + ” rows.”);
}
});

转自:http://hi.baidu.com/untra_/item/0942c0311ea36ee0a884289d

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902