我有一个 jsp 文件,它显示数据存储区中的一些记录,我希望在每个条目旁边都有一个删除按钮,可以将其从数据库中删除。
所以,我知道 Servlet 会进行实际的删除,但我不确定在 jsp 中使用删除按钮放什么,以便我的 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);
}
}
谢谢!!