`
fishyu0817
  • 浏览: 111192 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Google Web Toolkit (GWT) MVP Example (3)

阅读更多

GreetingResponsePresenter.java

view source
<object id="highlighter_476763_clipboard" title="copy to clipboard" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="16" height="16" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" type="application/x-shockwave-flash"> </object>
print?
01.package co.uk.hivedevelopment.greet.client.mvp;
02.import net.customware.gwt.presenter.client.EventBus;
03.import net.customware.gwt.presenter.client.place.Place;
04.import net.customware.gwt.presenter.client.place.PlaceRequest;
05.import net.customware.gwt.presenter.client.widget.WidgetDisplay;
06.import net.customware.gwt.presenter.client.widget.WidgetPresenter;
07.import co.uk.hivedevelopment.greet.shared.event.GreetingSentEvent;
08.import co.uk.hivedevelopment.greet.shared.event.GreetingSentEventHandler;
09.import com.allen_sauer.gwt.log.client.Log;
10.import com.google.gwt.event.dom.client.ClickEvent;
11.import com.google.gwt.event.dom.client.ClickHandler;
12.import com.google.gwt.event.dom.client.HasClickHandlers;
13.import com.google.gwt.user.client.ui.DialogBox;
14.import com.google.gwt.user.client.ui.HasHTML;
15.import com.google.gwt.user.client.ui.HasText;
16.import com.google.inject.Inject;
17.public class GreetingResponsePresenter extends WidgetPresenter {
18. public interface Display extends WidgetDisplay {
19.  HasText getTextToServer();
20.  HasHTML getServerResponse();
21.  HasClickHandlers getClose();
22.  DialogBox getDialogBox();
23. }
24. public static final Place PLACE = new Place("GreetingResponse");
25. @Inject
26. public GreetingResponsePresenter(final Display display, final EventBus eventBus) {
27.  super(display, eventBus);
28.  bind();
29. }
30.  
31. @Override
32. protected void onBind() {
33.  // Add a handler to close the DialogBox
34.  display.getClose().addClickHandler(new ClickHandler() {
35.   public void onClick(final ClickEvent event) {
36.    display.getDialogBox().hide();
37.    // Not sure of a nice place to put these!
38.//    sendButton.setEnabled(true);
39.//    sendButton.setFocus(true);
40.   }
41.  });
42.  eventBus.addHandler(GreetingSentEvent.TYPE, new GreetingSentEventHandler() {
43.   @Override
44.   public void onGreetingSent(final GreetingSentEvent event) {
45.    Log.info("Handling GreetingSent event");
46.     
47.    display.getTextToServer().setText(event.getName());
48.    display.getServerResponse().setHTML(event.getMessage());
49.    display.getDialogBox().show();
50.   }
51.  });
52. }
53. @Override
54. protected void onUnbind() {
55.  // Add unbind functionality here for more complex presenters.
56. }
57. public void refreshDisplay() {
58.  // This is called when the presenter should pull the latest data
59.  // from the server, etc. In this case, there is nothing to do.
60. }
61. public void revealDisplay() {
62.  // Nothing to do. This is more useful in UI which may be buried
63.  // in a tab bar, tree, etc.
64. }
65. /**
66.  * Returning a place will allow this presenter to automatically trigger when
67.  * '#GreetingResponse' is passed into the browser URL.
68.  */
69. @Override
70. public Place getPlace() {
71.  return PLACE;
72. }
73. @Override
74. protected void onPlaceRequest(final PlaceRequest request) {
75.  // this is a popup
76. }
77.}

At this point, we're still missing some code but hopefully you should start to see the structure coming together.

Events

Since this is a simple application, we only have one event - the GreetingSent event which has a corresponding handler:

GreetingSent.java

view source
<object id="highlighter_883694_clipboard" title="copy to clipboard" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="16" height="16" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" type="application/x-shockwave-flash"> </object>
print?
01.package co.uk.hivedevelopment.greet.shared.event;
02.import com.google.gwt.event.shared.GwtEvent;
03.public class GreetingSentEvent extends GwtEvent{
04. public static Type TYPE = new Type();
05.  
06. private final String name;
07. private final String message;
08.  
09. public GreetingSentEvent(final String name, final String message) {
10.  this.name = name;
11.  this.message = message;
12. }
13.  
14. public String getName() {
15.  return name;
16. }
17.  
18. public String getMessage() {
19.  return message;
20. }
21.  
22. @Override
23. public Type getAssociatedType() {
24.  return TYPE;
25. }
26. @Override
27. protected void dispatch(final GreetingSentEventHandler handler) {
28.  handler.onGreetingSent(this);
29. }
30.}

GreetingSentHandler.java

view source
<object id="highlighter_65598_clipboard" title="copy to clipboard" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="16" height="16" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" type="application/x-shockwave-flash"> </object>
print?
1.package co.uk.hivedevelopment.greet.shared.event;
2.import com.google.gwt.event.shared.EventHandler;
3.public interface GreetingSentEventHandler extends EventHandler {
4. void onGreetingSent(GreetingSentEvent event);
5.}

If you now look at the project references for the event and handler you can see where the events are fired and subsequently handled. The components are blissfully unaware of what produced the event and it just has the information that it needs to get the job done. Now imagine if you want to have another component that also reacts to this event, say to update another part of the GUI. Simple, just register another event handler - no spaghetti code.

RPC

Let's define client RPC. As mentioned earlier, we'll not be making the RPC calls directly. Instead we'll use the command pattern and let the gwt-dispatch library handle the underlying server calls. Although this is an implementation of the command pattern, it turns out that there is already a core GWT class called Command, so the authors of the gwt-dispatch library have opted to use Action instead - so "actions" are really "commands".

In our example, we'll define the Action SendGreeting which represent our server request and a SendGreetingResult class to encapsulate the server response:

SendGreeting.java

view source
<object id="highlighter_760712_clipboard" title="copy to clipboard" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="16" height="16" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" type="application/x-shockwave-flash"> </object>
print?
01.package co.uk.hivedevelopment.greet.shared.rpc;
02.import net.customware.gwt.dispatch.shared.Action;
03.public class SendGreeting implements Action {
04. private static final long serialVersionUID = 5804421607858017477L;
05. private String name;
06. @SuppressWarnings("unused")
07. private SendGreeting() {
08. }
09. public SendGreeting(final String name) {
10.  this.name = name;
11. }
12. public String getName() {
13.  return name;
14. }
15.}

SendGreetingResult.java

view source
<object id="highlighter_9512_clipboard" title="copy to clipboard" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="16" height="16" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" type="application/x-shockwave-flash"> </object>
print?
01.package co.uk.hivedevelopment.greet.shared.rpc;
02.import net.customware.gwt.dispatch.shared.Result;
03.public class SendGreetingResult implements Result {
04. private static final long serialVersionUID = 7917449246674223581L;
05. private String name;
06. private String message;
07. public SendGreetingResult(final String name, final String message) {
08.  this.name = name;
09.  this.message = message;
10. }
11. @SuppressWarnings("unused")
12. private SendGreetingResult() {
13. }
14. public String getName() {
15.  return name;
16. }
17. public String getMessage() {
18.  return message;
19. }
20.}
分享到:
评论

相关推荐

    Google Web工具包(GWT)编程手册The Google Web Toolkit (GWT) Programming Cookbook

    Google Web Toolkit(GWT)是一个用于开发和优化复杂浏览器端应用的开源工具集,它允许开发者使用Java语言编写前端代码,然后通过编译器将Java代码转换成兼容各主流浏览器的JavaScript、HTML和CSS。《Google Web工具...

    Google Web Toolkit gwt-2.10.0 最新版本

    Google Web Toolkit(GWT)是Google推出的一款强大的Web开发框架,专注于帮助开发者使用Java语言创建高性能、跨浏览器的Web应用程序。GWT g-2.10.0是该框架的一个重要版本更新,提供了许多增强的功能和优化,以提升...

    GWT(Google Web Toolkit)

    GWT(Google Web Toolkit) 是 Google 最近推出的一个开发 Ajax 应用的框架,它支持用 Java 开发和调试 Ajax 应用,本文主要介绍如何利用 GWT 进行 Ajax 的开发。 GWT特性简介  1.动态,可重用的UI组件  GWT提供的...

    gwt-2.8.2 SDK 最新下载 google web toolkit

    Google Web Toolkit(GWT)是Google推出的一款开源的、基于Java的Web开发框架,它允许开发者使用Java语言来编写前端应用程序。GWT-2.8.2是该SDK的一个版本,提供了最新的特性和改进,旨在简化Web应用的开发流程,...

    Google Web Toolkit 入门

    ### Google Web Toolkit (GWT) 入门指南 #### 一、引言 随着网络技术的发展,用户对Web应用的期望越来越高,不仅要求其功能强大,还希望具有良好的交互性和用户体验。为此,一种名为Ajax(Asynchronous JavaScript...

    Google Web Toolkit Applications

    《Google Web Toolkit Applications》这本书是针对Google Web Toolkit(GWT)这一强大开发工具的深入指南。GWT是一款由Google开发的开源JavaScript框架,它允许开发者使用Java语言来编写Web应用程序,然后自动编译成...

    Google Web Toolkit 开发 Ajax

    ### Google Web Toolkit (GWT) 开发 Ajax 技术详解 #### 一、GWT特性简介 **GWT**(Google Web Toolkit)是Google推出的一款用于构建和优化复杂Web前端应用的开发工具包。它通过提供一系列强大的特性,极大地简化...

    ajax例子,Google Web Toolkit 1.0.21-ajax example, Google Web Toolkit 1.0.21

    在"ajax例子,Google Web Toolkit 1.0.21-ajax example"中,我们可以预期找到的是一个使用GWT 1.0.21构建的Ajax示例应用。这个示例可能展示了如何使用GWT库创建异步通信,以及如何处理服务器返回的数据。通过学习这个...

    Using Google Web Toolkit (GWT) for Developing AJAX-Based Web Applications for the SAP NetWeaver J2EE Framework.pdf

    ### 使用Google Web Toolkit (GWT) 开发基于AJAX的SAP NetWeaver J2EE框架Web应用 #### 概述 本文档旨在提供一种利用Google Web Toolkit (GWT) 在SAP NetWeaver J2EE框架下开发AJAX基础Web应用的方法。SAP ...

    Google Web Toolkit开发实战

    谷歌Web工具包(Google Web Toolkit,简称GWT)是一种开源的Java框架,它允许开发者使用Java语言编写客户端的Web应用程序,然后自动编译为优化过的JavaScript代码。GWT的核心理念是利用Java的强类型、面向对象的特性...

    利用 Google Web Toolkit 在 Java 框架中开发 Ajax 应用程序

    Google Web Toolkit(GWT)是主要工具,它允许开发者使用 Java 语言来编写前端的 AJAX 应用程序。Ajax,即异步 JavaScript 和 XML,是一种用于创建快速互动网页的技术。通过 GWT,Java 开发者无需深入学习 ...

    Google-Web-Toolkit (GWT)

    ### Google Web Toolkit (GWT):工作原理与应用实例 #### 一、GWT简介 Google Web Toolkit(GWT)是一种强大的开发框架,用于构建基于AJAX的Web应用程序。GWT的主要特点在于它允许开发者完全使用Java语言进行前端...

    Google Web Toolkit API 文档

    Google Web Toolkit(GWT)是Google推出的一款开源的JavaScript开发框架,它允许开发者使用Java语言来编写前端Web应用。GWT API文档是开发者理解和使用GWT进行开发的重要参考资料,提供了全面的技术指南和API参考。 ...

    google web toolkit 1.5.3

    Google Web Toolkit(GWT)1.5.3是一款由Google开发的开源JavaScript开发框架,它允许Java开发者使用Java语言来构建高性能、跨浏览器的Web应用程序。这个版本是GWT的一个重要里程碑,带来了许多改进和新特性,使得...

    Accelerated GWT: Building Enterprise Google Web Toolkit Applications

    《加速GWT:构建企业级Google Web Toolkit应用》是一本深度探讨如何利用Google Web Toolkit(GWT)构建高性能Ajax应用程序的专业书籍。本书作者Vipul Gupta深入解析了GWT的核心功能,以及如何通过GWT生成优化的...

    面向 Java 开发人员的 Ajax 探索 Google Web Toolkit

    Google Web Toolkit(GWT)是一款由Google推出的开源框架,专为Java开发者设计,旨在简化和加速Web应用程序的开发过程。它允许开发者使用Java语言编写前端代码,并将其编译成高性能的JavaScript,从而在浏览器上运行...

    面向 Java 开发人员的 Ajax: Google Web Toolkit 入门(GWT入门)

    **Java开发人员的Ajax:Google Web Toolkit (GWT) 入门** Google Web Toolkit (GWT) 是一个强大的工具,它允许Java开发人员使用熟悉的Java语言来构建高性能、跨浏览器的Ajax应用程序。GWT通过将Java代码编译为优化...

    GWT (GOOGLE WEB TOOLKIT)介绍PPT

    **GWT (Google Web Toolkit)** 是一款由Google开发的开源工具包,专为Java开发者设计,使得他们能够使用Java语言创建高效、动态且交互性强的Ajax应用。GWT通过将Java代码编译成浏览器可执行的JavaScript和HTML,解决...

    Manning - Gwt In Action Easy Ajax With The Google Web Toolkit

    Google Web Toolkit(GWT)

Global site tag (gtag.js) - Google Analytics