将多个动态HTML输入的提交值收集并保存回servlet中 [英] Collect and save submitted values of multiple dynamic HTML inputs back in servlet

查看:52
本文介绍了将多个动态HTML输入的提交值收集并保存回servlet中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过遍历列表并在HTML输入标记中输出bean属性,我可以使用JSTL以JSP格式显示bean的ArrayList.

I am able to display an ArrayList of beans in a JSP form using JSTL by looping through the list and outputting the bean properties in a HTML input tag.

<c:forEach items="${listOfBeans}" var="bean">
  <tr>
    <td><input type="text" id="foo" value="${bean.foo}"/></td>  
    <td><input type="text" id="bar" value="${bean.bar}"/></td>                     
  </tr>
</c:forEach>

我该如何编码JSP,以便在页面提交时,更新后的值位于ArrayList的适当项中?

How do I code the JSP so that when the page submits then the updated values are in the appropriate item of the the ArrayList?

推荐答案

给出此简化模型:

public class Item {
    private Long id;
    private String foo;
    private String bar;
    // ...
}

${items}List<Item>的情况下,您可以按照以下方法进行操作:

Here's how you can do it provided ${items} is List<Item>:

<c:forEach items="${items}" var="item">
    <tr>
        <td>
            <input type="hidden" name="id" value="${item.id}" />
            <input name="foo_${item.id}" value="${fn:escapeXml(item.foo)}" />
        </td>  
        <td>
            <input name="bar_${item.id}" value="${fn:escapeXml(item.bar)}" />
        </td>
    </tr>
</c:forEach>

(请注意fn:escapeXml()作为 XSS的重要性攻击预防)

因此,基本上,您需要将商品的唯一标识符设置为每一行中的隐藏输入字段,如上面的代码片段所示:

So, basically, you need to set the item's unique identifier as a hidden input field in each row as shown in above snippet:

<input type="hidden" name="id" value="${item.id}" />

并且您应该依次将此id用作同一行中所有输入字段的name的后缀,例如:

And you should in turn use this id as suffix of name of all input fields in the same row such as:

<input name="foo_${item.id}" ... />

在servlet中,可以按request.getParameterValues()收集所有行中所有<input type="hidden" name="id" ...>的值.只需对其进行循环,然后按id捕获各个输入.

In the servlet, you can collect all values of <input type="hidden" name="id" ...> from all rows by request.getParameterValues(). Just loop over it and then grab the individual inputs by id.

for (String id : request.getParameterValues("id")) {
    String foo = request.getParameter("foo_" + id);
    String bar = request.getParameter("bar_" + id);
    // ...
}

您也可以在没有id的情况下完成所有操作,并像name="foo"request.getParameterValues("foo")这样按名称捕获所有输入作为数组,但是请求参数的顺序不受您的控制. HTML表单将按顺序发送它,但是最终用户可以轻松地操纵该命令.

You can also do this all without that id and grab all inputs by name as an array like so name="foo" and request.getParameterValues("foo"), but the ordering of request parameters is not under your control. HTML forms will send it in order, but the enduser can easily manipulate the order.

这里不需要JavaScript.

No need for JavaScript mess here.

  • Show JDBC ResultSet in HTML in JSP page using MVC and DAO pattern
  • ServletRequest.getParameterMap() returns Map<String, String[]> and ServletRequest.getParameter() returns String?
  • Send an Array with an HTTP Get

这篇关于将多个动态HTML输入的提交值收集并保存回servlet中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆