RPC调用不起作用。 GUICE + DISPATCH

时间:2016-09-15 07:36:33

标签: java gwt guice rpc gwt-dispatch

我有一个简单的动作和结果类。处理程序只是增加变量的动作并返回结果。 的操作

public class IncrementCounter implements Action<IncrementCounterResult> {
    private int amount;

    /** For serialization only. */
    public IncrementCounter(){}

    public IncrementCounter(int amount){
        this.amount = amount;
    }

    public int getAmount() {
        return amount;
    }
}

结果

public class IncrementCounterResult implements Result {
    private int amount;
    private int current;

    /** For serialization only. */
    public IncrementCounterResult(){}

    public IncrementCounterResult(int amount, int current){
        this.amount = amount;
        this.current = current;
    }

    public int getAmount() {
        return amount;
    }

    public int getCurrent() {
        return current;
    }
}

我有一个动作模块,我在处理程序上绑定动作。 的 ActionsModule

public class ActionsModule extends ActionHandlerModule {
    @Override
    protected void configureHandlers() {
        bindHandler(IncrementCounter.class, IncrementCounterHandler.class);
    }
}

DispatchServletModule

public class DispatcherServletModule extends ServletModule {
    @Override
    protected void configureServlets() {
        serve("/dispatch").with(GuiceStandardDispatchServlet.class);
    }
}

处理程序,在操作中增加字段并将其返回到结果中。

public class IncrementCounterHandler implements ActionHandler<IncrementCounter, IncrementCounterResult> {
    private int current = 0;


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


    public IncrementCounterResult execute(IncrementCounter action, ExecutionContext context) throws ActionException {
        current += action.getAmount();
        return new IncrementCounterResult(action.getAmount(), current);
    }


    public void rollback(IncrementCounter action, IncrementCounterResult result, ExecutionContext context) throws ActionException {
        current = result.getCurrent() - result.getAmount();
    }
}

最后一个是听众

public class RpcCommandGuiceConfig extends GuiceServletContextListener {

    @Override
    protected Injector getInjector() {
        return Guice.createInjector(new ServerDispatchModule(), new ActionsModule());
    }
}

这是我的 web.xml 文件

<?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>server.RpcCommandGuiceConfig</listener-class>
    </listener>
    <welcome-file-list>
        <welcome-file>Module.html</welcome-file>
    </welcome-file-list>
</web-app>

单击按钮时调用dispatch.execute。和代码跳转在onFailure部分。 我在日志中有这个:

WARN] 404 - POST /Module/dispatch (127.0.0.1) 1380 bytes
   Request headers
      Host: 127.0.0.1:8888
      User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0
      Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
      Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3
      Accept-Encoding: gzip, deflate
      X-GWT-Permutation: HostedMode
      X-GWT-Module-Base: http://127.0.0.1:8888/Module/
      Content-Type: text/x-gwt-rpc; charset=utf-8
      Referer: http://127.0.0.1:8888/Module/Module.html?gwt.codesvr=127.0.0.1:9997
      Content-Length: 238
      Connection: keep-alive
      Pragma: no-cache
      Cache-Control: no-cache

有人可以帮忙解决这个问题吗?

... 这里是gwt文件

<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 2.0//EN"
        "http://google-web-toolkit.googlecode.com/svn/releases/2.0/distro-source/core/src/gwt-module.dtd">
<module>
    <source path="client"/>
    <source path="shared"/>
    <source path="server"/>
    <inherits name='com.google.gwt.user.User'/>
    <inherits name="com.mvp4g.Mvp4gModule"/>
    <inherits name="org.moxieapps.gwt.highcharts.Highcharts"/>
    <inherits name="net.customware.gwt.dispatch.Dispatch" />
    <inherits name="com.google.gwt.inject.Inject" />
    <entry-point class='client.Module'/>
</module>

1 个答案:

答案 0 :(得分:0)

我认为您需要在serve DispatcherServletModule前加上模块名称。在您的情况下,它看起来像是在根目录中,所以它只是模块名称:Module

  

服务(&#34; /模块/调度&#34)。与(GuiceStandardDispatchServlet.class);