最近在公司接触了GWT框架的学习,在网上找了下例子不是很多。下面分享一个示例给大家:
-----适合初学者
如何下载安装、配置环境不说了
第一步 新建工程和应用
D:/project>projectCreator -eclipse GwtProject
D:/project>applicationCreator -eclipse GwtProject com.test.gwt.client.GwtDemoEntry
第二步 导入IDE(eclipse) 选择File/Import 选择Existing Projects into Workspace 点击Next 选择Select root directory 点击Browse选择D:/project/
第三步 正式在eclipse开发
大家注意到我们这个工程里面有个com.test.gwt.server包,这个包里面包含有运行在服务器端的代码。我们从客户端发送的请求也是发送到这个包里面的一个Servlet上去的。
为了进行异步调用,在Client端必须定义一个继承自接口RemoteService的接口,在我们的这个例子中,我们定义了接口MyService,MyService的代码如清单4所示。
package com.test.gwt.client; import com.google.gwt.user.client.rpc.RemoteService; public interface MyService extends RemoteService { String getHtmlCode(); }
继承RemoteService,没有任何自身方法,我们需要自定义方法,在这里我们的方法是getHtmlCode();这个方法会在服务器端的代码中实现。也就是我们采用异步调用方式所能够调用的方法。
在声明完这个接口之后,我们还必须声明另外一个异步调用接口,在我们的例子中是接口MyServiceAsync,这个接口里声明的方法名称必须与上面定义的MyService接口里面的相同,但是多个一个类型为AsyncCallback的参数,还有返回类型一定只能是void
package com.test.gwt.client; import com.google.gwt.user.client.rpc.AsyncCallback; public interface MyServiceAsync { public void getHtmlCode(AsyncCallback callback); }
接下来新建包 com.test.gwt.server ,新建类MyServiceImpl,也就是我们要写具体实现异步调用的代码在此。记得一定要继承RemoteServiceServlet 类和实现前面定义的MyService 接口
package com.test.gwt.server; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.regex.Matcher; import java.util.regex.Pattern; import com.google.gwt.user.server.rpc.RemoteServiceServlet; import com.hotye.gwt.client.MyService; public class MyServiceImpl extends RemoteServiceServlet implements MyService { public String getHtmlCode() { String sCurrentLine; String sTotalString; String resultCode = null; sCurrentLine = ""; sTotalString = ""; InputStream l_urlStream; try { URL l_url = new java.net.URL("http://2008.qq.com/ "); HttpURLConnection l_connection = (java.net.HttpURLConnection) l_url .openConnection(); l_connection.connect(); l_urlStream = l_connection.getInputStream(); BufferedReader l_reader = new java.io.BufferedReader( new java.io.InputStreamReader(l_urlStream)); while ((sCurrentLine = l_reader.readLine()) != null) { sTotalString += sCurrentLine; } } catch (MalformedURLException ex) { } catch (IOException ex) { } String regEx = "(?=oly_bg_a13.png)([//s//S]*?)(?=<!--[if !IE]>|xGv00)"; Pattern p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE); Matcher m = p.matcher(sTotalString); while (m.find()) { resultCode = m.group(0).replaceAll("oly_bg_a13.png/" width=/"212/" height=/"35/" border=/"0/"></a></td> </tr> <tr> <td align=/"center/">", ""); } return resultCode; } }
接下来写入口的GwtDemoEntry类了,我们使用它来进行向server端的异步调用的。
package com.test.gwt.client; import com.google.gwt.core.client.EntryPoint; import com.google.gwt.core.client.GWT; import com.google.gwt.user.client.Event; import com.google.gwt.user.client.Window; import com.google.gwt.user.client.rpc.AsyncCallback; import com.google.gwt.user.client.rpc.ServiceDefTarget; import com.google.gwt.user.client.ui.Button; import com.google.gwt.user.client.ui.ClickListener; import com.google.gwt.user.client.ui.DialogBox; import com.google.gwt.user.client.ui.HTML; import com.google.gwt.user.client.ui.HTMLPanel; import com.google.gwt.user.client.ui.Image; import com.google.gwt.user.client.ui.PopupListener; import com.google.gwt.user.client.ui.PopupPanel; import com.google.gwt.user.client.ui.RootPanel; import com.google.gwt.user.client.ui.VerticalPanel; import com.google.gwt.user.client.ui.Widget; /** * @author jiang */ public class GwtDemoEntry implements EntryPoint { public void onModuleLoad() { final MyServiceAsync myService = (MyServiceAsync) GWT .create(MyService.class); ServiceDefTarget target = (ServiceDefTarget) myService; String staticResponseURL = GWT.getModuleBaseURL(); staticResponseURL += "/getHtmlCodeService"; target.setServiceEntryPoint(staticResponseURL); final Image image = new Image(); image.setUrl("http://www.google.com/images/logo.gif "); Button button = new Button("刷新数据"); final DialogBox box = new DialogBox(); box.setPopupPosition(400, 200); box.setHTML("<b><font color=red>数据正在获取中,按刷新按钮...</font></b>"); VerticalPanel vPanel = new VerticalPanel(); vPanel.addStyleName("widePanel"); vPanel.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER); image.setUrl("http://mat1.qq.com/2008/images/final/oly_bg_a13.png "); image.setSize("225", "20"); vPanel.add(image); vPanel.add(box); vPanel.add(button); RootPanel.get().add(vPanel); button.addClickListener(new ClickListener() { public void onClick(Widget sender) { myService.getHtmlCode(new AsyncCallback() { public void onFailure(Throwable caught) { box.setHTML("发生异常<br>"+caught.toString()); } public void onSuccess(Object result) { if(result!=null)box.setHTML(result.toString()); } }); } }); } }
接下来讲一下staticResonseURL += "/getHtmlCodeService"该行getHtmlCodeService 的意思吧。实际上这个URL最终会在web.xml文件中被mapping到servlet SampleServiceImpl上。我们的程序没有web.xml怎么办,我们只要在我们的入口配置文件中配置就可以了,如在本例中的GwtDemoEntry.gwt.xml配置如下:
<entry-point class='com.hotye.gwt.client.GwtDemoEntry'/><servlet class="com.test.gwt.server.MyServiceImpl" path="/getHtmlCodeService" />
如果有问题发邮件:jiangw.sky@gmail.com ,我们一起讨论
运行结果: