我们一直使用GWT-Dispatch来支持使用命令模式的RPC调用。我们现在需要转移到GWTP,因为Dispatch已被吸收到该项目中。似乎一切都很好。问题是我们无法再获取DispatchAsync对象。在Dispatch中,获取默认实现非常简单:
private final DispatchAsync dispatchAsync = GWT.create(DefaultDispatchAsync.class);
这不再适用。不推荐使用DefaultDispatchAsync,当我们使用建议的替换(RpcDispatchAsync)时,它看起来像这样:
private final DispatchAsync dispatchAsync = GWT.create(RpcDispatchAsync.class);
我们收到以下错误:
Rebind result 'com.gwtplatform.dispatch.rpc.client.RpcDispatchAsync' has no default (zero argument) constructors.
有没有人知道如何做到这一点?我知道如果我们重写所有页面以使用它在Presenter中可用的GWTP MVP模式,但移动使用完整GWTP是一个漫长的过程,如果我们无法获得RPC调用并快速工作,那将是一个该项目的问题。
提前致谢 - 希望这很容易。
答案 0 :(得分:0)
不再通过延迟绑定生成DispatchAsync。因此,您无法使用GWT.create来实例化它。
GWTP Dispatch正在大量使用GIN / Guice。因此,我建议您使用此依赖注入框架来使GWTP Dispatch工作。
这是一个示例,它提供了对DispatchAsync的轻松访问(无需重写所有页面以使用GWTP MVP模式):
[注意:此示例使用gwtp dispatch 1.0.3]
<强>客户端:强>
MyClientModule.java - 为DispatchAsync配置注入规则
import com.google.gwt.inject.client.AbstractGinModule;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import com.gwtplatform.dispatch.client.DefaultExceptionHandler;
import com.gwtplatform.dispatch.client.DefaultSecurityCookieAccessor;
import com.gwtplatform.dispatch.client.ExceptionHandler;
import com.gwtplatform.dispatch.client.RpcDispatchAsync;
import com.gwtplatform.dispatch.client.actionhandler.ClientActionHandlerRegistry;
import com.gwtplatform.dispatch.client.actionhandler.DefaultClientActionHandlerRegistry;
import com.gwtplatform.dispatch.shared.DispatchAsync;
import com.gwtplatform.dispatch.shared.SecurityCookie;
import com.gwtplatform.dispatch.shared.SecurityCookieAccessor;
public class MyClientModule extends AbstractGinModule {
private static final String COOKIE_NAME = "JSESSIONID";
@Override
protected void configure() {
bindConstant().annotatedWith(SecurityCookie.class).to(COOKIE_NAME);
bind(ExceptionHandler.class).to(DefaultExceptionHandler.class);
bind(SecurityCookieAccessor.class).to(DefaultSecurityCookieAccessor.class);
bind(ClientActionHandlerRegistry.class).to(DefaultClientActionHandlerRegistry.class);
bind(DispatchAsync.class).toProvider(DispatchAsyncProvider.class).in(Singleton.class);
}
public static class DispatchAsyncProvider implements Provider<DispatchAsync> {
private final DispatchAsync fDispatchAsync;
@Inject
public DispatchAsyncProvider(ExceptionHandler eh, SecurityCookieAccessor sca, ClientActionHandlerRegistry cahr) {
this.fDispatchAsync = new RpcDispatchAsync(eh, sca, cahr);
}
@Override
public DispatchAsync get() {
return fDispatchAsync;
}
}
}
MyClientInjector.java - 进样器提供对DispatchAsync的访问
import com.google.gwt.inject.client.GinModules;
import com.google.gwt.inject.client.Ginjector;
import com.gwtplatform.dispatch.shared.DispatchAsync;
@GinModules(MyClientModule.class)
public interface MyClientInjector extends Ginjector {
DispatchAsync getDispatchAsync();
}
服务器强>
MyGuiceServletContextListener.java - 为servlet创建注入器,它接收命令和servermodule,其中定义了(clientside)命令和(serverside)处理程序之间的绑定。
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.servlet.GuiceServletContextListener;
public class MyGuiceServletContextListener extends GuiceServletContextListener {
@Override
protected Injector getInjector() {
return Guice.createInjector(new ServerModule(), new DispatchServletModule());
}
}
DispatchServletModule.java - 配置接收命令的servlet
import com.google.inject.servlet.ServletModule;
import com.gwtplatform.dispatch.server.guice.DispatchServiceImpl;
import com.gwtplatform.dispatch.server.guice.HttpSessionSecurityCookieFilter;
import com.gwtplatform.dispatch.shared.Action;
import com.gwtplatform.dispatch.shared.SecurityCookie;
public class DispatchServletModule extends ServletModule {
@Override
public void configureServlets() {
bindConstant().annotatedWith(SecurityCookie.class).to("JSESSIONID");
filter("*").through(HttpSessionSecurityCookieFilter.class);
serve("/" + Action.DEFAULT_SERVICE_NAME + "*").with(DispatchServiceImpl.class);
}
}
ServerModule.java - (clientside)命令与(serverside)处理程序之间的绑定
import com.gwtplatform.dispatch.server.guice.HandlerModule;
public class ServerModule extends HandlerModule {
@Override
protected void configureHandlers() {
bindHandler(YourCommand.class, YourHandler.class);
}
}
web.xml - 告诉网络服务器使用MyGuiceServletContextListener
<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>
<!--
This Guice listener hijacks all further filters and servlets. Extra
filters and servlets have to be configured in your
ServletModule#configureServlets() by calling
serve(String).with(Class<? extends HttpServlet>) and
filter(String).through(Class<? extends Filter)
-->
<listener>
<listener-class>de.gwtpdispatch.server.MyGuiceServletContextListener</listener-class>
</listener>
<强>用法强>
现在,您可以使用延迟绑定创建注入器并访问DispatchAsync-instance:
MyClientInjector injector = GWT.create(MyClientInjector.class);
injector.getDispatchAsync().execute(...YourCommand...)
(AND:不要忘记在项目中包含GIN和Guice的jar并将gin-module添加到项目的gwt.xml中)
我希望这个解释足够详细。快乐的编码:)