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

I have an HTML table with rows fetched from a database displayed in that table. I want the user to be able to delete a row by clicking on a delete hyperlink or button besides each row.

How do I invoke a JSP function on the page, when the user clicks on each of those delete hyperlinks or button, so that I can delete that row's entry from the database? What exactly should the or tag have to call the JSP function?

Note that I need to call a JSP function, not a JavaScript function.

解决方案

Simplest way: just let the link point to a JSP page and pass row ID as parameter:

delete

And in delete.jsp (I'm leaving obvious request parameter checking/validating aside):

This is however a pretty poor practice (that was still an understatement) and due to two reasons:

HTTP requests which modifies the data on the server side should not be done by GET, but by POST. Links are implicit GET. Imagine what would happen when a web crawler like googlebot tries to follow all delete links. You should use a

and a for the delete action. You can however use CSS to style the button to look like a link. Edit links which just preload the item to prefill the edit form can safely be GET.

Putting business logic (functions as you call it) in a JSP using scriptlets (those things) is discouraged. You should use a Servlet to control, preprocess and postprocess HTTP requests.

Since you didn't tell any word about a servlet in your question, I suspect that you're already using scriptlets to load data from DB and display it in a table. That should also be done by a servlet.

Here's a basic kickoff example how to do it all. I have no idea what the table data represents, so let take Product as an example.

public class Product {

private Long id;

private String name;

private String description;

private BigDecimal price;

// Add/generate public getters and setters.

}

And then the JSP file which uses JSTL (just drop jstl-1.2.jar in /WEB-INF/lib to install it) to display the products in a table with an edit link and a delete button in each row:

...

editdelete

add

Note the difference of approach: the edit link fires a GET request with an unique identifier of the item as request parameter. The delete button however fires a POST request instead whereby the unique identifier of the item is passed as value of the button itself.

Save it as products.jsp and put it in /WEB-INF folder so that it's not directly accessible by URL (so that the enduser is forced to call the servlet for that).

Here's how the servlet roughly look like (validation omitted for brevity):

@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 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.

}

}

Here's how the add/edit form at /WEB-INF/product.jsp can look like:

...

Name

Description

Price

save

The fn:escapeXml() is just there to prevent XSS attacks when edit data is redisplayed, it does exactly the same as , only better suitable for usage in attribtues.

Here's how the product servlet can look like (again, conversion/validation omitted for brevity):

@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.

}

}

Deploy and run it. You can open the table by http://example.com/contextname/products.

See also:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将当前页面的值传递到$.acooly.divdialog,可以通过以下步骤实现: 1. 在页面定义一个按钮,用于触发打开$.acooly.divdialog的操作。 2. 在按钮点击事件,获取需要传递到$.acooly.divdialog的值,并将这些值存储在一个JavaScript对象。 3. 调用$.acooly.divdialog的open方法打开弹框,同时将第2步存储的JavaScript对象作为参数传递给$.acooly.divdialog。 4. 在$.acooly.divdialog,通过参数获取第3步传递过来的JavaScript对象,并将其的值填充到弹。 具体实现代码如下: ```javascript // 页面按钮点击事件 $('#btn-open-dialog').click(function() { // 获取需要传递到弹的值 var name = $('#name').val(); var age = $('#age').val(); // 将这些值存储在一个JavaScript对象 var data = { name: name, age: age }; // 打开弹框,并将数据对象传递给$.acooly.divdialog $.acooly.divdialog.open({ title: '弹框标题', url: '/path/to/dialog', data: data }); }); // 弹页面,通过参数获取传递过来的数据对象 $(function() { var data = $.acooly.divdialog.options.data; // 将数据对象的值填充到弹 $('#name').val(data.name); $('#age').val(data.age); }); ``` 在上面的代码,`#btn-open-dialog`是页面按钮,`#name`和`#age`是输入框,`/path/to/dialog`是弹框的页面地址。当用户点击按钮时,页面会获取输入框的值,并将这些值存储在一个JavaScript对象。然后调用$.acooly.divdialog的open方法打开弹框,并将数据对象传递给它。在弹框的页面,可以通过`$.acooly.divdialog.options.data`获取传递过来的数据对象,然后将其的值填充到弹

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值