Google Web Toolkit (GWT) MVP Example (5)

LogProvider.java

view source
<object id="highlighter_654322_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 org.apache.commons.logging.Log;
03. import org.apache.commons.logging.impl.Log4JLogger;
04. import com.google.inject.Provider;
05. import com.google.inject.Singleton;
06. @Singleton
07. public class LogProvider implements Provider<Log>{
08.  public Log get() {
09.   return new Log4JLogger("GreetingLogger");
10.  }
11. }

MyGuiceServletConfig.java

view source
<object id="highlighter_401696_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 com.google.inject.Guice;
03. import com.google.inject.Injector;
04. import com.google.inject.servlet.GuiceServletContextListener;
05. public class MyGuiceServletConfig extends GuiceServletContextListener {
06.  @Override
07.  protected Injector getInjector() {
08.   return Guice.createInjector(new ServerModule(), new DispatchServletModule());
09.  }
10. }

ServerModule.java

view source
<object id="highlighter_647091_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.guice.ActionHandlerModule;
03. import org.apache.commons.logging.Log;
04. import co.uk.hivedevelopment.greet.server.handler.SendGreetingHandler;
05. import com.google.inject.Singleton;
06. /**
07.  * Module which binds the handlers and configurations
08.  *
09.  */
10. public class ServerModule extends ActionHandlerModule {
11.  @Override
12.  protected void configureHandlers() {
13.   bindHandler(SendGreetingHandler.class);
14.    
15.   bind(Log.class).toProvider(LogProvider.class).in(Singleton.class);
16.  }
17. }

Finally we need to reconfigure the web.xml with the following:

web.xml

view source
<object id="highlighter_107363_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. <?xml version="1.0" encoding="UTF-8"?>
02. <!DOCTYPE web-app
03.     PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
05. <web-app>
06.  <!-- Remote logging agent for gwt-log -->
07.  <servlet>
08.   <servlet-name>remoteLoggerServiceImpl</servlet-name>
09.   <servlet-class>com.allen_sauer.gwt.log.server.RemoteLoggerServiceImpl</servlet-class>
10.  </servlet>
11.  <servlet-mapping>
12.   <servlet-name>remoteLoggerServiceImpl</servlet-name>
13.   <url-pattern>/greetmvp/gwt-log</url-pattern>
14.  </servlet-mapping>
15.  <filter>
16.   <filter-name>guiceFilter</filter-name>
17.   <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
18.  </filter>
19.  <filter-mapping>
20.   <filter-name>guiceFilter</filter-name>
21.   <url-pattern>/*</url-pattern>
22.  </filter-mapping>
23.  <listener>
24.   <listener-class>co.uk.hivedevelopment.greet.server.guice.MyGuiceServletConfig</listener-class>
25.  </listener>
26.  <!-- Default page to serve -->
27.  <welcome-file-list>
28.   <welcome-file>GreetMvp.html</welcome-file>
29.  </welcome-file-list>
30. </web-app>

You may now launch the application. Surprise surprise, it looks no different but you're now following best practises. Good luck with your own GWT MVP development.

你可能感兴趣的:(Web,servlet,Google,Flash,gwt)