2

我在我的 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 接口:

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() 方法时,我无法检索服务器数据上的序列化。有人可以告诉问题出在哪里吗?

4

2 回答 2

4

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

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

于 2012-05-21T11:14:04.103 回答
0

我只是尝试了类似的事情。我想使用假数据来测试 UI,因为我还没有真实数据。我替换了一个包含静态数据成员的 Singleton 类。一个成员是一个有时被写入的列表。

写操作不起作用。

使用 Debug 遍历它,似乎每个到服务器的 RPC 每次都在实例化这些类。我跟踪了 Singleton 的getInstance,并为每个 RPC 实例化了该类。

Singleton 类的单元测试工作正常,但这是本地的,而不是服务器上的。

我相信这是有道理的,因为在 Google 服务器上,您可能有多个应用程序实例正在运行。由于每个都有自己的 Singleton,因此无需保留它。

基于此,我建议每次调用 RPC 时都实例化 DefenceServiceImpl,因此它始终是一个新类,即您可以保存数据。

于 2014-06-02T00:30:30.280 回答