http://roberthanson.blogspot.com/2006/06/trivial-gwt-example.html
找了一个GWT RPC的例子,试着做了一下,效果挺好,呵呵.
目录结构如下:
./org/hanson/gwt/MyApplication.gwt.xml
./org/hanson/gwt/public/MyApplication.html
./org/hanson/gwt/client/MyApplication.java
./org/hanson/gwt/client/MyService.java
./org/hanson/gwt/client/MyServiceAsync.java
./org/hanson/gwt/server/MyServiceImpl.java
在此项目的根目录下有一个MyApplication.gwt.xml文件,这是GWT项目的配置文件.
<module>
<inherits name='com.google.gwt.user.User'/>
<entry-point class='org.hanson.gwt.client.MyApplication'/>
<servlet path="/myService"
class="org.hanson.gwt.server.MyServiceImpl"/>
</module>
下面是public文件夹下面的HTML文件,用来做hosted模式测试时用的.这是GWT提供的一种特殊的测试方法.
<html>
<head>
<title>Wrapper HTML for MyApplication</title>
<meta name='gwt:module'
content='org.hanson.gwt.MyApplication' />
</head>
<body>
<script language="javascript" src="gwt.js"></script>
</body>
</html>
client目录下有三个文件,被GWT用来转换JAVA类文件为JS文件的.这些文件的位置你可以自己进行配置.有一个主应用程序MyApplication以及两个接口用来远程调用服务器端,下面是所有代码.
[./org/hanson/gwt/client/MyService.java]
package org.hanson.gwt.client;
import com.google.gwt.user.client.rpc.RemoteService;
public interface MyService extends RemoteService
{
public String myMethod (String s);
}
[./org/hanson/gwt/client/MyServiceAsync.java]
package org.hanson.gwt.client;
import com.google.gwt.user.client.rpc.AsyncCallback;
public interface MyServiceAsync
{
public void myMethod(String s, AsyncCallback callback);
}
[./org/hanson/gwt/client/MyApplication.java]
package org.hanson.gwt.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;
/**
* Entry point classes define onModuleLoad()
.
*/
public class MyApplication implements EntryPoint
{
public void onModuleLoad ()
{
// define the service you want to call
MyServiceAsync svc =
(MyServiceAsync) GWT.create(MyService.class);
ServiceDefTarget endpoint = (ServiceDefTarget) svc;
endpoint.setServiceEntryPoint("/myService");
// define a handler for what to do when the
// service returns a result
AsyncCallback callback = new AsyncCallback() {
public void onSuccess (Object result)
{
RootPanel.get().add(new HTML(result.toString()));
}
public void onFailure (Throwable ex)
{
RootPanel.get().add(new HTML(ex.toString()));
}
};
// execute the service
svc.myMethod("Do Stuff", callback);
}
}
[./org/hanson/gwt/server/MyServiceImpl.java]
package org.hanson.gwt.server;
import org.hanson.gwt.client.MyService;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
public class MyServiceImpl
extends RemoteServiceServlet
implements MyService
{
public String myMethod (String s)
{
return "You sent us '" + s + "'";
}
}
还有两个可执行文件:我都是自己写的....我觉得这些东西如果用GWT应该可以生成,不过我是自己在文本编辑器里一步步写的,包括上面的代码.
MyApplication-compile.cmd编译这个工程的文件,最后会生成一个www目录,里面就是我们想要的JS文件和HTML文件,你可以把这些文件直接考到你自己的WEB应用程序目录下,就可以用的.
MyApplication-shell.cmd这是用GWT的HOSTED模式测试时用到的,它会调用上面的MyApplication.html文件.在写这个文件时,注意里面有个BIN的路径,是你的SRC文件编译后的路径,如果写错的话,会运行不起来的.
好久没用EJB写东西了,看了这一套流程后.忽然感觉有点回到EJB的风格了...呵呵.远程调用的形式基本是一样的.只是这里用了JS来提高效率.