将gwt-dispatch与guice和mvp4g连接起来 [英] Connecting gwt-dispatch with guice and mvp4g
问题描述
我有一些关于gwt-dispatch和guice的问题。我使用的是Guice 2.0,gwt-dispatch 1.1.0快照,mvp4g 1.1.0和GIN 1.0。首先,我定义了简单的动作,结果和处理函数:
ListContactsAction.java
public class ListContactsAction实现Action< ListContactsResult> ; {
public ListContactsAction(){
}
}
ListContactsResult.java
public class ListContactsResult implements Result {
private列表与LT;联系与GT;联系人列表;
$ b $ public ListContactsResult(){
}
$ b $ public ListContactsResult(List< Contact> contactList){
this.contactList = contactList;
}
公共列表<联系人> getContactList(){
return contactList;
$ / code $ / pre
ListContactsHandler.java
public class ListContactsHandler实现ActionHandler< ListContactsAction,ListContactsResult> {
@Inject
private SqlSessionFactory factory;
public Class< ListContactsAction> getActionType(){
return ListContactsAction.class;
$ b $ public ListContactsResult execute(ListContactsAction a,ExecutionContext ec)throws DispatchException {
//一些使用SqlSessionFactory的代码并返回ListContactResult
//与联系人列表
$ b $ public void rollback(ListContactsAction a,ListContactsResult r,ExecutionContext ec)抛出DispatchException {
/ * get action - 不需要回滚* /
}
}
在我的应用程序的以前版本中,它使用rpc服务而不是命令模式,我有一个方法提供了 SqlSessionFactory
用于注入,如下所示:
@Provides
public SqlSessionFactory getSqlSessionFactory(){
//这里的一些代码
}
我在gwt-dispatch入门读过,我必须提供我的动作和它的处理程序之间的绑定,它应该看起来像:
pre $ public class ContactModule extends ActionHandlerModule {
@Override
protected void configureHandlers(){
bindHandler(ListContactsAction.class,ListContactsHandler.class);
$ / code>
但是我有问题把它与Guice连接起来,因为这个例子来自gwt-dispatch站点:
public class DispatchServletModule extends ServletModule {
@Override
public void configureServlets(){
serve(/ path / to / dispatch).with(DispatchServiceServlet.class);
$ b 不起作用,因为没有 DispatchServiceServlet
。
我的问题是:
- 我应该如何编写DispatchServletModule以及如何完成它(使用我应该提供的路径)
- 我应该在我的应用的web.xml文件中放入什么能够正确执行从我的演示者,其中有GIN注入
DispatcherAsync
实现
- 我应该在哪里我的
SqlSessionFactory
提供方法(在哪个模块中)能够在需要的地方注入SqlSessionFactory
- 我如何实例化注入器,然后我可以使用它其他操作处理程序正常
我认为这一切都是我自己清楚的。如果有什么不够清楚的话,我会试着更具体一些。 解决方案
首先,我很同情。把它放在一起并没有记录在任何一个地方。我会依次回答你的每个问题。添加评论给我的答案,如果有任何不清楚的话。
QU:我应该如何编写DispatchServletModule以及如何使其发挥作用)?
net.customware.gwt.dispatch.server.guice包中有一个GuiceStandardDispatchServlet类;使用它。我不是100%确定为什么,但我使用的路径包括我的GWT模块的名称,然后是/ dispatch。
$ p $
public class MyServletModule extends ServletModule {
@Override protected void configureServlets(){
serve(/ com.my.module.name/dispatch)
.with(GuiceStandardDispatchServlet.class);
$ / code $ / pre
$ b $ QU:我应该在我的应用程序的web.xml文件能够正确执行从我的演示者,其中有GIN注入DispatcherAsync实现的行动? <?xml version =1.0encoding =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扩展了GuiceServletContextListener {
@Override protected Injector getInjector(){
return Guice.createInjector (new MyServletModule(),
new ContactModule(),new SQLStuffModule());
$ QUICK:我应该在哪里放置我的SqlSessionFactory提供方法(在哪个模块中)能够在需要的地方注入SqlSessionFactory? 注意,我在前面的代码片段中包含了一个新的SQLStuffModule;这将是一个放置SqlSessionFactory绑定的好地方。 QUOT:我如何实例化注入器,然后我可以使用它。 在其他操作处理程序中是否正确?
对于服务器端,请参阅上面的MyContextListener代码片段。
在客户端,你需要一个自定义注入接口,如下所示:
@GinModules(StandardDispatchModule.class ,MyClientModule.class)
public interface MyGinjector扩展了Ginjector {
MyWidgetMainPanel getMainPanel();
}
...您可以将自定义Gin模块中的MVP内容绑定为如下。我很抱歉,我对mvp4g不熟悉,但我认为你需要将模块类中的视图和演示文稿连接在一起。
public class MyClientModule extends AbstractGinModule {
@Override protected void configure(){
bind(...)。(...);
...
}
}
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.
解决方案 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(...);
...
}
}
这篇关于将gwt-dispatch与guice和mvp4g连接起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!