我有一个HTML表,该表中显示了从数据库中提取的行。我希望用户能够通过单击每行旁边的删除超链接或按钮来删除行。
当用户单击每个delete超链接或按钮时,如何在页面上调用JSP函数,以便从数据库中删除该行的条目?要调用<a>函数,<button>或JSP标记到底应该有什么呢?
注意,我需要调用一个JSP函数,而不是JavaScript函数。
发布于 2010-06-20 20:48:50
最简单的方法:让链接指向JSP页面,并将行ID作为参数传递:
<a href="delete.jsp?id=1">delete</a>在delete.jsp中(我将明显的请求参数检查/验证放在一边)
<% dao.delete(Long.valueOf(request.getParameter("id"))); %>然而,这是一个相当不错的poor practice (这仍然是轻描淡写的),原因有两个:
修改服务器端数据的
<form method="post">和<button type="submit">。但是,您可以使用CSS设置按钮的样式,使其看起来像一个链接。可以安全地获取仅预加载项以预填充编辑表单的编辑链接。<% %>的东西)。您应该使用Servlet来控制、预处理和后处理HTTP请求。由于您在问题中没有提到任何关于servlet的内容,因此我怀疑您已经在使用scriptlet从DB加载数据并将其显示在表中。这也应该由servlet来完成。
这是一个基本的启动示例,如何做到这一切。我不知道表数据代表什么,所以让我们以Product为例。
public class Product {
private Long id;
private String name;
private String description;
private BigDecimal price;
// Add/generate public getters and setters.
}然后使用JSTL (只需将jstl-1.2.jar放在/WEB-INF/lib中安装)的JSP文件在表中显示产品,其中每一行都有一个编辑链接和一个删除按钮:
<%@ 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请求。但是,delete按钮将触发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>当编辑数据被重新显示时,fn:escapeXml()只是为了防止XSS attacks,它的作用与<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打开该表。
另请参阅:
的示例
https://stackoverflow.com/questions/3078701
复制相似问题