如何从JSP文件(使用Google App Engine的Java)中删除实体

如何从JSP文件(使用Google App Engine的Java)中删除实体,java,google-app-engine,jsp,Java,Google App Engine,Jsp,我有一个jsp文件,它显示了数据存储中的一些记录,我希望在每个条目旁边都有一个删除按钮,将其从数据库中删除 因此,我知道Servlet将执行实际的删除操作,但我不确定jsp中的delete按钮应该放什么,这样我的Servlet就知道要在数据库中查找哪个实体(项) 有更好的方法吗 这是我的jsp。向下看底部附近我的删除按钮: <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page

我有一个jsp文件,它显示了数据存储中的一些记录,我希望在每个条目旁边都有一个删除按钮,将其从数据库中删除

因此,我知道Servlet将执行实际的删除操作,但我不确定jsp中的delete按钮应该放什么,这样我的Servlet就知道要在数据库中查找哪个实体(项)

有更好的方法吗

这是我的jsp。向下看底部附近我的删除按钮:

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.List" %>
<%@ page import="com.google.appengine.api.users.User" %>
<%@ page import="com.google.appengine.api.users.UserService" %>
<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>
<%@ page import="com.google.appengine.api.datastore.DatastoreServiceFactory" %>
<%@ page import="com.google.appengine.api.datastore.DatastoreService" %>
<%@ page import="com.google.appengine.api.datastore.Query" %>
<%@ page import="com.google.appengine.api.datastore.Entity" %>
<%@ page import="com.google.appengine.api.datastore.FetchOptions" %>
<%@ page import="com.google.appengine.api.datastore.Key" %>
<%@ page import="com.google.appengine.api.datastore.KeyFactory" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

<html>
  <head>
    <link type="text/css" rel="stylesheet" href="/stylesheets/main.css" />
  </head>

  <body>

<%
    String podlistName = request.getParameter("podlistName");
    if (podlistName == null) {
        podlistName = "default";
    }
    pageContext.setAttribute("podlistName", podlistName);
    UserService userService = UserServiceFactory.getUserService();
    User user = userService.getCurrentUser();
    if (user != null) {
      pageContext.setAttribute("user", user);

%>
<p>Hello, ${fn:escapeXml(user.nickname)}! (You can
<a href="<%= userService.createLogoutURL(request.getRequestURI()) %>">sign out</a>.)</p>
<%
    } else {
%>
<p>Hello!
<a href="<%= userService.createLoginURL(request.getRequestURI()) %>">Sign in</a>
to include your name with podcasts you post.</p>
<%
    }
%>

<%
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    Key podlistKey = KeyFactory.createKey("Podlist", podlistName);
    // Run an ancestor query to ensure we see the most up-to-date
    // view of the Greetings belonging to the selected Podlist.
    Query query = new Query("Podcast", podlistKey).addSort("date", Query.SortDirection.DESCENDING);
    List<Entity> podcasts = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(5));
    if (podcasts.isEmpty()) {
        %>
        <p>Podlist '${fn:escapeXml(podlistName)}' has no items.</p>
        <%
    } else {
        %>
        <p>Items in Podlist '${fn:escapeXml(podlistName)}'.</p>
        <%
        for (Entity podcast : podcasts) {
            pageContext.setAttribute("podcast_url",
                                     podcast.getProperty("podcast_url"));
            if (podcast.getProperty("user") == null) {
                %>
                <p>An anonymous person wrote:</p>
                <%
            } else {
                pageContext.setAttribute("podcast_user",
                                         podcast.getProperty("user"));
                %>
                <p><b>${fn:escapeXml(podcast_user.nickname)}</b> wrote:</p>
                <%
            }
            %>
            <blockquote>${fn:escapeXml(podcast_url)}</blockquote>
            <form action="/delete" method="post">
              <div><input type="submit" value="Delete Podcast" /></div>
              <input type="hidden" name="podlistName" value="${fn:escapeXml(podlistName)}"/>
              <!-- not sure what to put here to give a reference to this entity-->
              <input type="hidden" name="podcast_id" value="$??"/>
            </form>
            <%
        }
    }
%>

    <form action="/add" method="post">
      <div><textarea name="podcast_url" rows="3" cols="60"></textarea></div>
      <div><input type="submit" value="Post Podcast" /></div>
      <input type="hidden" name="podlistName" value="${fn:escapeXml(podlistName)}"/>
    </form>

  </body>
</html>

谢谢

您似乎已经在使用“podcast\u id”-那么为什么不在请求中将其传递给
/delete
处理程序

podcast.getProperty("podcast_id")

我们似乎很难告诉您需要传递什么,因为它是您的应用程序-但您可能想要传递的是唯一引用实体的参数(无论是“key”、“podcast_id”等)。GAE中的每个实体都有一个唯一的密钥。

谢谢Matt!!我同意,我希望传递实体的唯一键,但我不确定如何在jsp文件中获得该键。(自从你发表评论以来,我已经更新了我的代码,podcast_id是我一直在胡乱处理的东西,它不起作用)根据,每个实体都有一个
getKey()
方法,它为实体提供唯一的键。我正在尝试这个方法:key-key=podcast.getKey();或者字符串key=podcast.getKey().toString();然后像这样传入:,它总是空的。我有语法问题吗?我是通过使用以下命令得到的:String key=KeyFactory.keyToString(podcast.getKey());然后在以下形式中:和servlet中:String keyString=req.getParameter(“key”);Key=KeyFactory.stringToKey(键串);数据存储。删除(键);
podcast.getProperty("podcast_id")