【发布时间】: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));
}
}
所以(或多或少):
- 标准的welcomeGWT.html被提供给服务器,直接调用JS创建的servlet MainEntryProject.java
- MainEntryProject.java(加载时通过 onModuleLoad())应该生成字符串并发送到客户端。
就在此时?
我不明白的是:
- 谁调用了 GWTServiceImpl 中的方法 myMethod()?没有人问这个方法,我只看到 getService().myMethod(callback),它应该调用 GWTServiceAsync 类。
- 谁将 GWTServiceImpls 生成的字符串传递给 public void onSuccess(Object result)?
- 为什么 getService() 返回 GWTService 而不是 GWTServiceImpl?它应该返回一个类,而不是一个接口;
如果有人可以帮助我,我会很高兴!干杯
【问题讨论】:
标签: gwt