GWT通过serviceimpl从数据存储区检索列表 [英] GWT retrieve list from datastore via serviceimpl
问题描述
public class ContactDAOJdo实现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实现ContactService {
private static final long serialVersionUID = 1L;
private ContactDO contactDAO = new ContactDAOJdo(){
@Override
public LinkedHashSet< Contact> listContacts(){
LinkedHashSet< Contact> contacts = contactDAO.listContacts();
System.out.println(service imp+ 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 catch){
gui.service_eventListContactenFailed );
System.out.println(delegate+ catch);
}
public void onSuccess(LinkedHashSet< Contact> result){
gui.service_eventListRetrievedFromService(result);
System.out.println(delegate+ result);
}
});
}
}
public class ListContactGUI {
protected Grid contactlijst;
protected ListContactDelegate listContactService;
私人标签状态;
public void init(){
status = new Label();
contactlijst = new Grid();
contactlijst.setVisible(false);
status.setText(正在检索联系人列表);
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 catch){
status.setText(无法从数据库中检索联系人列表);
}
}
可以查询返回一个懒惰列表。这意味着在列表发送给客户端时,并不是所有的值都在列表中。我使用一个技巧,只需调用列表中的 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(); //这将触发获取所有值。
返回联系人;
} finally {
pm.close();
}
}
但我不知道这是否是最佳做法...
Hi I'm trying to retrieve a linkedhashset from the Google datastore but nothing seems to happen. I want to display the results in a Grid using GWT on a page. I have put system.out.println() in all the classes to see where I go wrong but it only shows one and I don't recieve any errors. I use 6 classes 2 in the server package(ContactDAOJdo/ContactServiceImpl) and 4 in the client package(ContactService/ContactServiceAsync/ContactListDelegate/ContactListGui). I hope someone can explain why this isn't worken and point me in the right direction.
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.");
}
}
It could be the query returns a lazy list. Which means not all values are in the list at the moment the list is send to the client. I used a trick to just call size()
on the list (not sure how I got to that solution, but seems to work):
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();
}
}
But I'm not sure if this is the best practice...
这篇关于GWT通过serviceimpl从数据存储区检索列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!