我正在尝试为带有GWT. 我Google Web Application在Eclipse没有示例代码的情况下创建了一个,现在我想添加该服务,但开发人员 Google 指南对我没有帮助。我不确定在哪里添加界面以及它是如何工作的。
如果我正确理解了谷歌文档,我必须添加 amodule和 an entry point class,对吗?如果您能给我一些提示并帮助我如何创建 rpc 服务,那就太好了。
我正在尝试为带有GWT. 我Google Web Application在Eclipse没有示例代码的情况下创建了一个,现在我想添加该服务,但开发人员 Google 指南对我没有帮助。我不确定在哪里添加界面以及它是如何工作的。
如果我正确理解了谷歌文档,我必须添加 amodule和 an entry point class,对吗?如果您能给我一些提示并帮助我如何创建 rpc 服务,那就太好了。
如果您在 Eclipse 的“新建项目”向导中创建一个新的 GWT 项目,并选中“生成项目示例代码”,它将包括一个具有示例方法的功能齐全的 RPC 服务,然后您可以根据需要对其进行调整或复制。
我不确定什么对你最有帮助。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
和
配置您的 Web 应用程序以使用 servlet/my-service.gwt
但这完全取决于您的喜好:)
无论如何,我认为谷歌教程是最好的。所以请复制粘贴。尝试和修改,直到你了解整个事情。
Out of memory, don't have eclipse in front of me. First do create a test project with generated testcode, you can delete it afterward. Yes you will have to add a module. Create in client the two interfaces for the async calls, inherit it on server side. Hope I understood your question right.