存储App Engine分页的光标 [英] Storing the Cursor for App Engine Pagination

查看:67
本文介绍了存储App Engine分页的光标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用App Engine的RPC和GWT(它是一个应用程序引擎连接的项目)来实现分页。

如何将查询结果和网页安全游标对象从RPC传递给GWT客户端?



我看过使用servlet的例子,但我想知道如何在没有servelt的情况下执行它。



我已经考虑使用memcache在服务器上缓存游标,但我不确定这是合适的还是应该用作关键字(我假设的会话标识符,但我不确定这些是如何在App Engine上处理的)。



示例项目的链接太棒了,我一直无法找到任何。

解决方案

好的,所以最好的方法是将游标作为字符串存储在客户端。



要做到这一点,你有创建一个可传输的包装类,以便您可以通过RequestFactory将其传回给客户端,该客户端可以保存结果列表和游标字符串。要做到这一点,您需要创建一个普通的POJO,然后为其创建代理。



以下是POJO的代码:

  public class OrganizationResultsWrapper {

public List< Organization>列表;
public String webSafeCursorString;

公共列表<组织> getList(){
return list;
}

public void setList(List< Organization> list){
this.list = list;
}

public String getWebSafeCursorString(){
return this.webSafeCursorString;
}

public void setWebSafeCursorString(String webSafeCursorString){
this.webSafeCursorString = webSafeCursorString;
}
}

代表:

  @ProxyFor(OrganizationResultsWrapper.class)
public interface OrganizationResultsWrapperProxy extends ValueProxy {

List< OrganizationProxy>的GetList();
void setList(List< OrganizationProxy> list);

String getWebSafeCursorString();
void setWebSafeCursorString(String webSafeCursorString);




$ p
$ b

设置您的服务和requestFactory以使用POJO和代理分别

  //服务类方法
@ServiceMethod
Public OrganizationResultsWrapper getOrganizations(String webSafeCursorString){
return dao.getOrganizations(webSafeCursorString);
}

//请求工厂方法
请求< OrganizationResultsWrapperProxy> getOrganizations(String webSafeCursorString);

然后确保并运行RPC向导,以便验证过程运行,否则您将收到请求服务器上的上下文错误。

以下是我的数据访问类中的实现:

  public OrganizationResultsWrapper getOrganizations(String webSafeCursorString){
List< Organization> list = new ArrayList< Organization>();
OrganizationResultsWrapper resultsWrapper = new OrganizationResultsWrapper();

查询<组织> query = ofy()。load()。type(Organization.class).limit(50);

if(webSafeCursorString!= null){
query = query.startAt(Cursor.fromWebSafeString(webSafeCursorString));
}

QueryResultIterator<组织> iterator = query.iterator();
while(iterator.hasNext()){
list.add(iterator.next());
}

resultsWrapper.setList(list);
resultsWrapper.setWebSafeCursorString(iterator.getCursor()。toWebSafeString());

返回resultsWrapper;
}


I'm trying to implement pagination using App Engine's RPC and GWT (it's an app engine connected project).

How can I pass both the query results and the web-safe cursor object to the GWT client from the RPC?

I've seen examples using a servlet but I want to know how to do it without a servelt.

I've considered caching the cursor on the server using memcache but I'm not sure if that's appropriate or what should be used as the key (session identifier I would assume, but I'm not sure how those are handled on App Engine).

Links to example projects would be fantastic, I've been unable to find any.

解决方案

OK, so the best way to do this is to store the cursor as a string on the client.

To do this you have to create a wrapper class that is transportable so you can pass back it to the client via RequestFactory that can hold the results list and the cursor string. To do that you create a normal POJO and then a proxy for it.

here's what the code looks like for the POJO:

public class OrganizationResultsWrapper {

    public List<Organization> list;
    public String webSafeCursorString;

    public List<Organization> getList() {
        return list;
    }

    public void setList(List<Organization> list) {
        this.list = list;
    }

    public String getWebSafeCursorString() {
        return this.webSafeCursorString;
    }

    public void setWebSafeCursorString(String webSafeCursorString) {
        this.webSafeCursorString = webSafeCursorString;
    }
}

for the proxy:

@ProxyFor(OrganizationResultsWrapper.class)
public interface OrganizationResultsWrapperProxy extends ValueProxy{

    List<OrganizationProxy> getList();
    void setList(List<OrganizationProxy> list);

    String getWebSafeCursorString();
    void setWebSafeCursorString(String webSafeCursorString);

}

set up your service and requestFactory to use the POJO and proxy respectively

// service class method
@ServiceMethod
public OrganizationResultsWrapper getOrganizations(String webSafeCursorString) {
    return dao.getOrganizations(webSafeCursorString);
}

// request factory method
Request<OrganizationResultsWrapperProxy> getOrganizations(String webSafeCursorString); 

Then make sure and run the RPC wizard so that your validation process runs otherwise you'll get a request context error on the server.

Here's the implementation in my data access class:

public OrganizationResultsWrapper getOrganizations(String webSafeCursorString) {
    List<Organization> list = new ArrayList<Organization>();
    OrganizationResultsWrapper resultsWrapper = new OrganizationResultsWrapper();

    Query<Organization> query = ofy().load().type(Organization.class).limit(50);

    if (webSafeCursorString != null) {
        query = query.startAt(Cursor.fromWebSafeString(webSafeCursorString));
    }

    QueryResultIterator<Organization> iterator = query.iterator();
    while (iterator.hasNext()) {
        list.add(iterator.next());
    }

    resultsWrapper.setList(list);
    resultsWrapper.setWebSafeCursorString(iterator.getCursor().toWebSafeString());

    return resultsWrapper;
}

这篇关于存储App Engine分页的光标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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