GWT-对象化 : basic

标签 gwt objectify

我已经阅读了一些文档,但还无法与数据存储区通信...任何人都可以给我一个在 GWT Web 应用程序中使用的 objectify 示例项目/代码(我使用 eclipse)...只是使用 RPC 的简单“放置”和“获取”操作应该可以...或者,至少告诉我它是如何完成的

最佳答案

了解如何使对象化工作的最简单方法是重复this article中描述的所有步骤。来自大卫的钱德勒博客。如果您对 GWT、GAE(Java)、gwt-presenter、gin\guice 等感兴趣,整个博客是必读的。在那里您会找到工作示例,但无论如何,我将在这里展示一个稍微高级的示例。

在包shared中定义您的实体/模型:

import javax.persistence.Embedded;
import javax.persistence.Id;
import com.google.gwt.user.client.rpc.IsSerializable;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Unindexed;

@Entity
public class MyEntry implements IsSerializable {
    // Objectify auto-generates Long IDs just like JDO / JPA
    @Id private Long id;
    @Unindexed private String text = "";
    @Embedded private Time start;

    // empty constructor for serialization
    public MyEntry () {
    }
    public MyEntry (Time start, String text) {
        super();
        this.text = tText;
        this.start = start;
    }
    /*constructors,getters,setters...*/
}

时间类(也是共享包)仅包含一个字段毫秒:

@Entity  
public class Time implements IsSerializable, Comparable<Time> {
protected int msecs = -1;    
  //rest of code like in MyEntry 
}

将类 ObjectifyDao 从上面的链接复制到您的 server.dao 包。然后专门为MyEntry制作DAO类——MyEntryDAO:

package com.myapp.server.dao;

import java.util.logging.Logger;

import com.googlecode.objectify.ObjectifyService;
import com.myapp.shared.MyEntryDao;

public class MyEntryDao extends ObjectifyDao<MyEntry>
{
    private static final Logger LOG = Logger.getLogger(MyEntryDao.class.getName());

    static
    {
        ObjectifyService.register(MyEntry.class);
    }

    public MyEntryDao()
    {
        super(MyEntry.class);
    }

}

最后我们可以向数据库(server 包)发出请求:

public class FinallyDownloadingEntriesServlet extends HttpServlet {
      protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws
ServletException, IOException {
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/plain");
                //more code...
                resp.setHeader("Content-Disposition", "attachment; filename=\""+"MyFileName"+".txt\";");
        try {
            MyEntryDao = new MyEntryDao();
          /*query to get all MyEntries from datastore sorted by start Time*/
            ArrayList<MyEntry> entries = (ArrayList<MyEntry>) dao.ofy().query(MyEntry.class).order("start.msecs").list();

            PrintWriter out = resp.getWriter();
            int i = 0;
            for (MyEntry entry : entries) {
                ++i;
                out.println(i);
                out.println(entry.getStart() + entry.getText());
                out.println();
            }
        } finally {
            //catching exceptions
        }
    }

关于GWT-对象化 : basic,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5370396/

相关文章:

java - Objectify - 如何按 boolean 值过滤?

android - 谷歌云端点、对象化和持久性

java - 为什么无论层次结构如何,GAE 实体名称都必须是唯一的?

java - GWT 将大量数据从服务器发送到客户端的最佳实践

java - 使用父键对象化过滤器实体

java - 你能调整 GAE 的内存缓存超时吗?

java - GWT DataGrid 中可扩展行的简单示例

java - 如何在 GWT 中使用 java.util.Calendar

java - 输入文本字段不会在单击鼠标时移动光标?

javascript - GWT 客户端 - 未捕获的类型错误 : Cannot read property 'clear_31_g$' of undefined