当用户点击每个删除超链接或按钮时,如何在页面上调用JSP函数,以便我可以从数据库中删除该行的条目?究竟应该< a>或者< button>标签必须调用JSP函数吗?
请注意,我需要调用JSP函数,而不是JavaScript函数.
最简单的方法:只需让链接指向JSP页面并将行ID作为参数传递:<a href="delete.jsp?id=1">delete</a>
并在delete.jsp(我将明显的请求参数检查/验证放在一边):
<% dao.delete(Long.valueOf(request.getParameter("id"))); %>
然而,这是一个非常漂亮的poor practice(仍然是轻描淡写),原因有两个:
>修改服务器端数据的HTTP请求不应在GET之前完成,而应在POST之前完成.链接是隐式GET.想象一下,当像googlebot这样的网络抓取工具尝试关注所有删除链接时会发生什么.你应该使用< form method =“post”>和< button type =“submit”>用于删除操作.但是,您可以使用CSS将按钮设置为看起来像链接.编辑只是预加载项目以预编辑编辑表单的链接可以安全地获取.
>不鼓励使用scriptlet(那些< %%>事物)在JSP中放置业务逻辑(就像你调用的那样).您应该使用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. }
然后使用JSTL的JSP文件(只需在/ WEB-INF / lib中删除jstl-1.2.jar进行安装)以在表中显示产品,每行包含一个编辑链接和一个删除按钮:
<%@ 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>
将其命名为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>完全相同,只是更适合在attribtues中使用.
以下是产品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之前打开桌子.
也可以看看:
> Our servlets wiki page(还包含验证示例)
> doGet and doPost in Servlets
> Show JDBC ResultSet in HTML in JSP page using MVC and DAO pattern