GWT

http://www.softwaresummit.com/2006/speakers/BlumTamingAJAXWithGWT.pdf

http://www.sencha.com/learn/getting-started-with-ext-gwt/

http://www.artima.com/lejava/articles/java_to_javascript.html

http://www.sencha.com/learn/ext-gwt-and-maven/

Advertisements

GWT Exception Serialization

To serialize an exception to the client during an RPC call:

  1. The exception needs to be declared in the throws clause of the RPC method.
  2. The exception itself needs to be serializable.

When the rpc method throws the exception gwt converts it into JavaScript Exception object and when the  onFailure(Throwable) is invoked the exception is passed as a param to the method.

GWT Serialization

GWT RPC uses Serialization to facilitate async communication between server and client.

All GWT RPC Services extend RemoteServiceSrvlet (RSS) instead of HttpServlet.  RSS automatically takes care of serialization (in addition to calling appropriate service method).

RPC method params and return values needs to be serializable.

GWT Serialization is different from java.io.serialization and the following are rules to user classes in RPC:

  • primitives
  • String, Date, or a primitive wrapper
  • Enumeration
  • array of serializable types (including other serializable arrays).
  • user-defined serializable class.
  • The type has at least one serializable subclass.

User-Defined GWT Serializable Class

  • assignable to IsSerializable or Serializable.
  • All non-final, non-transient instance fields are themselves serializable, and
  • Prior to GWT 1.5, it must have a public default (zero argument) constructor or no constructor at all.
  • As of GWT 1.5, it must have a default (zero argument) constructor (with any access modifier) or no constructor at all.

The transient keyword is honored, so values in transient fields are not exchanged during RPCs.So are fields that are declared as final and hence advisable to be marked transient as well.

GWT RPC serialization supports polymorphism with params and return types.

Making collections serializable in GWT

Prior to GWT 1.4 @gwt.typeArgs annotation is used

in GWT 1.5 parameterized types are used.

Adding an object that doesn’t satisfy type constraint (of course in 1.4) leads to undefined behavior.

  • For RPC interfaces you need to annotate the method param’s powith field names and return types

           Ex:
           @gwt.typeArgs idList <java.lang.Integer>
           @gwt.typeArgs <java.lang.String>
           List populateList(List idList)

  • For DataTypes (DTO) you need to annotate the fields

GWT Serialization File Name:

*.gwt.rpc

Custom Field Serializer for GWT RPC

To custom serialize com.my.DTO creeate a class named com.my.DTO_CustomFieldSerializer and implement the following methods in the class:

    public static void serialize(SerializationStreamWriter streamWriter, MyObject myObject)
        throws SerializationException {}

    public static void deserialize(SerializationStreamReader streamReader, MyObject myObject)
        throws SerializationException {}

    //This method is optional
    public static DTO instantiate(SerializationStreamReader streamReader)
        throws SerializationException {}

More about custom serialization: http://code.google.com/p/wogwt/wiki/CustomFieldSerializer

Presentation Patterns

Below is summary of http://aspiringcraftsman.com/2007/08/25/interactive-application-architecture/

(Web) MVC:

Controller’s responsibility is to mediate between the user interaction and application logic/domain model

Controller decouples View and Domain Model.

FrontController to deal with infrastructural concerns and dispatching requests to individual Controllers

Because web-applications(HTTP) is stateless view is rendered on each request.

MVP:

UI Widgets take care of intercepting the users interactions which are delegated as events to EventHandlers.

Presenters responsibility is to contain presentation logic as well as interact with the Domain Model/Application Logic for the view.

In both the cases Views handle the output to present to user and controller/presenter and updates the domain model. Controller takes care of handling the input from user where as View (for MVP) handlers takes care of user interaction.

GWT Summary

EntryPoint interface void onModuleLoad()

RPC Service:
  – Service interface extending RemoteService
  – ServiceAsync Interface for SI with AsyncCallback<Output> as last param
  – ServiceImpl extending RemoteServiceServlet
           – DTO’s (DomainModel) to be returned by ServiceImpl
           – DTO (DomainModel) needs to be Serializable to be sent from server to client
           – define servlet paths for serviceImpl in web.xml
  – ServiceAsyncCallBack implementing AsyncCallback<T>
           –  implements methods onFailure(Throwable) & onSuccess(T)
           –  takes view as a constructor param and onSuccess paints the result (Presentation Model) on the view (dto (Domain) -> model (Presentation) ;          (Presentation) model -> view)
(Presentation) Model representing the UI data
           –  converts dto (DomainModel) to itself (Presentation Model)!
View representing the display screen/widgets – can take (Presentation) model as constructor param
EntryPointImpl hooks everything together
 – creates Async version of service using GWT.create(class)
 – creates the view and attaches handlers to widgets
 – handlers implement onClick(ClickEvent )  etc.,
 – handlers creates AsyncCallback, views and services (or obtains references to them) and invokes the Async service and passes the AsyncCallback created to handle the service response asynchronously.
*only ServiceImpl is in Server pkg.
*shared package is for classes shared between client and server
*note the difference between presentation model and domain model