gpt4 book ai didi

GWT 简单 RPC 用例问题 : Code included

转载 作者:行者123 更新时间:2023-12-04 11:17:19 25 4
gpt4 key购买 nike

我正在尝试研究如何使用 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

这是项目类和文件。

测试.gwt.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 2.0.0//EN" "http://google-web-toolkit.googlecode.com/svn/tags/2.0.0/distro-source/core/src/gwt-module.dtd">
<module>
<inherits name="com.google.gwt.user.User" />
<source path="client/test" />
<entry-point class="org.redboffin.worldhug.client.test.Test"></entry-point>
</module>

网页.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

<!-- Servlets -->

<servlet>
<servlet-name>testServlet</servlet-name>
<servlet-class>org.redboffin.worldhug.server.test.TestServiceImpl</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>testServlet</servlet-name>
<url-pattern>/worldhug/test/testService</url-pattern>
</servlet-mapping>

<!-- Default page to serve -->
<welcome-file-list>
<welcome-file>test.html</welcome-file>
</welcome-file-list>

</web-app>

测试对象
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<ListObject> listObjects = new ArrayList<ListObject>();

public TestObject() {}

// Getters and setters removed for brevity

}

内部对象.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

}

列表对象.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.

}

测试服务.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 <code>TestService</code>.
* @author Darren
*/
public interface TestServiceAsync {

void test(AsyncCallback<TestObject> callback);

}

测试服务Impl.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<ListObject> 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;
}

}

测试 View
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<VerticalPanel, TestView> {}

@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<TestObject> callback = new AsyncCallback<TestObject>() {

public void onSuccess(TestObject testObject) {
testObjectStringLabel.setText(testObject.getTestObjectString());
InnerObject innerObject = testObject.getInnerObject();
innerObjectStringLabel.setText(innerObject.getInnerObjectString());
ArrayList<ListObject> listObjects = (ArrayList<ListObject>) testObject.getListObjects();
Iterator<ListObject> 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);

}

}

测试 View .ui.xml
<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder
xmlns:ui="urn:ui:com.google.gwt.uibinder"
xmlns:g="urn:import:com.google.gwt.user.client.ui">

<g:VerticalPanel>
<g:Label>Test Object</g:Label>
<g:Label ui:field="testObjectStringLabel"></g:Label>
<g:VerticalPanel>
<g:Label>Inner Object</g:Label>
<g:Label ui:field="innerObjectStringLabel"></g:Label>
</g:VerticalPanel>
<g:VerticalPanel ui:field="listObjectsPanel">
<g:Label>List Objects</g:Label>
</g:VerticalPanel>
<g:Button ui:field="button">Display Test Object</g:Button>
<g:Label ui:field="errorMessageLabel"></g:Label>
</g:VerticalPanel>

</ui:UiBinder>

感谢您阅读到这里,并感谢您可以给我(和其他人)的任何帮助。

最佳答案

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

例如

<source path="client/test"/>
<source path="server/test"/>

将域类放在服务器包中可能是更好的选择。我们经常做这样的事情:
<source path="client"/>
<source path="shared"/>

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

关于GWT 简单 RPC 用例问题 : Code included,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2237142/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com