【问题标题】:Need to select from a dropdown in JSP and add the choice to an ArrayList需要从 JSP 中的下拉列表中进行选择并将选择添加到 ArrayList
【发布时间】:2013-03-18 01:42:12
【问题描述】:

我有一个 sn-p,其中我在下拉列表中显示 OptionName 和各种选项。

现在,我想将用户所做的所有选择保存在 ArrayList 中,然后将其传递给另一个 Servlet。

<form method="post" action="ComputePrice">
<table border="1">
    <TR>
        <c:forEach items="${optionSets}" var="optionSetLocal">
            <TD>${optionSetLocal}</TD>

            <TD><select name="myChoice">
            <d:forEach items="${rawData.get(optionSetLocal)}"
                        var="optionsLocal">
                        <option>${optionsLocal}</option>
                    </d:forEach>
                    <select onclick value="${choices.add(myChoice)}"></select>
            </select></TD>
    </TR>

    </c:forEach>


</table>
<input type="Submit" value="Ok" />

现在,当我在另一个 Servlet 中打印 ArrayList“选择”时,我得到了一组全空值。如何使用页面上的选择填充ArrayList

为了更好地了解代码:optionSets 是ArrayList。 rawData 是 LinkedHashMap&lt;String, ArrayList&gt;,其中哈希是 optionSets arrayList 中的一个条目。

我不确定如何将选择存储在 ArrayList 中,以便在下一个 Servlet 中使用它进行进一步处理。

尝试附加图像,但由于点数不足而无法附加。

【问题讨论】:

  • 为什么不通过表单提交将多个选定的值传递给您的servlet?接收 POST 请求时,将数据添加到 arraylist 应该是您的 servlet 的任务之一。
  • 您不能使用表单提交传递 Arraylist、HashMap 等对象。将它们存储在会话中或使用隐藏字段
  • @Med:如何将多个选定的值传递给 servlet?我不知道帖子中有多少值要添加所有值。这是我最初的计划,但后来我不知道如何计算传入 servlet 的值的数量。
  • Hardik:arraylist 是 JSP 中使用的会话变量。但是,我想为 servlet 获取一组选定值。
  • 你不能简单地使用request.getParameterValues()来处理你的servlet提交的表单数据吗?

标签: jsp web-applications jstl


【解决方案1】:

我不知道这是否是一个简单的方法。但是尝试添加与选择列表大小相等的隐藏字段。选择时使用脚本设置字段值。 而在servlet中你可以得到它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-10
    • 2021-10-05
    • 1970-01-01
    相关资源
    最近更新 更多