how do i create an arraylist inside jsp using jstl

送分小仙女□ 提交于 2019-12-11 09:12:30

问题


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?


回答1:


You use a request scoped HashMap for that.

Example

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"/>  

Note: The scope="request" is what makes it accessible in other JSPs.

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) You can now pull out the values in a different JSP by simply doing:

<c:out value="${requestScope.map['city']}"/>

-or-

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>


来源:https://stackoverflow.com/questions/25201995/how-do-i-create-an-arraylist-inside-jsp-using-jstl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!