GWT RPC
Remote Procedure Calls前端
Google Web Toolkit的缩写,有了 GWT可使用 Java 编程语言编写 AJAX 前端,而后 GWT 会交叉编译到优化的JavaScript 中,而 JavaScript 能够自动在全部主要浏览器上运行。GWT容许开发人员使用 Java 编程语言快速构建和维护复杂但性能高的 JavaScript 前端应用程序,从而下降了开发难度,尤为是与 Eclipse Google 插件结合使用时,优点更明显web
GWT 经过很是简单的 RPC 与服务器通讯编程
GWT支持一组开放的传输协议,例如 JSON 和 XML,但 GWT RPC 使全部 Java 通讯都特别轻松且有效。相似于传统 JavaRMI,只需建立一个用于指定您要调用的远程方法的接口。从浏览器调用远程方法时,GWT RPC将自动串行化参数,并调用服务器上的适当方法,而后反串行化客户端代码的返回值。GWT RPC也将很是成熟,其能够处理多态类层次结构、对象图循环,甚至能够跨网抛出异常json
建立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服务器