当前位置:  开发笔记 > 编程语言 > 正文

GWT简单RPC用例问题:包含代码

如何解决《GWT简单RPC用例问题:包含代码》经验,为你挑选了1个好方法。

我试图找出如何使用GWT RPC将域对象从服务器端发送到客户端.我编写了一个非常简单的用例,它表示我(和其他人)需要能够做的事情,但目前无法开始工作.

我已经浏览了文档,教程和论坛,但他们要么显示字符串被传递,要么提供解释(当我将它应用于此时)仍然无效.

希望有人可以向我和其他人解释为什么这些代码不起作用以及如何使其工作.

谢谢.

以下是错误消息.

13:12:54.328 [DEBUG] [org.redboffin.worldhug.Test] Validating newly compiled units
13:12:54.328 [ERROR] [org.redboffin.worldhug.Test] Errors in 'file:/C:/Documents%20and%20Settings/Darren/workspace/WorldHug/src/org/redboffin/worldhug/client/test/TestService.java'
13:12:54.343 [ERROR] [org.redboffin.worldhug.Test] Line 14: No source code is available for type org.redboffin.worldhug.server.test.TestObject; did you forget to inherit a required module?
13:12:54.515 [ERROR] [org.redboffin.worldhug.Test] Errors in 'file:/C:/Documents%20and%20Settings/Darren/workspace/WorldHug/src/org/redboffin/worldhug/client/test/TestServiceAsync.java'
13:12:54.515 [ERROR] [org.redboffin.worldhug.Test] Line 12: No source code is available for type org.redboffin.worldhug.server.test.TestObject; did you forget to inherit a required module?
13:12:55.953 [ERROR] [org.redboffin.worldhug.Test] Errors in 'file:/C:/Documents%20and%20Settings/Darren/workspace/WorldHug/src/org/redboffin/worldhug/client/test/TestView.java'
13:12:55.968 [ERROR] [org.redboffin.worldhug.Test] Line 42: No source code is available for type org.redboffin.worldhug.server.test.TestObject; did you forget to inherit a required module?
13:12:55.968 [ERROR] [org.redboffin.worldhug.Test] Line 46: No source code is available for type org.redboffin.worldhug.server.test.InnerObject; did you forget to inherit a required module?
13:12:55.984 [ERROR] [org.redboffin.worldhug.Test] Line 48: No source code is available for type org.redboffin.worldhug.server.test.ListObject; did you forget to inherit a required module?
13:12:56.765 [INFO] [org.redboffin.worldhug.Test] Module org.redboffin.worldhug.Test has been loaded

这是项目类和文件.

Test.gwt.xml




    
    
    

在web.xml






  

  
    testServlet
    org.redboffin.worldhug.server.test.TestServiceImpl
  

  
    testServlet
    /worldhug/test/testService
  

  
  
    test.html
  


TestObject.java

package org.redboffin.worldhug.server.test;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.user.client.rpc.IsSerializable;

public class TestObject implements IsSerializable {

    private String testObjectString;
    private InnerObject innerObject;
    private List listObjects = new ArrayList();

    public TestObject() {}

    // Getters and setters removed for brevity

}

InnerObject.java

package org.redboffin.worldhug.server.test;

import com.google.gwt.user.client.rpc.IsSerializable;

public class InnerObject implements IsSerializable {

    private String innerObjectString;

    public InnerObject() {}

        // Getters and setters removed for brevity

}

ListObject.java

package org.redboffin.worldhug.server.test;

import com.google.gwt.user.client.rpc.IsSerializable;

public class ListObject implements IsSerializable {

    private String listObjectString;

    public ListObject() {}

        // Getters and setters removed for brevity.

}

TestService.java

package org.redboffin.worldhug.client.test;

import org.redboffin.worldhug.server.test.TestObject;

import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;

/**
 * The client side stub for the Test Service.
 * @author Darren
 */
@RemoteServiceRelativePath("testService")
public interface TestService extends RemoteService {

    TestObject test();

}

TestServiceAsync.java

package org.redboffin.worldhug.client.test;

import org.redboffin.worldhug.server.test.TestObject;

import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * The async counterpart of TestService.
 * @author Darren
 */
public interface TestServiceAsync {

    void test(AsyncCallback callback);

}

TestServiceImpl.java

package org.redboffin.worldhug.server.test;

import java.util.List;

import org.redboffin.worldhug.client.test.TestService;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The server side implementation of the RPC service.
 * @author Darren
 */
@SuppressWarnings("serial")
public class TestServiceImpl extends RemoteServiceServlet implements TestService {

    @Override
    public TestObject test() {

        TestObject testObject = new TestObject();       
        testObject.setTestObjectString("Test Object String");

        InnerObject innerObject = new InnerObject();
        innerObject.setInnerObjectString("Inner Object String");

                testObject.setInnerObject(innerObject);

        List listObjects = testObject.getListObjects();

        ListObject listObjectOne = new ListObject();
        listObjectOne.setListObjectString("List Object One");
        listObjects.add(0, listObjectOne);

        ListObject listObjectTwo = new ListObject();
        listObjectTwo.setListObjectString("List Object Two");
        listObjects.add(0, listObjectTwo);

        ListObject listObjectThree = new ListObject();
        listObjectThree.setListObjectString("List Object Three");
        listObjects.add(0, listObjectThree);

        return testObject;
    }

}

TestView.java

package org.redboffin.worldhug.client.test;

import java.util.ArrayList;
import java.util.Iterator;

import org.redboffin.worldhug.server.test.InnerObject;
import org.redboffin.worldhug.server.test.ListObject;
import org.redboffin.worldhug.server.test.TestObject;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

public class TestView extends Composite {

    private static TestViewUiBinder uiBinder = GWT.create(TestViewUiBinder.class);

    interface TestViewUiBinder extends UiBinder {}

    @UiField Label testObjectStringLabel;
    @UiField Label innerObjectStringLabel;
    @UiField VerticalPanel listObjectsPanel;
    @UiField Button button;
    @UiField Label errorMessageLabel;

    public TestView(String firstName) {
        initWidget(uiBinder.createAndBindUi(this));
    }

    @UiHandler("button")
    void onClick(ClickEvent e) {

        TestServiceAsync testService = (TestServiceAsync) GWT.create(TestService.class);

        AsyncCallback callback = new AsyncCallback() {

            public void onSuccess(TestObject testObject) {
                testObjectStringLabel.setText(testObject.getTestObjectString());
                InnerObject innerObject = testObject.getInnerObject();
                innerObjectStringLabel.setText(innerObject.getInnerObjectString());
                ArrayList listObjects = (ArrayList) testObject.getListObjects();
                Iterator iterator = listObjects.iterator();
                while(iterator.hasNext()) {
                    ListObject listObject = (ListObject) iterator.next();
                    listObjectsPanel.add(new Label(listObject.getListObjectString()));
                }
            }

            public void onFailure(Throwable caught) {
                errorMessageLabel.setText("Error : "+caught.getMessage());
            }
          };

          testService.test(callback);

    }

}

TestView.ui.xml




    
        Test Object
        
        
            Inner Object
            
        
        
            List Objects
        
        Display Test Object
        
    


感谢您阅读这篇文章,以及您可以给我(和其他人)的任何帮助.



1> Craig Day..:

您需要标识包含要进行GWT编译的源的所有包.

例如



将域类不在服务器包中可能是更好的选择.我们经常这样做:



其中shared包含在客户端和服务器之间来回传递的DTO.

推荐阅读
手机用户2402851335
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有