2011-03-20 63 views
2

我已經通過了一些文檔,但我無法與數據存儲進行通信......任何人都可以給我一個GWT Web應用程序中使用的objectify示例項目/代碼(我使用eclipse )...只是使用RPC進行簡單的「put」和「get」操作應該可以......至少告訴我它是如何完成的GWT-Objectify:基本

+0

GWT物化文檔是非常全面的,應該可以幫助您快速上手。您的問題與客觀化或獲得基本的GWT客戶端 - 服務器RPC有關嗎? – 2011-03-21 08:20:24

+0

是的...但我得到這個錯誤myproject/servlet甚至在對xml,java和al進行所有更改後都沒有找到 – Maheshwar 2011-03-27 11:08:45

+0

我對GWT很陌生,有些事情我總是會忘記/難以找到添加新的依賴關係時:gwt.xml的正確繼承行。 Objectify: ptdev 2011-06-11 01:04:49

回答

3

瞭解如何使客體化工作的最簡單方法是重複所有步驟來自David's Chandler博客的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...*/ 
} 

Time類(也shared包)只包含一個場毫秒:

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

從鏈接複製類ObjectifyDao以上的server.dao包。然後進行DAO類專門爲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); 
    } 

} 

最後,我們可以提出請求數據庫(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 
     } 
    } 
+0

非常感謝!會嘗試一下,讓你知道很快...... – Maheshwar 2011-03-26 18:34:49