2
我有一個Servlet,它將一個實體保存到數據存儲和一個顯示錶格內容的jsp中。我注意到數據存儲自動爲每個添加到表的新行創建一個唯一的ID /名稱。當我嘗試在我的jsp中顯示ID /名稱值時,我返回空值。以下是我的代碼。我想知道是否有人可以告訴我如何從表中獲取ID /名稱值。提前謝謝你。- 如何檢索ID /名稱值?
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
%>
這就是我根據你的建議所做的:<%= user.getKey()。getName()%>,但沒有奏效。 – sap 2012-03-18 23:58:02
這工作:user.getKey()。getId(); – sap 2012-03-19 00:37:59