`
fishyu0817
  • 浏览: 110162 次
  • 性别: 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.}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics