2016-09-15 65 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