【问题标题】:GWT retrieve list from datastore via serviceimplGWT 通过 serviceimpl 从数据存储中检索列表
【发布时间】:2011-10-04 18:33:26
【问题描述】:

您好,我正在尝试从 Google 数据存储中检索linkedhashset,但似乎没有任何反应。我想在页面上使用 GWT 在 Grid 中显示结果。我已将 system.out.println() 放入所有类中,以查看我哪里出错了,但它只显示一个并且我没有收到任何错误。我在服务器包(ContactDAOJdo/ContactServiceImpl)中使用 6 个类 2,在客户端包(ContactService/ContactServiceAsync/ContactListDelegate/ContactListGui)中使用 4 个。我希望有人能解释为什么这不起作用并指出我正确的方向。

public class ContactDAOJdo implements ContactDAO {
  @SuppressWarnings("unchecked")
  @Override
  public LinkedHashSet<Contact> listContacts() {
    PersistenceManager pm = PmfSingleton.get().getPersistenceManager(); 
    String query = "select from " + Contact.class.getName();
    System.out.print("ContactDAOJdo: ");
    return (LinkedHashSet<Contact>) pm.newQuery(query).execute();
  }
}

public class ContactServiceImpl extends RemoteServiceServlet implements ContactService{
  private static final long serialVersionUID = 1L;
  private ContactDAO contactDAO = new ContactDAOJdo() {
  @Override
  public LinkedHashSet<Contact> listContacts() {
    LinkedHashSet<Contact> contacts = contactDAO.listContacts();
    System.out.println("service imp "+contacts);
    return contacts;
  }
}

@RemoteServiceRelativePath("contact")
public interface ContactService extends RemoteService {
  LinkedHashSet<Contact> listContacts();
}

public interface ContactServiceAsync {
  void listContacts(AsyncCallback<LinkedHashSet <Contact>> callback);
}

public class ListContactDelegate {
private ContactServiceAsync contactService = GWT.create(ContactService.class);
ListContactGUI gui;
void listContacts(){
    contactService.listContacts(new AsyncCallback<LinkedHashSet<Contact>> () {
        public void onFailure(Throwable caught) {
            gui.service_eventListContactenFailed(caught);
            System.out.println("delegate "+caught);
        }
        public void onSuccess(LinkedHashSet<Contact> result) {
            gui.service_eventListRetrievedFromService(result);
            System.out.println("delegate "+result);
        }
    });
 }
}

public class ListContactGUI {
protected Grid contactlijst;
protected ListContactDelegate listContactService;
private Label status;

public void init() {
    status = new Label();
    contactlijst = new Grid();
    contactlijst.setVisible(false);
    status.setText("Contact list is being retrieved"); 
    placeWidgets();

}

public void service_eventListRetrievedFromService(LinkedHashSet<Contact> result){
    System.out.println("1 service eventListRetreivedFromService "+result);
    status.setText("Retrieved contactlist list");
    contactlijst.setVisible(true);
    this.contactlijst.clear();
    this.contactlijst.resizeRows(1 + result.size());
    int row = 1;
    this.contactlijst.setWidget(0, 0, new Label ("Voornaam"));
    this.contactlijst.setWidget(0, 1, new Label ("Achternaam"));
    for(Contact contact: result) {
        this.contactlijst.setWidget(row, 0, new Label (contact.getVoornaam()));
        this.contactlijst.setWidget(row, 1, new Label (contact.getVoornaam()));
        row++;
        System.out.println("voornaam: "+contact.getVoornaam());
    }
    System.out.println("2 service eventListRetreivedFromService "+result);
}

public void placeWidgets() {
    System.out.println("placewidget inside listcontactgui" + contactlijst);
    RootPanel.get("status").add(status);
    RootPanel.get("contactlijst").add(contactlijst);
}

public void service_eventListContactenFailed(Throwable caught) {
    status.setText("Unable to retrieve contact list from database.");
}
}

【问题讨论】:

    标签: user-interface gwt delegates gwt-rpc


    【解决方案1】:

    可能是查询返回了一个惰性列表。这意味着在将列表发送给客户端时,并非所有值都在列表中。我使用了一个技巧,只在列表中调用size()(不知道我是如何找到该解决方案的,但似乎可行):

    public LinkedHashSet<Contact> listContacts() {
      final PersistenceManager pm = PmfSingleton.get().getPersistenceManager(); 
      try {        
        final LinkedHashSet<Contact> contacts =
            (LinkedHashSet<Contact>) pm.newQuery(Contact.class).execute();
        contacts.size(); // this triggers to get all values.
        return contacts;
      } finally {
        pm.close();
      }
    

    }

    但我不确定这是否是最佳做法...

    【讨论】:

      猜你喜欢
      • 2019-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-21
      • 2023-04-02
      • 1970-01-01
      • 2010-12-28
      • 1970-01-01
      相关资源
      最近更新 更多