栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

最简单的方法:只需让链接指向JSP页面,然后将行ID作为参数传递即可:

<a href="delete.jsp?id=1">delete</a>

delete.jsp
(我将明显的请求参数检查/验证放在一边)中

<% dao.delete(Long.valueOf(request.getParameter("id"))); %>

但是,这是一个非常差的做法(仍然被轻描淡写),原因有两个:

  1. 修改服务器端数据的HTTP请求不应由GET完成,而应由POST完成。链接是隐式的GET。想象一下,当类似googlebot的网络抓取工具尝试跟踪所有删除链接时会发生什么。您应该将a

    <form method="post">
    和a
    <button type="submit">
    用于删除操作。但是,您可以使用CSS设置按钮样式,使其看起来像一个链接。只需预加载项目以预填充编辑表单的编辑链接就可以安全地进行GET操作。

  2. 把业务逻辑( 功能 使用的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.}

然后,使用JSTL的JSP文件(只需将jstl-1.2.jar放入其中

/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>

请注意方法的区别:编辑链接会触发一个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>

fn:escapeXml()
是只是为了防止XSS攻击时,编辑数据再次显示,它完全一样的
<c:out>
,只有更好的适用于attribtues使用。

这是

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打开表格。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/391849.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号