views:

378

answers:

2

I have some questions regarding gwt-dispatch and guice. I'm using Guice 2.0, gwt-dispatch 1.1.0 snapshot, mvp4g 1.1.0 and GIN 1.0

First of all, I have defined simple action, result and handler:

ListContactsAction.java

public class ListContactsAction implements Action<ListContactsResult>{

    public ListContactsAction() {
    }

}

ListContactsResult.java

public class ListContactsResult implements Result {

    private List<Contact> contactList;

    public ListContactsResult() {
    }

    public ListContactsResult(List<Contact> contactList) {
        this.contactList = contactList;
    }

    public List<Contact> getContactList() {
        return contactList;
    }
}

ListContactsHandler.java

public class ListContactsHandler implements ActionHandler<ListContactsAction, ListContactsResult>{

    @Inject
    private SqlSessionFactory factory;

    public Class<ListContactsAction> getActionType() {
        return ListContactsAction.class;
    }

    public ListContactsResult execute(ListContactsAction a, ExecutionContext ec) throws DispatchException {
        // some code using SqlSessionFactory and returning ListContactResult
        // with list of contacts
    }

    public void rollback(ListContactsAction a, ListContactsResult r, ExecutionContext ec) throws DispatchException {
        /* get action - no rollback needed */
    }

}

In previous version of my app, which was using rpc service instead of command pattern, I had a method which was providing SqlSessionFactory for injections, something like this:

@Provides
    public SqlSessionFactory getSqlSessionFactory(){
        // some code here
    }

I read on gwt-dispatch getting started that I have to provide binding between my action and it's handler, which should look something like that:

public class ContactModule extends ActionHandlerModule{
    @Override
    protected void configureHandlers() {
        bindHandler(ListContactsAction.class, ListContactsHandler.class);
    }
}

But I have problem wiring it all with Guice, because this example from gwt-dispatch site:

public class DispatchServletModule extends ServletModule {
    @Override
    public void configureServlets() {
        serve( "/path/to/dispatch" ).with( DispatchServiceServlet.class );
    }
}

doesn't work, since there is no DispatchServiceServlet in the package.

My questions are:

  • How should I write DispatchServletModule and how to make it going (with what I should serve path)
  • what should I put in the web.xml file of my app to be able to correctly execute actions from my presenter, which has GIN injected DispatcherAsync implementation
  • Where should I put my SqlSessionFactory providing method (in which module) to be able to inject SqlSessionFactory where I need it
  • How I instantiate the injector so then I can use it in other action handlers properly

I think that is all and I made myself clear. If something isn't clear enough, I'll try to be more specific.

+1  A: 

Have you created a GuiceServletConfig class? This is where you setup your Dispatch servlet module as well as your action handler module with Guice.

plubic class GuiceServletConfig extends GuiceServletContextListener {
    @Override
    protected Injector getInjector() {
        return Guice.createInjector(new HandlerModule(), new DispatchServletModule());
    }
}

The HandlerModule is your ActionHandler module class, so from your code you would put your ContactModule class.

For your SqlSessionFactory, you could setup the binding for it in your ContactModule, with my code I only have a single ServerModule that sets up all my service and action handler bindings. This is mainly for the sake of simplicity.

Simon.D
A: 

Hi,

Can anyone please explain what is "/path/to/dispatch" here in Guice implementation!

Gaurav
Path to dispacth, is a path that directs to gwt-dispatch servlet. When your gwt module is in package com.foo.bar.myApp, then probably the path You have to set will be com.foo.bar.myApp/dispatch. If You set a "rename-to" attribute in your gwt module config file, eg. rename-to="foo-bar", your path to dispatch will be foo-bar/dispatch.If You still have problems, fire Your application in hosted mode and try to use dispatch. Then look for 404 error - is You have one, look where it tried to find dispatch and copy this path.
jjczopek
@jjczopek,Thanks a lot!
Gaurav