简体   繁体   English

如何使用 JSTL 将来自不同文本框的所有数据保存在一个数组中?

[英]How can I save all the data from different textboxs in an array, using JSTL?

I want to recieve any number of textbox values, and save it to an array with JSTL if it's possible.我想接收任意数量的文本框值,如果可能的话,将其保存到带有 JSTL 的数组中。

I generate all the texbox where numberAsked can be any number.我生成所有的 texbox,其中 numberAsked 可以是任何数字。

<c:if test="${param.buttonSend != null}">
        <form action="index.jsp" method="post">
                <c:forEach var = "i" begin = "1" end = "${param.numberAsked}">
                        <input type="text" name="textbox[]" class="form-control">
                </c:forEach>
            <button type="submit" name="buttonSave">Save</button>
        </form>
</c:if>

Now I want to save all the textboxs in an array.现在我想将所有文本框保存在一个数组中。

<c:if test="${param.buttonSave != null}">
    <c:set var="data" value="${request.getParameterValues(textbox)}"/>
    <c:forEach var = "item"  items="${param.data}">
        <c:out value="${item}"/>
    </c:forEach>
</c:if>

But, it doesn't work, how can I save all the data from all the generated textboxs in an array?但是,它不起作用,如何将所有生成的文本框中的所有数据保存在一个数组中? . .

"Now I want to save all the textboxs in an array." “现在我想将所有文本框保存在一个数组中。” You want to do scripting without a scriptlet.您想在没有 scriptlet 的情况下编写脚本。 The only way that I could think of is to use the useBean tag to create an ArrayList.我能想到的唯一方法是使用 useBean 标签来创建 ArrayList。 It's add method returns a boolean.它的 add 方法返回一个 boolean。 That is why I used empty if tags to insert the elements.这就是为什么我使用空的 if 标签来插入元素。 Here is a demonstration JSP.这是一个演示 JSP。

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<jsp:useBean id="myArray" class="java.util.ArrayList" />
<form method="post">
    <c:forEach var = "i" begin = "0" end = "5">
         <input type="text" name="textbox">
    </c:forEach>
    <button type="submit" name="buttonSave">Save</button>
</form>
    <c:forEach var = "item"  items="${paramValues}">
        <c:if test='${item.key == "textbox"}'>
            <c:forEach var='boxValue' items='${item.value}'> 
                <c:if test = '${myArray.add(boxValue)}'>
                </c:if>
            </c:forEach>
        </c:if>
    </c:forEach>
    <c:forEach var="element" items="${myArray}">
        ${element},
    </c:forEach>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM