zoukankan      html  css  js  c++  java
  • GWT RPC

    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

    我是这耀眼的瞬间,是划过天边的刹那火焰...
  • 相关阅读:
    Openwave V7 不支持中文的解决方法
    VBS的疑惑,它们不考虑效率吗?
    删除顽固 NTServic和webacc.exe病毒。
    我的电脑怎么多了一些乱七八糟的东西。
    阿怒再发,突然的发现,为了编码输入速度!
    庆祝开博,也算给自己加油!
    超级简单的工厂模式温度转换
    阿怒乱弹之VS05重构的提取方法操作不方便啊!
    随笔嘛!就是随便下笔~呵呵!
    Oracle数据库一样平常维护手册2
  • 原文地址:https://www.cnblogs.com/lijiews/p/lj201312170955.html
Copyright © 2011-2022 走看看