GWT 无法读取未定义的 属性 'example'

GWT Cannot read property 'example' of undefined

我正在学习 GWT,目前正在为 RPC 苦苦挣扎。我有一个简单的项目:一个标签、一个文本框、一个输出标签和一个按钮。 我希望当用户在 TextBox 中输入他的姓名并按下 "send" 按钮时,他将从服务器收到一条消息 "Hello "+name+"Here speaks the Server" - 愚蠢的例子。 但是在我的客户端中,我有一个包 GUI 和一个包服务以及我的入口点 class

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());
        }       
    }   
}

服务:

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

异步服务:

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

客户端接口:

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);
        }       
    }
}

服务器端代码:

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

我不知道这个错误是从哪里来的,希望你能帮助我。

我自己找到了答案 - 我犯了一个简单的错误:

在 class 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
        ...