【问题标题】:GWT - Main steps for this codeGWT - 此代码的主要步骤
【发布时间】:2010-09-28 15:39:15
【问题描述】:

我今天在 GWT 框架上迈出了第一步。我需要了解(使用 netbeans 官方 tutorial 这个应用程序是如何工作的 :) 我放置代码:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.yournamehere.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * Main entry point.
 *
 * @author djfonplaz
 */
public class MainEntryPoint implements EntryPoint {
    /** 
     * Creates a new instance of MainEntryPoint
     */
    public MainEntryPoint() {
    }

    public static GWTServiceAsync getService() {
        // Create the client proxy. Note that although you are creating the
        // service interface proper, you cast the result to the asynchronous
        // version of the interface. The cast is always safe because the
        // generated proxy implements the asynchronous interface automatically.

        return GWT.create(GWTService.class);
    }

    public void onModuleLoad() {
        final Label quoteText = new Label();

        Timer timer = new Timer() {
            public void run() {
                //create an async callback to handle the result:
                AsyncCallback callback = new AsyncCallback() {
                    public void onFailure(Throwable arg0) {
                        //display error text if we can't get the quote:
                        quoteText.setText("Failed to get a quote");
                    }

                    public void onSuccess(Object result) {
                        //display the retrieved quote in the label:
                        quoteText.setText((String) result);
                    }
                };
                getService().myMethod(callback);
            }
        };

        timer.scheduleRepeating(1000);
        RootPanel.get().add(quoteText);
    }
}

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.yournamehere.client;

import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;

/**
 *
 * @author djfonplaz
 */
@RemoteServiceRelativePath("gwtservice")
public interface GWTService extends RemoteService {
    public String myMethod();
}

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.yournamehere.client;

import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 *
 * @author djfonplaz
 */
public interface GWTServiceAsync {
    public void myMethod(AsyncCallback callback);
}

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.yournamehere.server;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.yournamehere.client.GWTService;

/**
 *
 * @author djfonplaz
 */
public class GWTServiceImpl extends RemoteServiceServlet implements GWTService {
    private Random randomizer = new Random();
    private static final long serialVersionUID = -15020842597334403L;
    private static List quotes = new ArrayList();

    static {
        quotes.add("No great thing is created suddenly - Epictetus");
        quotes.add("Well done is better than well said - Ben Franklin");
        quotes.add("No wind favors he who has no destined port - Montaigne");
        quotes.add("Sometimes even to live is an act of courage - Seneca");
        quotes.add("Know thyself - Socrates");
    }

    public String myMethod() {
        return (String) quotes.get(randomizer.nextInt(5));
    }
}

所以(或多或少):

  1. 标准的welcomeGWT.html被提供给服务器,直接调用JS创建的servlet MainEntryProject.java
  2. MainEntryProject.java(加载时通过 onModuleLoad())应该生成字符串并发送到客户端。

就在此时?

我不明白的是:

  1. 谁调用了 GWTServiceImpl 中的方法 myMethod()?没有人问这个方法,我只看到 getService().myMethod(callback),它应该调用 GWTServiceAsync 类。
  2. 谁将 GWTServiceImpls 生成的字符串传递给 public void onSuccess(Object result)?
  3. 为什么 getService() 返回 GWTService 而不是 GWTServiceImpl?它应该返回一个类,而不是一个接口;

如果有人可以帮助我,我会很高兴!干杯

【问题讨论】:

    标签: gwt


    【解决方案1】:

    要了解发生了什么,重要的是 GWT 使用 Generators 创建该服务的实际客户端实现。使用这种技术,可以生成通常必须自己编写的代码。自动为您生成整个 RPC 调用。
    接口 GWTService:这只是您对服务外观的定义
    接口 GWTServiceAsync:这是在您的服务的自动生成的客户端部分中实现的接口。
    class GWTServiceImpl:这是在服务器端运行的代码。

    因此,当您调用 GWT.create(GWTService.class); 时,您会得到一个自动生成的类实例。如果您真的对发生的事情感兴趣,您必须查看生成器的实现。
    这或多或少是您手动执行的操作:
    - 序列化(谷歌为此使用非标准方法,它可以通过不同的 GWT 版本进行更改)
    - 使用序列化数据设置请求
    - 发送请求并等待响应
    - 反序列化respone并调用通过回调返回结果

    【讨论】:

    • 是的,这发生在主要的 Java 应用程序上。但至少,有一个类可以获取和发送值处理方法。在这个框架上,我不明白这怎么可能:)
    【解决方案2】:

    文件 MainEntryProject.java 位于客户端包中,因此它不是 servlet - 它是由 GWT 编译为 JavaScript 的 java 文件。生成的 javascript 嵌入在您的 HTML 文件 (welcomeGWT.html) 中。 所以,

    • 首先加载welcomeGWT.html
    • 然后浏览器开始执行GWT生成的javascript,1000ms后调用服务器方法myMethod
    • 终于服务器返回回调,客户端执行下一段代码:

    public void onFailure(Throwable arg0) {
    quoteText.setText("Failed to get a quote");
    }
    public void onSuccess(Object result) {
    quoteText.setText((String) result);
    }

    答案:
    1) 客户端调用该方法。
    2) 客户
    3)我只能猜测,似乎这就是 RPC 滚动的方式

    【讨论】:

    • 嗯...好吧!我只是觉得它很奇怪嘿嘿(事实上我根本不知道这个框架,我想嘿嘿)。那么,调用GWTServiceAsync的“抽象”方法会自动调用写在GWTServiceImpl上的那个方法的实现吗?之后,我可以使用实例“结果”管理该方法返回的内容?
    • 是的,如果您从 YourServiceAsync 服务调用方法,它将导致从 YourServiceImpl 文件调用服务器方法。认为这篇文章应该解释其他一切:code.google.com/intl/en/webtoolkit/doc/1.6/…
    • 并像往常一样在YourService接口中定义的返回类型,AsyncCallback参数中的YourServiceAsync如:“AsyncCallback回调”。然后在YourServiceImpl方法中的服务器端:“public String myMethod(String s){...}”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    相关资源
    最近更新 更多