GWT-Objectify:基本

发布于 2024-10-25 07:10:52 字数 146 浏览 5 评论 0原文

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

I've been through a few documentations, but am not able to communicate to the datastore yet...can anyone give me a sample project/code of objectify used in GWT web app(I use eclipse)...just a simple 'put' and 'get' action using RPC should do...or, atleast tell me how its done

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱*していゐ 2024-11-01 07:10:52

了解如何使 objectify 工作的最简单方法是重复 中描述的所有步骤本文来自 David 的钱德勒博客。如果您对 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...*/
}

Time 类(也是 shared 包)仅包含一个字段 msecs:

@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
        }
    }

Easiest way to understand how to make objectify work is to repeat all steps described in this article from David's Chandler blog. Whole blog is a pretty much must read if you interested in GWT, GAE(Java), gwt-presenter, gin\guice,etc. There you will find working example, but anyway here i'll show a slighly advanced example.

In package shared define your entity/model:

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...*/
}

Time class (also shared package) contains just one field msecs:

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

Copy class ObjectifyDao from link above to your server.dao package. And then make DAO class specifically for MyEntry -- 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);
    }

}

Finally we can make requests to database(server package):

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
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文