データストアからいくつかのレコードを表示している jsp ファイルがあり、データベースから削除する各エントリの横に削除ボタンが必要です。
したがって、サーブレットが実際の削除を行うことはわかっていますが、データベースで検索するエンティティ (項目) をサーブレットが認識できるように、jsp の削除ボタンに何を配置すればよいかわかりません。
これを行うより良い方法はありますか?
これが私の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>
ここに私のサーブレットがあります:
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);
}
}
ありがとう!!