GWT通过RequestFactory从服务器向客户端传递Objectify游标,并在DataGrid中显示更多页面 [英] GWT pass Objectify Cursor from Server to Client with RequestFactory and show more pages in DataGrid

查看:74
本文介绍了GWT通过RequestFactory从服务器向客户端传递Objectify游标,并在DataGrid中显示更多页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public List< Venue>这是一个全新的游标,并有以下方法: findPage(Subject subject,Objectify ofy,int pageSize){
final Business business =(Business)subject.getSession()。getAttribute(BUSINESS_ATTRIBUTE);
final Query< Venue> query = ofy.query(Venue.class).filter(BUSINESS_ATTRIBUTE,business);
final String encodedCursor =(String)subject.getSession()。getAttribute(VENUE_CURSOR_CONDITION);
if(encodedCursor!= null){
query.startCursor(Cursor.fromWebSafeString(encodedCursor));
}
final QueryResultIterator< Key< Venue>> iterator = query.fetchKeys()。iterator();
final List< Key< Venue>> data = new ArrayList< Key< Venue>>(pageSize);
boolean more = false;
for(int i = 0; i< pageSize&&(more = iterator.hasNext()); i ++){
data.add(iterator.next());
}
subject.getSession()。setAttribute(VENUE_CURSOR_CONDITION,iterator.getCursor()。toWebSafeString());
return get(ofy,data);
}

其中get是以下方法:

  public< T>列表与LT; T> get(Objectify ofy,List< Key< T>> keys){
if(keys == null){
return null;
}
最终映射< Key< T>,T> map = ofy.get(keys);
最终列表< T> list = new ArrayList< T>(); $ t $ b for(T t:map.values()){
list.add(t);
}
返回列表;
}

现在,我正在做的是 - 这有点冒险。我正在使用Apache Shiro来跟踪用户的会话 - 在用户的会话中,我保留最后使用的游标 - 如果用户从页面导航离开页面,我将光标设置为false(显然 - 这是非常非常脆弱的,因为它需要每个方法都有一个代码来使最后一个游标失效)。

现在,这里是我被抓到的地方。我做了一个简单的HasCursor接口,并且将要传回带有Cursor的List到客户端 - 唯一的问题是RequestFactory只能处理从服务器到客户端的某些类型的传递 - 这个Custom类型不是其中之一。所以,现在使用GWT RequestFactory - 是否有一种将Objectify游标从服务器传递给客户端的好方法?

另外 - 我遇到的另一个问题是 - 如果我使用这些数据填充DataGrid,它会在网格中显示数据正常 - 但就是这样 - 寻呼机会在25中表示1-25。所以,我想知道的是,您如何确切地告诉DataGrid存在服务器上提供更多数据。我有点困惑,因为它似乎没有很多可用的文档/示例。



非常感谢您






对于遇到这个问题的其他人来说,我发现了其中一部分的答案 - 我忘记了所有关于ValueProxies的知识,它们允许您映射标准的java bean使用RequestFactory从服务器返回到客户端。



不幸的是,您不能将泛型与ValueProxies一起使用,所以我必须为每种类型创建一个ListCursor包装器。



以下是代理:

  @ProxyFor(value = VenueListCursorWrapper .class)
public interface VenueListCursorWrapperProxy extends ValueProxy {

List< VenueProxy> getVenues();

String getCursor();

boolean isMoreAvailable();

void setVenues(List< VenueProxy> venues);

void setCursor(String cursor);

void setMoreAvailable(boolean moreAvailable);
}

以下是服务器上的更新方法:

  public VenueListCursorWrapper findPage(Subject subject,Objectify ofy,int pageSize,String cursor){
final Business business =(Business)subject.getSession() .getAttribute(BUSINESS_ATTRIBUTE);
final Query< Venue> query = ofy.query(Venue.class).filter(BUSINESS_ATTRIBUTE,business);
if(cursor!= null){
query.startCursor(Cursor.fromWebSafeString(cursor));
}
final QueryResultIterator< Key< Venue>> iterator = query.fetchKeys()。iterator();
final List< Key< Venue>> data = new ArrayList< Key< Venue>>(pageSize);
boolean more = false;
for(int i = 0; i< pageSize&&(more = iterator.hasNext()); i ++){
data.add(iterator.next());
}
返回新的VenueListCursorWrapper(get(ofy,data),iterator.getCursor()。toWebSafeString(),more);

$ / code>

所以 - 那就是它的一部分 - 我仍然没有完全想到如何做是让页面设置正确 - 我想显示在寻呼机中的项目的总数,让您通过它,但我不知道如何做到这一点 - 但如果我找到我将发布一个解决方案

解决方案

您可以将 Cursor 转换为可串行化的字符串 - 实际上你已经使用了这个: iterator.getCursor()。toWebSafeString()

只需将序列化的游标(= String)与所有其他列表数据一起返回给客户端,并在下一个请求中将其作为参数提供。 b
$ b

Optonaly在片段标识符中使用它,aka GWT历史令牌,以便用户实际上可以为列表中的特定页面添加书签(取决于用例,如果列表数据变化不大,则是有意义的)。


I am brand new to cursors and have the following method:

public List<Venue> findPage(Subject subject, Objectify ofy, int pageSize) {
  final Business business = (Business) subject.getSession().getAttribute(BUSINESS_ATTRIBUTE);
  final Query<Venue> query = ofy.query(Venue.class).filter(BUSINESS_ATTRIBUTE, business);
  final String encodedCursor = (String) subject.getSession().getAttribute(VENUE_CURSOR_CONDITION);
  if (encodedCursor != null) {
    query.startCursor(Cursor.fromWebSafeString(encodedCursor));
  }
  final QueryResultIterator<Key<Venue>> iterator = query.fetchKeys().iterator();
  final List<Key<Venue>> data = new ArrayList<Key<Venue>>(pageSize);
  boolean more = false;
  for (int i = 0; i < pageSize && (more = iterator.hasNext()); i++) {
    data.add(iterator.next());
  }
  subject.getSession().setAttribute(VENUE_CURSOR_CONDITION, iterator.getCursor().toWebSafeString());
  return get(ofy, data);
}

where get is the following method

public <T> List<T> get(Objectify ofy, List<Key<T>> keys) {
  if (keys == null) {
    return null;
  }
  final Map<Key<T>, T> map = ofy.get(keys);
  final List<T> list = new ArrayList<T>();
  for (T t : map.values()) {
    list.add(t);
  }
  return list;
}

Now, here's what I'm doing currently - which is a little hackish. I am using Apache Shiro to track User's sessions - in the user's session I am keeping the last cursor that was used - if the user navigates away from the page i set the cursor to false (obviously - this is very, very brittle as it requires every method to have a code to invalidate the last cursor).

Now, here's where I'm getting caught. I made a simple HasCursor interface, and was going to pass back a List with Cursor in it to the client side - the only problem is RequestFactory can only handle passing certain types from server to client - this Custom type isn't one of them. So, right now with GWT RequestFactory - is there a good way to pass an Objectify Cursor from the Server to the Client?

Also - another issue I'm running into - if I populate the DataGrid with this data it shows the data fine in the grid - but that's it - the pager says 1-25 out of 25. So, what I'm wondering is, how do you actually tell the DataGrid that there is more data available on the server. I'm a little confused as it seems there isn't a whole lot of documentation / examples available for this.

Thank you very much


for anybody else that runs into this problem i found out the answer to one part of it - i forgot all about ValueProxies that allow you to map standard java beans to return to the client side from the server with RequestFactory.

Unfortunately, you can't use Generics with ValueProxies so I had to create a ListCursor wrapper for each individual type.

Here's the proxy:

@ProxyFor(value = VenueListCursorWrapper.class)
public interface VenueListCursorWrapperProxy extends ValueProxy {

  List<VenueProxy> getVenues();

  String getCursor();

  boolean isMoreAvailable();

  void setVenues(List<VenueProxy> venues);

  void setCursor(String cursor);

  void setMoreAvailable(boolean moreAvailable);
}

and here's the updated method on the server:

public VenueListCursorWrapper findPage(Subject subject, Objectify ofy, int pageSize, String cursor) {
  final Business business = (Business) subject.getSession().getAttribute(BUSINESS_ATTRIBUTE);
  final Query<Venue> query = ofy.query(Venue.class).filter(BUSINESS_ATTRIBUTE, business);
  if (cursor != null) {
    query.startCursor(Cursor.fromWebSafeString(cursor));
  }
  final QueryResultIterator<Key<Venue>> iterator = query.fetchKeys().iterator();
  final List<Key<Venue>> data = new ArrayList<Key<Venue>>(pageSize);
  boolean more = false;
  for (int i = 0; i < pageSize && (more = iterator.hasNext()); i++) {
    data.add(iterator.next());
  }
  return new VenueListCursorWrapper(get(ofy, data), iterator.getCursor().toWebSafeString(), more);
}

So - that's that part of it - the one thing I still haven't quite figured out how to do is get the pager set up correctly - I would like to show the total count of items in the pager to allow you to page through it but I'm not sure exactly how to do this yet - if I find a solution I will post it

解决方案

You can convert Cursor to a serializable string - actually you already use this: iterator.getCursor().toWebSafeString().

Just return the serialized cursor (= a String) to the client together with all other list data and on the next request provide it as a parameter.

Optonaly use it in fragment identifier, aka GWT history token, so that user can actually bookmark the particular page in your list (depends on the use case, makes sense if list data does not change much).

这篇关于GWT通过RequestFactory从服务器向客户端传递Objectify游标,并在DataGrid中显示更多页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆