Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过单击JSP页面中的超链接或按钮将当前项传递给Java方法?_Jsp - Fatal编程技术网

如何通过单击JSP页面中的超链接或按钮将当前项传递给Java方法?

如何通过单击JSP页面中的超链接或按钮将当前项传递给Java方法?,jsp,Jsp,我有一个HTML表,其中包含从该表中显示的数据库中获取的行。我希望用户能够通过单击每行旁边的删除超链接或按钮来删除一行 当用户单击每个删除超链接或按钮时,如何调用页面上的JSP函数,以便从数据库中删除该行的条目?或标记必须调用JSP函数的具体内容是什么 请注意,我需要调用一个JSP函数,而不是JavaScript函数。最简单的方法是:让链接指向一个JSP页面,并将行ID作为参数传递: <a href="delete.jsp?id=1">delete</a&g

我有一个HTML表,其中包含从该表中显示的数据库中获取的行。我希望用户能够通过单击每行旁边的删除超链接或按钮来删除一行

当用户单击每个删除超链接或按钮时,如何调用页面上的JSP函数,以便从数据库中删除该行的条目?
标记必须调用JSP函数的具体内容是什么


请注意,我需要调用一个JSP函数,而不是JavaScript函数。

最简单的方法是:让链接指向一个JSP页面,并将行ID作为参数传递:

<a href="delete.jsp?id=1">delete</a>
然而,这是一个相当不错的说法(这仍然是一个轻描淡写的说法),原因有两个:

  • 修改服务器端数据的HTTP请求不应由,而应由。链接是隐式的。想象一下,当像谷歌机器人这样的网络爬虫试图跟踪所有删除链接时会发生什么。您应该使用
    来执行删除操作。但是,您可以使用CSS将按钮样式设置为链接。只需预加载项目以预填充编辑表单的编辑链接就可以安全地获得

  • 不鼓励使用scriptlet(那些
    东西)将业务逻辑(您所称的函数)放入JSP中。您应该使用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.
    }
    
    然后是JSP文件,它使用(只需插入
    /WEB-INF/lib
    即可安装)在表格中显示产品,每行有一个编辑链接和一个删除按钮:

    <%@ 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>
    
    下面是位于
    /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>
    
    部署并运行它。你可以在旁边打开桌子

    另请参见:

    • (还包含一个验证示例)

    谢谢。我将尝试实现它,因为它更有意义。我现在的代码非常混乱,需要一些重组来实现您提出的方法。谢谢你的参考链接,不客气。是的,把所有东西都塞进一个大的JSP文件中是相当混乱的,而且很难维护:)祝你好运。
    @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.
        }
    
    }
    
    <%@ 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>
    
    @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.
        }
    
    }