2016-09-15 34 views
0

我有一个简单的动作和结果类。处理程序只是在操作中增加变量并将结果返回。 行动RPC调用不起作用。 GUICE + 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> 
+0

我想我有xml.file问题。也许我搞砸标签。但是,当我加入到 server.DispatcherServletModule时,我有一个错误:这个类不能分配给'javax.servlet.Servelt',但他扩展了import com.google.inject.servlet.ServletModule ;?他如何知道这个javax.servlet的错误? – Andrew

+0

server.DispatcherServletModule这里是错误不能分配给'javax.servlet.Servlet', – Andrew

+0

任何人都可以帮忙吗?我仍然无法解决这个问题( – Andrew

回答

0

我认为你需要的模块名称的前缀serveDispatcherServletModule。在你的情况下,它看起来像它的根,所以才模块名称:Module

serve("/Module/dispatch").with(GuiceStandardDispatchServlet.class);

+0

你刚刚救了我的命)非常感谢你。我整整呆了一整天,并没有解决这个问题,今天早上你帮了我。谢谢 – Andrew