gpt4 book ai didi

java - GWT RPC 无法正常工作

转载 作者:行者123 更新时间:2023-11-30 07:25:48 25 4
gpt4 key购买 nike

我正在我的 GWT 项目中制作 RPC,它正在运行,但我想以某种方式处理的数据消失了。我正在使用本地主机作为服务器。我在服务器包中有两个类:防御:

 package org.elsys.salvation.server;


import org.elsys.salvation.client.Defence;
import org.elsys.salvation.client.FunctionalityManager;

public class Defences {

private ArrayList<Defence> netDefences;
private ArrayList<Defence> hardDefences;
private ArrayList<Defence> softDefences;

public Defences(FunctionalityManager fm){
netDefences = fm.getNetDefences();
hardDefences = fm.getHardDefences();
softDefences = fm.getSoftDefences();
}

public ArrayList<Defence> getNetDefences() {
return netDefences;
}

public ArrayList<Defence> getHardDefences() {
return hardDefences;
}

public ArrayList<Defence> getSoftDefences() {
return softDefences;
}
}

和 DefenceServiceImpl.java :

package org.elsys.salvation.server;

import java.util.ArrayList;

import org.elsys.salvation.client.DefenceService;
import org.elsys.salvation.client.FunctionalityManager;

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

public class DefenceServiceImpl extends RemoteServiceServlet implements DefenceService {

private Defences defences;

@Override
public void saveDefences(FunctionalityManager fm) {
defences = new Defences(fm);
}

@Override
public void getHardDefences(FunctionalityManager fm) {
fm.setHardDefences(defences.getHardDefences());
}

@Override
public void getNetDefences(FunctionalityManager fm) {
fm.setNetDefences(defences.getNetDefences());
}

@Override
public void getSoftDefences(FunctionalityManager fm) {
fm.setSoftDefences(defences.getSoftDefences());
}

}

这是 DefenceService 接口(interface):

package org.elsys.salvation.client;

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

@RemoteServiceRelativePath("defences")
public interface DefenceService extends RemoteService {

void saveDefences(FunctionalityManager fm);
void getHardDefences(FunctionalityManager fm);
void getNetDefences(FunctionalityManager fm);
void getSoftDefences(FunctionalityManager fm);
}

和 DefenceServiceAsync:

package org.elsys.salvation.client;

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

public interface DefenceServiceAsync {

void saveDefences(FunctionalityManager fm, AsyncCallback<Void> callback);

void getHardDefences(FunctionalityManager fm, AsyncCallback<Void> callback);

void getNetDefences(FunctionalityManager fm, AsyncCallback<Void> callback);

void getSoftDefences(FunctionalityManager fm, AsyncCallback<Void> callback);

}

这是我调用 saveDefences 方法的代码:

private void addDiploma() {

final AsyncCallback<Void> callback = new AsyncCallback<Void>() {
public void onFailure(Throwable caught) {

}

@Override
public void onSuccess(Void result) {
SC.say("Submited");
}
};

some code...

Button submitButton = new Button("Submit");
submitButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
FM.getDiploma(projectNameTextBox, diplomantsNameTextBox,
diplomaLeadersListBox, reviewersListBox,
specialtiesComboBox, typeComboBox);
FM.generateDefences();
defenceSvc.saveDefences(FM,callback);
RootPanel.get("mainDiv").clear();
showDefences();
}
});

some more code...
}

下面是FM和defenceSvc的定义:

public FunctionalityManager FM = new FunctionalityManager();
private DefenceServiceAsync defenceSvc = GWT.create(DefenceService.class);

这里是我想将保存的数据返回给客户端的地方:

protected void showDefence() {  
FunctionalityManager funcM = new FunctionalityManager();
final AsyncCallback<Void> callback = new AsyncCallback<Void>() {
public void onFailure(Throwable caught) {

}

@Override
public void onSuccess(Void result) {
// TODO Auto-generated method stub
}
};
defenceSvc.getHardDefences(funcM, callback);
defenceSvc.getNetDefences(funcM, callback);
defenceSvc.getSoftDefences(funcM, callback);
final ListGrid DiplomaGrid = new ListGrid();
DiplomaGrid.setWidth(500);
DiplomaGrid.setHeight(224);
DiplomaGrid.setShowAllRecords(true);
DiplomaGrid.setCanEdit(true);
DiplomaGrid.setEditEvent(ListGridEditEvent.CLICK);
DiplomaGrid.setModalEditing(true);

DiplomaData dd= new DiplomaData(funcM);

ListGridField nameField = new ListGridField("name", "Project Name");
ListGridField diplomantsField = new ListGridField("diplomants", "Diplomants");
ListGridField leaderField = new ListGridField("leader", "Leader");
ListGridField reviewerField = new ListGridField("reviewer", "Reviewer");
ListGridField typeField = new ListGridField("type", "Type");
ListGridField dateField = new ListGridField("date", "Date");
DiplomaGrid.setFields(new ListGridField[] {nameField, diplomantsField, leaderField,reviewerField, typeField, dateField});
DiplomaGrid.setData(dd.getRecords());

RootPanel.get("mainDiv").add(DiplomaGrid);
}

web.xml:

<servlet>
<servlet-name>defenceServiceImpl</servlet-name>
<servlet-class>org.elsys.salvation.server.DefenceServiceImpl</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>defenceServiceImpl</servlet-name>
<url-pattern>/salvation/defences</url-pattern>
</servlet-mapping>

当我在开发模式下运行它时,它正在运行,但是当我调用 showDefence() 方法时,我无法检索服务器数据上的序列化。谁能告诉我问题出在哪里?

最佳答案

在服务器端,您正在修改 FM,但您没有将其发送回客户端:那是行不通的。客户端和服务器之间的对象传递是通过复制(序列化→反序列化)进行的,因此在服务器上进行更改只会影响服务器端的副本。您无法通过将对象发送到服务器来更新对象;您正在发送它的副本,然后服务器必须发回另一个应用了更改的副本。

换句话说,对于您的代码,将方法的返回类型从 void 更改为调频,让服务器返回调频传递的参数,并在客户端,在onSuccess方法,使用结果更新您的 singleton 对象(在 setHardDefences 的回调中,将 FM 的 hardDefences 设置为服务器返回的 FM 中的 hardDefences;对于其他 < em>防御)。

关于java - GWT RPC 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10684052/

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