在视图之间传输数据

时间:2014-02-08 02:37:11

标签: java eclipse gwt

我正在尝试将登录详细信息从登录视图传递到下一个视图。我找到了一个教程并遵循它然而我被卡住了。我能够在服务器端存储登录数据,但无法检索它。

服务器端代码是:

private ViewData viewData = new ViewData();

public ViewData setViewData(String accountId, String accountLevel, String ymId) {

    System.out.println("accountId = " + accountId);
    ViewData viewData = new ViewData();

    viewData.setaccountId(accountId);
    viewData.setaccountLevel(accountLevel);
    viewData.setymId(ymId);

    System.out.println("setViewData viewData.getaccountId() = " + viewData.getaccountId());

    return null;
}

public ViewData getViewData(String accountId, String accountLevel, String ymId) {

    //viewData = new ViewData(viewData.getaccountId(), viewData.getaccountLevel(), viewData.getymId());
    viewData = new ViewData();

    System.out.println("getViewData viewData.getaccountId() = " + viewData.getaccountId());
    System.out.println("getViewData accountId = " + accountId);

    return viewData;

}

public ViewData getViewData() {
    return viewData;
}

当我调用setViewData时,两个System.out.println显示7和7是正确的。

当我调用getViewData时,两个System.out.println显示null和null返回给调用视图。

getViewData由:

调用
verticalPanel.addAttachHandler(new Handler() {
        public void onAttachOrDetach(AttachEvent event) {
            //On load of page get the Account Level and ID.
            Window.alert("SelectPersonView vertical panel attached.");
            AsyncCallback<ViewData> callback = new ViewDataHandler<ViewData>(SelectPersonView.this);
            rpc.getViewData(null, null, null, callback);
        }
    });

class ViewDataHandler<T> implements AsyncCallback<ViewData> {

    SelectPersonView view;

    public ViewDataHandler(SelectPersonView view) {
        this.view = view;
    }

    public void onFailure(Throwable ex) {
        Window.alert("RPC call failed.");
    }
    public void onSuccess(ViewData result) {
        //Store the view data
        ViewData viewData = result;
        accountId = viewData.getaccountId();
        accountLevel = viewData.getaccountLevel();
        ymId = viewData.getymId();

        Window.alert("accountId = " + accountId + " accountLevel = " + accountLevel + " ymId = " + ymId);
    }
}

接口的异步版本是:

//View Data
public void setViewData(String accountId, String accountLevel, String ymId, AsyncCallback<ViewData> callback);
public void getViewData(String accountId, String accountLevel, String ymId, AsyncCallback<ViewData> callback);

表示数据模型的类是:

package org.AwardTracker.client;

import java.io.Serializable;

public class ViewData implements Serializable {
private static final long serialVersionUID = 1L;

private String accountId;
private String accountLevel;
private String ymId;

//@SuppressWarnings("unused")
public ViewData() {
    //just here because GWT wants it.
}
//  public ViewData(String accountId, String accountLevel, String ymId) {
//      this.accountId = accountId;
//      this.accountLevel = accountLevel;
//      this.ymId = ymId;
//  }

public String getaccountId() {
    return accountId;
}
public void setaccountId(String accountId) {
    this.accountId = accountId;
}

public String getaccountLevel() {
    return accountLevel;
}
public void setaccountLevel(String accountLevel) {
    this.accountLevel = accountLevel;
}

public String getymId() {
    return ymId;
}
public void setymId(String ymId) {
    this.ymId = ymId;
}

}

1 个答案:

答案 0 :(得分:0)

非常感谢我的朋友Duc,他在很多人失败的地方取得了成功(当然包括我)。答案是:

在客户端调用以保存数据:

AsyncCallback<ViewData> callback = new ViewDataStoreHandler<ViewData>();
                    rpc.setViewData(accountId, accountLevel, youthMemberID, callback);

服务器端代码是:

private ViewData viewData = null;

public ViewData setViewData(String accountId, String accountLevel, String ymId) {

    viewData = new ViewData();

    viewData.setaccountId(accountId);
    viewData.setaccountLevel(accountLevel);
    viewData.setymId(ymId);

    return viewData;
}

在客户端调用获取数据是:

AsyncCallback callback = new ViewDataHandler(BoomerangView.this);                 rpc.getViewData(回调);

服务器端代码是:

public ViewData getViewData() {
    return viewData;
}

我真诚地希望这可以帮助其他人,因为我已经花了很长时间,现在已经解决了所有主要障碍,以完成这个童子军项目。我要感谢所有帮助过我的人。

致以最诚挚的问候,

格林