我有一个jsp文件,它显示了数据存储区中的一些记录,我希望在每个条目旁边都有一个删除按钮,将其从数据库中删除。
所以,我知道Servelet会进行实际的删除,但我不确定在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>
这是我的Servlet:
package com.aol.sharepodder;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;
import java.util.logging.Logger;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
public class AddPodcastServlet extends HttpServlet {
private static final Logger log = Logger.getLogger(AddPodcastServlet.class.getName());
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
UserService userService = UserServiceFactory.getUserService();
User user = userService.getCurrentUser();
String podlistName = req.getParameter("podlistName");
Key podlistKey = KeyFactory.createKey("Podlist", podlistName);
String podcast_url = req.getParameter("podcast_url");
Date date = new Date();
Entity podcast = new Entity("Podcast", podlistKey);
podcast.setProperty("user", user);
podcast.setProperty("date", date);
podcast.setProperty("podcast_url", podcast_url);
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
datastore.put(podcast);
resp.sendRedirect("/podlist.jsp?podlistName=" + podlistName);
}
}
谢谢!
答案 0 :(得分:2)
您好像已经在使用“podcast_id” - 那么为什么不将请求中的那个传递给/delete
处理程序?
podcast.getProperty("podcast_id")
我们很难告诉你需要传递什么,因为它是你的应用程序 - 但你可能想要的是传递一个唯一引用该实体的参数(无论是“key”,“podcast_id”,等等)。 GAE中的每个实体都有一个唯一的密钥。