最简单的方法:只需让链接指向 JSP 页面并传递行 ID 作为参数:
<a href="delete.jsp?id=1">delete</a>
And in delete.jsp
(我将明显的请求参数检查/验证放在一边):
<% dao.delete(Long.valueOf(request.getParameter("id"))); %>
然而,这是一个漂亮的不良做法(这仍然是轻描淡写),有两个原因:
-
修改服务器端数据的 HTTP 请求不应由GET,但是通过POST。链接是隐式 GET。想象一下,当像 googlebot 这样的网络爬虫尝试跟踪所有删除链接时会发生什么。你应该使用<form method="post">
and a <button type="submit">
用于删除操作。不过,您可以使用 CSS 将按钮设置为看起来像链接的样式。可以安全地获取仅预加载项目以预填写编辑表单的编辑链接。
-
将业务逻辑(功能正如你所说的那样)在 JSP 中使用小脚本 (those <% %>
的事情)感到气馁。你应该使用Servlet控制、预处理和后处理 HTTP 请求。
由于您在问题中没有提及有关 servlet 的任何信息,因此我怀疑您已经在使用 scriptlet 从数据库加载数据并将其显示在表中。这也应该由 servlet 来完成。
这是一个基本的启动示例,说明如何完成这一切。我不知道表数据代表什么,所以让我们看看Product
举个例子。
public class Product {
private Long id;
private String name;
private String description;
private BigDecimal price;
// Add/generate public getters and setters.
}
然后JSP文件使用JSTL(只需放下jstl-1.2.jar in /WEB-INF/lib
安装它)以显示products在每行都有一个编辑链接和一个删除按钮的表格中:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
...
<form action="products" method="post">
<table>
<c:forEach items="${products}" var="product">
<tr>
<td><c:out value="${fn:escapeXml(product.name)}" /></td>
<td><c:out value="${product.description}" /></td>
<td><fmt:formatNumber value="${product.price}" type="currency" currencyCode="USD" /></td>
<td><a href="${pageContext.request.contextPath}/product?edit=${product.id}">edit</a></td>
<td><button type="submit" name="delete" value="${product.id}">delete</button></td>
</tr>
</c:forEach>
</table>
<a href="${pageContext.request.contextPath}/product">add</a>
</form>
请注意方法的差异:编辑链接会触发 GET 请求,并将项目的唯一标识符作为请求参数。然而,删除按钮会触发 POST 请求,从而将项目的唯一标识符作为按钮本身的值传递。
另存为products.jsp
并把它放进去/WEB-INF
文件夹,以便不能通过 URL 直接访问它(因此最终用户被迫为此调用 servlet)。
Servlet 大致如下所示(为简洁起见,省略了验证):
@WebServlet("/products")
public class ProductsServlet extends HttpServlet {
private ProductDAO productDAO; // EJB, plain DAO, etc.
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Product> products = productDAO.list();
request.setAttribute("products", products); // Will be available as ${products} in JSP.
request.getRequestDispatcher("/WEB-INF/products.jsp").forward(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String delete = request.getParameter("delete");
if (delete != null) { // Is the delete button pressed?
productDAO.delete(Long.valueOf(delete));
}
response.sendRedirect(request.getContextPath() + "/products"); // Refresh page with table.
}
}
以下是添加/编辑表单的方法/WEB-INF/product.jsp
可以看起来像:
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
...
<form action="product" method="post">
<label for="name">Name</label>
<input id="name" name="name" value="${fn:escapeXml(product.name)}" />
<br/>
<label for="description">Description</label>
<input id="description" name="description" value="${fn:escapeXml(product.description)}" />
<br/>
<label for="price">Price</label>
<input id="price" name="price" value="${fn:escapeXml(product.price)}" />
<br/>
<button type="submit" name="save" value="${product.id}">save</button>
</form>
The fn:escapeXml()
只是为了防止XSS攻击当编辑数据重新显示时,它的作用与<c:out>
,只更适合在属性中使用。
以下是如何product
servlet 可以看起来像(再次,为简洁起见,省略了转换/验证):
@WebServlet("/product")
public class ProductServlet extends HttpServlet {
private ProductDAO productDAO; // EJB, plain DAO, etc.
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String edit = request.getParameter("edit");
if (edit != null) { // Is the edit link clicked?
Product product = productDAO.find(Long.valueOf(delete));
request.setAttribute("product", product); // Will be available as ${product} in JSP.
}
request.getRequestDispatcher("/WEB-INF/product.jsp").forward(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String save = request.getParameter("save");
if (save != null) { // Is the save button pressed? (note: if empty then no product ID was supplied, which means that it's "add product".
Product product = (save.isEmpty()) ? new Product() : productDAO.find(Long.valueOf(save));
product.setName(request.getParameter("name"));
product.setDescription(request.getParameter("description"));
product.setPrice(new BigDecimal(request.getParameter("price")));
productDAO.save(product);
}
response.sendRedirect(request.getContextPath() + "/products"); // Go to page with table.
}
}
部署并运行它。您可以通过以下方式打开表格http://example.com/contextname/products.
也可以看看:
-
我们的 servlet wiki 页面(还包含一个验证示例)
- Servlet 中的 doGet 和 doPost
- 使用 MVC 和 DAO 模式在 JSP 页面中的 HTML 中显示 JDBC 结果集