我今天在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));
}
}
所以(或多或少):
此时此刻?
我不明白的是:
如果有人可以帮助我,真的很高兴!欢呼声
答案 0 :(得分:2)
要了解正在发生的事情,GWT使用Generators创建该服务的实际客户端实现非常重要。使用该技术,可以生成通常由您自己编写的代码。将自动为您生成整个RPC调用。
interface GWTService:这只是你对服务的定义
interface GWTServiceAsync:这是在服务的自动生成的客户端部分中实现的接口。
class GWTServiceImpl:这是在服务器端运行的代码。
因此,当您调用GWT.create(GWTService.class);
时,您将获得一个自动生成的类实例。如果您真的对发生的事情感兴趣,那么您必须查看Generator实现。
你或多或少会手动做什么:
- 序列化(谷歌使用非标准方法,它可以通过不同的GWT版本改变)
- 使用序列化数据设置请求
- 发送请求并等待响应
- 反序列化respone并通过回调调用返回结果
答案 1 :(得分:1)
文件MainEntryProject.java位于客户端软件包中,因此它不是servlet - 它是由GWT编译为JavaScript的java文件。导致HTML文件中嵌入的javascript(welcomeGWT.html)。 所以,
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滚动