GWT简单应用-RPC篇(奥运奖牌版小偷)
来源:互联网 发布:java 线程池面试题 编辑:程序博客网 时间:2023/12/03 09:36
最近在公司接触了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 { |
接下来新建包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,我们一起讨论
运行结果:
- GWT简单应用-RPC篇(奥运奖牌版小偷)
- 奥运奖牌中文系统(IO版)
- 奥运奖牌计数
- 奥运奖牌中文信息系统
- 07:奥运奖牌计数
- 7. 奥运奖牌计数
- GWT RPC
- [奥运] 28届奥运会中国队奖牌一览
- 一道关于奥运奖牌排序的题
- 【奥运奖牌大竞猜】CSDN网友奥运大联欢(二)
- RPC-Thrift简单应用
- GWT - GWT Designer开发Ajax应用 (05) 利用RPC传递对象
- GWT - GWT Designer开发Ajax应用 (05) 利用RPC传递对象
- 程序设计实习MOOC / 程序设计与算法(一)第三周测验(2017冬季)8:奥运奖牌计数
- GWT RPC 开发
- [英语阅读]阿联酋重金奖励夺奥运奖牌运动员
- 奥运奖牌榜的小调用,是新浪的。
- rabbitmq简单应用-RPC模式
- 微软的面试题及答案-超变态但是很经典
- 问题解决:SQLException: org.apache.tomcat.dbcp.dbcp.DelegatingStatement with address: "com.mysql.jdbc.Statement@100363" is closed.
- powerbuilder的dw中使用graph风格,当横轴是日期时,如何显示才能完整显示日期?
- MFC 对话 模式和非模式显示
- DWR之入门学习笔记
- GWT简单应用-RPC篇(奥运奖牌版小偷)
- 用VC纯资源dll制作多语言界面程序
- [Perl]One-liner
- 对CreateCompatibleDC的认识
- 交流
- 总觉得在Perl里面读取文本文件应该用个循环:
- 最新版本下载(2008-8-20)
- 安装Perl模块
- 设置FreeBSD的终端色彩和设置开机Splash