问题描述
我想从JSP页面收集值,并借助JSTL将其动态传递给另一个JSP页面.我怎样才能做到这一点?
I want to collect values from a JSP page and pass it dynamically to another JSP page with the help of JSTL. How can I do this?
推荐答案
为此您使用了请求范围内的HashMap.
You use a request scoped HashMap for that.
1)在要插入或访问值列表的每个JSP中声明HashMap.
1) Declare the HashMap in each JSP you want to insert or access the list of values.
<jsp:useBean id="map" class="java.util.HashMap" scope="request"/>
注意:scope ="request"是使其能够在其他JSP中访问的原因.
Note: The scope="request" is what makes it accessible in other JSPs.
2)将信息填充到HashMap中
2) Stuff information into the HashMap
<c:set target="${requestScope.map}" property="city" value="${param.city}"/>
<c:set target="${requestScope.map}" property="state" value="${param.state}"/>
<c:set target="${requestScope.map}" property="phone" value="${param.phone}"/>
3a)现在,您只需执行以下操作即可拉出其他JSP中的值:
3a) You can now pull out the values in a different JSP by simply doing:
<c:out value="${requestScope.map['city']}"/>
-或-
3b)您还可以在其他JSP中对该HashMap进行迭代:
3b) You can also iterate over that HashMap in a different JSP:
<c:forEach items="${requestScope.map}" var="item">
${item.key} = ${item.value}<br/>
</c:forEach>
这篇关于我如何使用jstl在jsp中创建一个arraylist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!