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

Google Web Toolkit (GWT) MVP Example (4)

阅读更多

At this point, the client code should start to compile but there is still more to do before we can run it. We need some utility classes for the dispatcher and Gin:

CachingDispatchAsync.java

view source
<object id="highlighter_932213_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;
02.import java.util.HashMap;
03.import java.util.Map;
04.import com.google.gwt.user.client.rpc.AsyncCallback;
05.import com.google.inject.Inject;
06.import net.customware.gwt.dispatch.client.DispatchAsync;
07.import net.customware.gwt.dispatch.shared.Action;
08.import net.customware.gwt.dispatch.shared.Result;
09./**
10.* Dispatcher which support caching of data in memory
11.*
12.*/
13.public class CachingDispatchAsync implements DispatchAsync {
14. private DispatchAsync dispatcher;
15. private Map, Result> cache = new HashMap, Result>();
16. @Inject
17. public CachingDispatchAsync(final DispatchAsync dispatcher) {
18.  this.dispatcher = dispatcher;
19. }
20. /*
21.  * (non-Javadoc)
22.  * @see net.customware.gwt.dispatch.client.DispatchAsync#execute(A, com.google.gwt.user.client.rpc.AsyncCallback)
23.  */
24. public , R extends Result> void execute(final A action, final AsyncCallback callback) {
25.  dispatcher.execute(action, callback);
26. }
27. /**
28.  * Execute the give Action. If the Action was executed before it will get fetched from the cache
29.  *
30.  * @param Action implementation
31.  * @param Result implementation
32.  * @param action the action
33.  * @param callback the callback
34.  */
35. @SuppressWarnings("unchecked")
36. public , R extends Result> void executeWithCache(final A action, final AsyncCallback callback) {
37.  final Result r = cache.get(action);
38.   
39.  if (r != null) {
40.   callback.onSuccess((R) r);
41.  }
42.  else {
43.   dispatcher.execute(action, new AsyncCallback() {
44.    public void onFailure(Throwable caught) {
45.     callback.onFailure(caught);
46.    }
47.    public void onSuccess(R result) {
48.     cache.put((Action) action, (Result) result);
49.     callback.onSuccess(result);
50.    }
51.   });
52.  }
53. }
54. /**
55.  * Clear the cache
56.  */
57. public void clear() {
58.  cache.clear();
59. }
60.}

GreetingClientModule.java

view source
<object id="highlighter_727479_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.gin;
02.import net.customware.gwt.presenter.client.DefaultEventBus;
03.import net.customware.gwt.presenter.client.EventBus;
04.import net.customware.gwt.presenter.client.gin.AbstractPresenterModule;
05.import net.customware.gwt.presenter.client.place.PlaceManager;
06.import co.uk.hivedevelopment.greet.client.CachingDispatchAsync;
07.import co.uk.hivedevelopment.greet.client.mvp.AppPresenter;
08.import co.uk.hivedevelopment.greet.client.mvp.GreetingPresenter;
09.import co.uk.hivedevelopment.greet.client.mvp.GreetingResponsePresenter;
10.import co.uk.hivedevelopment.greet.client.mvp.GreetingResponseView;
11.import co.uk.hivedevelopment.greet.client.mvp.GreetingView;
12.import com.google.inject.Singleton;
13.public class GreetingClientModule extends AbstractPresenterModule {
14. @Override
15. protected void configure() { 
16.  bind(EventBus.class).to(DefaultEventBus.class).in(Singleton.class);
17.  bind(PlaceManager.class).in(Singleton.class);
18.   
19.  bindPresenter(GreetingPresenter.class, GreetingPresenter.Display.class, GreetingView.class);
20.  bindPresenter(GreetingResponsePresenter.class, GreetingResponsePresenter.Display.class, GreetingResponseView.class);
21.   
22.  bind(AppPresenter.class).in(Singleton.class);
23.  bind(CachingDispatchAsync.class);
24. }
25.}

GreetingGinjector.java

view source
<object id="highlighter_347758_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.gin;
02.import net.customware.gwt.dispatch.client.gin.ClientDispatchModule;
03.import net.customware.gwt.presenter.client.place.PlaceManager;
04.import co.uk.hivedevelopment.greet.client.mvp.AppPresenter;
05.import com.google.gwt.inject.client.GinModules;
06.import com.google.gwt.inject.client.Ginjector;
07.@GinModules({ ClientDispatchModule.class, GreetingClientModule.class })
08.public interface GreetingGinjector extends Ginjector {
09. AppPresenter getAppPresenter();
10. PlaceManager getPlaceManager();
11.}

Now we tie all this together by replacing the existing module entry class with the following:

GreetMvp.java

view source
<object id="highlighter_318282_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;
02.import co.uk.hivedevelopment.greet.client.gin.GreetingGinjector;
03.import co.uk.hivedevelopment.greet.client.mvp.AppPresenter;
04.import com.google.gwt.core.client.EntryPoint;
05.import com.google.gwt.core.client.GWT;
06.import com.google.gwt.user.client.ui.RootPanel;
07.public class GreetMvp implements EntryPoint {
08. private final GreetingGinjector injector = GWT.create(GreetingGinjector.class);
09. public void onModuleLoad() {
10.  final AppPresenter appPresenter = injector.getAppPresenter();
11.  appPresenter.go(RootPanel.get());
12.  injector.getPlaceManager().fireCurrentPlace();
13. }
14.}

Server Components

Okay, now let's define the server side. We need to configure Guice and the dispatch handler plus we need to provide an implementation for the SendGreeting action.

SendGreetingHandler.java

view source
<object id="highlighter_877314_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.server.handler;
02.import javax.servlet.ServletContext;
03.import javax.servlet.http.HttpServletRequest;
04.import net.customware.gwt.dispatch.server.ActionHandler;
05.import net.customware.gwt.dispatch.server.ExecutionContext;
06.import net.customware.gwt.dispatch.shared.ActionException;
07.import org.apache.commons.logging.Log;
08.import co.uk.hivedevelopment.greet.shared.rpc.SendGreeting;
09.import co.uk.hivedevelopment.greet.shared.rpc.SendGreetingResult;
10.import com.google.inject.Inject;
11.import com.google.inject.Provider;
12.public class SendGreetingHandler implements ActionHandler<SendGreeting, SendGreetingResult> {
13. private final Log logger;
14. private final Provider<ServletContext> servletContext;
15. private final Provider<HttpServletRequest> servletRequest;
16. @Inject
17. public SendGreetingHandler(final Log logger,
18.       final Provider<ServletContext> servletContext,      
19.       final Provider<HttpServletRequest> servletRequest) {
20.  this.logger = logger;
21.  this.servletContext = servletContext;
22.  this.servletRequest = servletRequest;
23. }
24. @Override
25. public SendGreetingResult execute(final SendGreeting action,
26.       final ExecutionContext context) throws ActionException {
27.  final String name = action.getName();
28.    
29.  try {
30.   String serverInfo = servletContext.get().getServerInfo();
31.    
32.   String userAgent = servletRequest.get().getHeader("User-Agent");
33.    
34.   final String message = "Hello, " + name + "!
35.I am running " + serverInfo
36.     + ".
37.It looks like you are using:
38." + userAgent;
39.    
40.   //final String message = "Hello " + action.getName();
41.    
42.   return new SendGreetingResult(name, message);
43.  }
44.  catch (Exception cause) {
45.   logger.error("Unable to send message", cause);
46.    
47.   throw new ActionException(cause);
48.  }
49. }
50. @Override
51. public void rollback(final SendGreeting action,
52.        final SendGreetingResult result,
53.        final ExecutionContext context) throws ActionException {
54.  // Nothing to do here
55. }
56.  
57. @Override
58. public Class<SendGreeting> getActionType() {
59.  return SendGreeting.class;
60. }
61.}

DispatchServletModule.java

view source
<object id="highlighter_506662_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.server.guice;
02.import net.customware.gwt.dispatch.server.service.DispatchServiceServlet;
03.import com.google.inject.servlet.ServletModule;
04.public class DispatchServletModule extends ServletModule {
05. @Override
06. public void configureServlets() {
07.  // NOTE: the servlet context will probably need changing
08.  serve("/greetmvp/dispatch").with(DispatchServiceServlet.class);
09. }
10.}
分享到:
评论

相关推荐

    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的一个重要里程碑,带来了许多改进和新特性,使得...

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

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

    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 入门(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