我不确定什么对您最有帮助。 Google 开发人员指南对我来说已经足够(至少当我在 1.6 版上开始使用它时)为我的 GWT 应用程序创建 RPC 服务。
通用APP
模块:是.gwt.xml 文件。是的,你会需要它。 GWT 编译器会自动找到它并尝试编译所有 GWT 代码(<source> 元素将告诉哪个子包包含将转换为 JS 的 Java 代码)。它还将告诉哪个类实现了 EntryPoint 接口。 onModuleLoad 将是 javascript 在客户端页面中运行时执行的代码。
RPC
嗯,你应该先尝试 UI 的东西,然后,当你有足够的信心时,再尝试服务器的东西。无论如何,方案是:
interface MyService extends RemoteService {
List<String> doSomething(String sample, int other);
}
@RemoteServiceRelativePath("../path/to/servlet") // see later
intercace MyServiceAsync {
void doSomething(String sample, int other, AsyncCallback<List<String>> callback);
}
这些是接口。稍后是异步的。这就是您将从客户端使用的内容。始终调用并传递 AsyncCallback 的实现,该实现将接收(稍后,您不知道何时)结果。
第一个接口是同步接口。这就是你需要在服务器上实现的。您必须从 RemoteServiceServlet 类继承(它是已经完成所有值处理的 servlet 的实现),并实现您的接口。 GWT 代码完成了其余的工作(几乎)。
public class ServiceImpl extends RemoteServiceServlet implements MyService
{
// implement the method normally
}
您需要从客户端创建服务代理:
private static MyServiceAsync MY_SERVICE = GWT.create(MyService.class);
是的。我知道 GWT 如何知道 MyserviceAsync 和 MyService 一起工作很奇怪。别担心。它有效:)
只需像这样使用服务:
MY_SERVICE.doSomething("value", 111, new AsyncCallback<List<String>>() {
// note that this code executes some time in the future when response from server is back
public void onSuccess(List<String> result) {
Window.alert("Server answered with " + result.size() + " elements!");
}
public void onFailure(Throwable t) {
Window.alert("Server failed: " + t.getMessage());
}
}
服务器路径
您必须配置您的应用程序以使该 servlet 实现侦听 @RemoteServiceRelativePath 中指示的 URL。这就是客户端知道在哪里发出请求,而服务器知道哪个 servlet 处理该请求的方式。我建议使用:
../my-service.gwt 作为相对路径(GWT 模块发布在<ROOT>/module_name
和
配置您的网络应用程序以使用 /my-service.gwt 的 servlet
但这完全取决于你的喜好:)
无论如何,我认为 Google 教程是最好的。所以请复制粘贴。尝试和修改,直到你了解整个事情。