将 gwt-dispatch 与 guice 和 mvp4g 连接

发布于 2024-08-31 07:04:48 字数 2598 浏览 14 评论 0原文

我有一些关于 gwt-dispatch 和 guice 的问题。我正在使用 Guice 2.0、gwt-dispatch 1.1.0 snapshot、mvp4g 1.1.0 和 GIN 1.0

首先,我定义了简单的操作、结果和处理程序:

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 */
    }

}

在我的以前版本中应用程序,它使用 rpc 服务而不是命令模式,我有一个为注入提供 SqlSessionFactory 的方法,如下所示:

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

我在 gwt-dispatch Getting Started 上读到,我必须提供我的 SqlSessionFactory 之间的绑定动作及其处理程序,应该看起来像这样:

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

但是我在用 Guice 连接它时遇到了问题,因为来自 gwt-dispatch 站点的这个示例:

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

不起作用,因为在包裹。

我的问题是:

  • 我应该如何编写 DispatchServletModule 以及如何让它运行(使用我应该提供的路径)
  • 我应该在应用程序的 web.xml 文件中放入什么,以便能够正确执行来自我的演示者的操作,该演示者具有 GIN注入的 DispatcherAsync 实现
  • 我应该把我的 SqlSessionFactory 提供方法(在哪个模块中)放在哪里,以便能够在我需要的地方注入 SqlSessionFactory
  • 如何实例化注入器,以便我可以使用在其他动作处理程序中,

我认为这就是全部,我已经说得很清楚了。如果有些事情还不够清楚,我会尝试更具体。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最佳男配角 2024-09-07 07:04:48

您创建了 GuiceServletConfig 类吗?您可以在此处使用 Guice 设置 Dispatch servlet 模块以及操作处理程序模块。

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

HandlerModule 是您的 ActionHandler 模块类,因此您可以在代码中放置 ContactModule 类。

对于您的 SqlSessionFactory,您可以在 ContactModule 中设置它的绑定,使用我的代码,我只有一个 ServerModule 来设置我的所有服务和操作处理程序绑定。这主要是为了简单起见。

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.

莫言歌 2024-09-07 07:04:48

GWT-Platform 框架使用 gwt-dispatch fork 来处理 rpc 请求。如果您认真考虑使用调度程序和 Guice,那么有很多代码,您可能必须自己编写。我强烈推荐它。

GWT-Platform framework uses a gwt-dispatch fork to handle rpc requests. There's a lot of code, which you probably had to wtite yourself, if you think of seriously using dispatcher and Guice. I highly recommend it.

梦醒时光 2024-09-07 07:04:48

首先,我表示同情。将所有这些放在一起并没有记录在任何地方。我将依次回答您的每个问题。如果有任何不清楚的地方,请在我的答案中添加评论。

QU:我应该如何编写 DispatchServletModule 以及如何让它运行(使用我应该提供的路径)?

net.customware.gwt.dispatch.server.guice 包中有一个 GuiceStandardDispatchServlet 类;用那个。我不是 100% 确定原因,但我使用的路径包括 GWT 模块的名称,后跟“/dispatch”。你可能必须尝试一下。

public class MyServletModule extends ServletModule {
  @Override protected void configureServlets() {
    serve("/com.my.module.name/dispatch")
      .with(GuiceStandardDispatchServlet.class);
  }
}

QU:我应该在应用程序的 web.xml 文件中放入什么,以便能够正确执行来自我的演示者的操作,该演示者已注入 GIN DispatcherAsync 实现?

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
  <filter>
    <filter-name>guiceFilter</filter-name>
    <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>guiceFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <listener>
    <listener-class>com.myapp.whatever.MyContextListener</listener-class>
  </listener>
  ...
</web-app>

...然后您需要一个自定义上下文侦听器创建 Guice 注入器,如下所示。请注意,我已经包含了您的 ContactModule,它绑定了操作处理程序。

public class MyContextListener extends GuiceServletContextListener {
  @Override protected Injector getInjector() {
    return Guice.createInjector(new MyServletModule(), 
      new ContactModule(), new SQLStuffModule());
  }
}

QU:我应该将 SqlSessionFactory 提供方法放在哪里(在哪个模块中)以便能够在需要的地方注入 SqlSessionFactory?

请注意,我在前面的代码片段中包含了一个新的 SQLStuffModule;这将是放置 SqlSessionFactory 绑定的好地方。拥有多个模块没有什么坏处,而且我发现它可以很好地分离各种问题。

QU:如何实例化注入器,以便可以在其他操作处理程序中正确使用它?

对于服务器端,请参阅上面的 MyContextListener 代码片段。

在客户端,您需要一个自定义注入器接口,如下所示:

@GinModules(StandardDispatchModule.class, MyClientModule.class)
public interface MyGinjector extends Ginjector {
  MyWidgetMainPanel getMainPanel();
}

...并且您可以将 MVP 内容绑定在自定义 Gin 模块中,如下所示。很抱歉,我不熟悉 mvp4g,但我假设您需要在模块类中将视图和演示者连接在一起。

public class MyClientModule extends AbstractGinModule {
  @Override protected void configure() {
    bind(...).to(...);
    ...
  }
}

Firstly, I sympathise. Putting this all together isn't documented in any one spot. I'll answer each of your questions in turn. Add comments to my answer if any of it is unclear.

QU: How should I write DispatchServletModule and how to make it going (with what I should serve path)?

There's a GuiceStandardDispatchServlet class in the net.customware.gwt.dispatch.server.guice package; use that. I'm not 100 percent sure why, but the path I use includes the name of my GWT module, followed by '/dispatch'. You might have to experiment with that.

public class MyServletModule extends ServletModule {
  @Override protected void configureServlets() {
    serve("/com.my.module.name/dispatch")
      .with(GuiceStandardDispatchServlet.class);
  }
}

QU: 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?

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
  <filter>
    <filter-name>guiceFilter</filter-name>
    <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>guiceFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <listener>
    <listener-class>com.myapp.whatever.MyContextListener</listener-class>
  </listener>
  ...
</web-app>

... and then you'll need a custom context listener that creates a Guice injector as follows. Notice that I've included your ContactModule, which binds action handlers.

public class MyContextListener extends GuiceServletContextListener {
  @Override protected Injector getInjector() {
    return Guice.createInjector(new MyServletModule(), 
      new ContactModule(), new SQLStuffModule());
  }
}

QU: Where should I put my SqlSessionFactory providing method (in which module) to be able to inject SqlSessionFactory where I need it?

Notice that I included a new SQLStuffModule in the previous code snippet; that would be a good place to put your SqlSessionFactory binding. There's no harm having multiple modules, and I find that it keeps the various concerns nicely separated.

QU: How I instantiate the injector so then I can use it in other action handlers properly?

For the server-side, see the MyContextListener code snippet above.

On the client side, you'll need a custom injector interface something like this:

@GinModules(StandardDispatchModule.class, MyClientModule.class)
public interface MyGinjector extends Ginjector {
  MyWidgetMainPanel getMainPanel();
}

...and you can bind your MVP stuff in a custom Gin module as follows. I'm sorry that I'm not familiar with mvp4g, but I assume that you'll need to wire the views and presenters together in the module class.

public class MyClientModule extends AbstractGinModule {
  @Override protected void configure() {
    bind(...).to(...);
    ...
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文