【问题标题】:GWT Cannot read property 'example' of undefinedGWT 无法读取未定义的属性“示例”
【发布时间】:2023-12-27 18:41:01
【问题描述】:

我正在学习 GWT,目前我正在努力使用 RPC。我有一个简单的项目:一个标签、一个文本框、一个 outputLabel 和一个按钮。 我希望当用户在 TextBox 中输入他的姓名并按下“发送”按钮时,他会从服务器“Hello”+name+“Heres speaks the Server”收到一条消息——愚蠢的例子。 但是在我的客户端中,我有一个包 GUI 和一个包服务以及我的入口点类

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 未捕获 com.google.gwt.event.shared.UmbrellaException:捕获异常:(TypeError):无法读取未定义的属性“sayHello_2_g$”

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

【问题讨论】:

  • 好的,感谢您的编辑:) 下次我做对了

标签: javascript java gwt gwt-super-dev-mode


【解决方案1】:

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

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

【讨论】: