2016-07-27 27 views
0

我正在學習GWT,目前我正忙於RPC。我有一個簡單的項目:標籤,文本框,輸出標籤和按鈕。 我想當用戶在文本框中輸入他的名字,然後按下「發送」按鈕,他將從服務器「你好」+名稱+「這裏講服務器」 - 一個愚蠢的例子得到一條消息。 然而,在我的客戶我有一個包GUI和打包服務,我的入口點類GWT無法讀取未定義的屬性'示例'

public class TestGwt270 implements EntryPoint { 

public void onModuleLoad() 
{  
    TestGwt270ClientImpl clientImpls = new TestGwt270ClientImpl("/TestGwt270/testgwt270service"); 
    GWT.log("Main "+GWT.getModuleBaseURL()); 
    RootPanel.get().add(clientImpls.getMainGUI()); 
} 

MyGui:

public class MainGUI extends Composite 
{ 
    private TestGwt270ClientImpl serviceImpl; 

    private VerticalPanel vPanel; 

    private TextBox inputTB; 
    private Label outputLbl; 

    public MainGUI(TestGwt270ClientImpl serviceImpl) 
    { 
     this.vPanel = new VerticalPanel(); 
     initWidget(vPanel); 

     this.inputTB = new TextBox(); 
     this.inputTB.setText("Gib deinen Namen ein"); 
     this.outputLbl = new Label("Hier kommt der output"); 
     this.vPanel.add(this.inputTB); 
     this.vPanel.add(this.outputLbl); 

     Button sendBtn = new Button("send"); 
     sendBtn.addClickHandler(new MyClickhandler()); 
     this.vPanel.add(sendBtn);  
    } 

    public void updateOutputLbl(String output) 
    { 
     this.outputLbl.setText(output); 
    } 

    private class MyClickhandler implements ClickHandler 
    { 
     @Override 
     public void onClick(ClickEvent event) { 
      // TODO Auto-generated method stub 
      serviceImpl.sayHello(inputTB.getText()); 
     }  
    } 
} 

TheService:

@RemoteServiceRelativePath("testgwt270service") 
public interface TestGwt270Service extends RemoteService 
{ 
    String sayHello(String name); 
} 

AsyncService:

public interface TestGwt270ServiceAsync 
{ 
    void sayHello(String name, AsyncCallback<String> callback); 
} 

ClientInterface:

public interface TestGwt270ServiceClientInt 
{ 
    void sayHello(String name); 
} 

客戶端實現:

public class TestGwt270ClientImpl implements TestGwt270ServiceClientInt 
{ 
    private TestGwt270ServiceAsync service; 
    private MainGUI maingui; 

    public TestGwt270ClientImpl(String url) 
    { 
     GWT.log(url); 
     // TODO Auto-generated constructor stub 
     this.service = GWT.create(TestGwt270Service.class); 
     ServiceDefTarget endpoint = (ServiceDefTarget) this.service; 
     endpoint.setServiceEntryPoint(url); 

     this.maingui = new MainGUI(this); 
    } 

    public MainGUI getMainGUI() 
    { 
     return this.maingui; 
    } 

    @Override 
    public void sayHello(String name) { 
     // TODO Auto-generated method stub 
     this.service.sayHello(name, new MyCallback()); 
    } 

    private class MyCallback implements AsyncCallback<String> 
    { 
     @Override 
     public void onFailure(Throwable arg0) { 
      // TODO Auto-generated method stub 
      GWT.log("Failure"); 
      maingui.updateOutputLbl("An Error has occured"); 
     } 

     @Override 
     public void onSuccess(String arg0) { 
      // TODO Auto-generated method stub 
      GWT.log("Success"); 
      maingui.updateOutputLbl(arg0); 
     }  
    } 
} 

ServerSideCode:

public class TestGwt270ServiceImpl extends RemoteServiceServlet implements TestGwt270Service 
{ 
    @Override 
    public String sayHello(String name) { 
     // TODO Auto-generated method stub 
     GWT.log("Hello " + name + "\nHier spricht der Server mit dir"); 
     return "Hello " + name + "\nHier spricht der Server mit dir"; 
    } 
} 

我的問題是,當我按下按鈕,發送我的名字服務器我得到以下錯誤:

HandlerManager.java:129 Uncaught com.google.gwt.event.shared.UmbrellaException: Exception caught: (TypeError) : Cannot read property 'sayHello_2_g$' of undefined

我不知道這個錯誤來自哪裏,我希望你能幫助我。

+0

好的,感謝您的編輯:)下次我說得對 – KilledByCheese

回答

0

,我發現自己的答案 - 我做了一個簡單的錯誤:

在課堂上MyGUI我得到這個:

public class MainGUI extends Composite 
{ 
    private TestGwt270ClientImpl serviceImpl; 
    ... 
    public MainGUI(TestGwt270ClientImpl serviceImpl) 
    { 
     ... 

我忘了分配serviceImpl 修復:

public class MainGUI extends Composite 
{ 
    private TestGwt270ClientImpl serviceImpl; 
    ... 
    public MainGUI(TestGwt270ClientImpl serviceImpl) 
    { 
     this.serviceImpl = serviceImpl; //this line is the solution to my problem 
     ... 
相關問題