DataStore - 如何检索ID /名称值?

时间:2012-03-18 22:31:38

标签: google-app-engine google-cloud-datastore

我有一个将实体保存到数据存储区的Servlet和一个显示表内容的jsp。我注意到数据存储区会自动为添加到表中的每个新行创建唯一的ID /名称。当我尝试在我的jsp中显示ID / Name值时,我返回null值。以下是我的代码。我想知道是否有人能告诉我如何从表中获取ID / Name值。非常感谢你提前。

public class RegisterForAccountServlet extends HttpServlet 
{
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException 
    {

    String email = req.getParameter("email");
    Key userKey = KeyFactory.createKey("RegisterUserKey", email);
    Entity user = new Entity("RegisteredUsers", userKey);
    user.setProperty("email", email);
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    datastore.put(user);


    resp.sendRedirect("/ListOfAllUsers.jsp");
}

}

以下是显示所有注册用户的jsp:

          

<%
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
//Key guestbookKey = KeyFactory.createKey("Guestbook", guestbookName);

// Run an ancestor query to ensure we see the most up-to-date
// view of the Greetings belonging to the selected Guestbook.
Query query = new Query("RegisteredUsers");
List<Entity> users = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(5));
if (users.isEmpty()) 
{
%>
    <p>no registered users</p>

<%
} 
else 
{
%>

    <%
    for (Entity user : users) 
    {
    %>
        <table border="1">
        <tr>
        <td><%= user.getProperty("ID/Name") %></td>
        <td><%= user.getProperty("email") %></td>
        </tr>
        </table>

   <%
    }//for
}//else
%>

1 个答案:

答案 0 :(得分:4)

名称/ ID是实体密钥的一部分。使用user.getKey().getName()