Sunday, February 5, 2012

Spring 3.1: GWT Maven Plugin and GWTHandler Integration (Part 3)

Review

In the previous section, we have discussed how to generate a simple GWT application using the GWT Maven plugin. In this section, we will study how to integrate Spring and GWT using the GWTHandler library via configuration and modifying existing classes.


Integrating GWTHandler

What is GWTHandler?

The GWTHandler is part of the GWT Server Library--a collection of Java server side components for the Google Web Toolkit AJAX framework with the current focus on the Spring framework by facilitating publishing of Spring beans as RPC services.

The GWTHandler allows you to quickly map multiple RPC service beans to different URLs very similar to the way Spring's SimpleUrlHandlerMapping maps URLs to controllers.

Source: http://code.google.com/p/gwt-sl/

Configuration

The initial step in configuring the GWTHandler is to declare our XML configuration files:
  • web.xml
  • gwt-servlet.xml
  • applicationContext.xml

In the web.xml the part that you need to pay extra attention is the url-pattern. Here we've declared the pattern as /gwtmodule/rpc/*. This means all RPC calls should follow that pattern!


The gwt-servlet.xml is nothing but a simple bean declaration. Here we've declared a GWTHandler bean, and it contains one mapping. We've mapped all calls to /greet to be processed by a GreetingService implementation. Remember this is an RPC call,sSo if you have plans of calling this RPC mapping, you must also take account the mapping the gwt-servlet.xml! In other words, the complete path is rpc/greet.


In the applicationContext.xml we've basically enabled annotation scanning.


Classes

The second step in configuring the GWTHandler is to modify our existing classes to take advantage of the GWTHandler.

We will create a new service class:
  • SpringService

And edit the following classes:
  • GreetingService
  • GreetingServiceImpl

The SpringService basically prints out a Hello *name* from Spring 3.1 message.


As discussed earlier, to use the /greet RPC mapping, we have to consider the parent mapping from the gwt-servlet.xml as well. In other words, the complete mapping for this RPC is rpc/greet


Next we modify the GreetingServiceImpl to take advantage of the SpringService implementation. So instead of the original reply that includes server information, we're now returning a simple "hello" message instead.


For completion purposes, we will also show the contents of the gwtmodule class, though nothing has been changed.


Next

In the next section, we will build and run the application using Maven, and show how to import the project in Eclipse. Click here to proceed.
StumpleUpon DiggIt! Del.icio.us Blinklist Yahoo Furl Technorati Simpy Spurl Reddit Google I'm reading: Spring 3.1: GWT Maven Plugin and GWTHandler Integration (Part 3) ~ Twitter FaceBook

Subscribe by reader Subscribe by email Share

8 comments:

  1. This is a perfect executed tutorial...
    what are the features that make GWT convenient for using in a web project?

    gwt seems way overengineered and extremely heavy weight. Is there other way to get same results? I wonder.

    Thank you in advance
    jD

    ReplyDelete
  2. @Findings, thanks for the comments. I do feel GWT is too complicated just to achieve a simple task. But it works. It is somewhat a heavyweight indeed.

    And that's why I've completely eradicated GWT from our development framework and instead opted for Spring MVC and jQuery because they are simpler to build and debug. They are more malleable than GWT. Of course, GWT has some niceties that you won't get or will require extra work with Spring MVC and jQuery combination, but most of the time I don't need those. In addition, I can use Backbone.js to achieve an MVC architecture on the presentation layer.

    If you look at my tutorial list, most of them use jQuery and jqGrid.

    ReplyDelete
    Replies
    1. @krams,
      I totally, fundamentally, completely agree with you on the GWT opinion...
      On the other hand: the problem that I see when using backbone.js or similar js libraries is that you ending replicating much of the server side plumbing on the client side. Besides it will increase the loading time of your initial pages.

      When you have time, I invite you to visit my blog too. I am in the same neighborhood. Blog at http://pragmatikroo.blogspot.com.

      Thank you
      jD

      Delete
  3. @Findings,
    Probably a repetition of patterns but with different concerns. It's true that it will increase the initial loading of all affected pages. But there are ways to optimize and improve the loading experience, i.e Wro4j and following the tips from YSlow and alike.

    I've visited your blog. It's the first time I've seen a blog that puts heavy focus on Spring Roo and its various uses. It's something I've tried before but never found the inspiration to use it. Your blog showcases Spring Roo on a different way--something that intrigues my mind. I will definitely take a second look. But I'm gonna read first your articles. Thanks

    ReplyDelete
    Replies
    1. @krams,

      Thank you for visiting my blog...
      Telling the true, I think currently Spring Roo is in "transitioning mode". The thing is I don't know what would be the ending state. Anyway I'll be good to try it.
      Any question on my stuff... please let me know neighbor.

      Delete
  4. Maybe you should also remove "extends RemoteServiceServlet" from GreetingServiceImpl.java since it is not a servlet anymore and getServletContext() returns null. That's why I was stuck here for an hour or so. Odd: Exceptions are not logged with mvn gwt:run, it just returns 500. BTW, how you debug the mvn gwt:run process?
    Otherwise: a nice introduction, thanks.

    ReplyDelete
    Replies
    1. also stuck for hours on this!! Is there any way to debug server side code?

      Delete
  5. Great tutorial - I found this very helpful. Many Thanks.

    ReplyDelete