Iterate ArrayList in JSP

后端 未结 5 1608
遥遥无期
遥遥无期 2020-12-03 15:54

I have two arraylists in my class and I want to send it to my JSP and then iterate the elements in arraylist in a select tag.

Here is my class:

packa         


        
相关标签:
5条回答
  • 2020-12-03 16:00

    There are multiple ways this can be done (with some changes in your scheme)

    Using JSTL:

    1. Create a bean with two fields as food and food_code

      public class Food{ private String food; private String food_code; // Setter/getters follows }

    Now the arraylist available on the page will be list Food objects. In the JSP code, you can use following:

    <select name="fooditems">
        <c:forEach items="${list}" var="item">
            <option value="${item.food_code}">${item.food}</option>
        </c:forEach>
    </select>
    

    If you are using struts:

    <html:select property="fooditems" >
    <html:optionsCollection property="list" label="food" value="food_code" />
    </html:select>
    

    Here list is object key which will be used to retrieve the list from context (page/session)

    0 讨论(0)
  • 2020-12-03 16:07

    You can retrieve the list in JSP as

    <select id="food" name="fooditems">
    
      <c:forEach items="${listname}" var="food" >
    
         <option value="${food}"> ${food} </option>
    
      </c:forEach>
    
    </select>
    
    0 讨论(0)
  • 2020-12-03 16:08

    You can use JSTL's foreach.

    <c:forEach items="${foodItems}" var="item">
       ${item}
    </c:forEach>
    

    You need also to import JSTL core:

    <%@ taglib prefix="c" 
           uri="http://java.sun.com/jsp/jstl/core" %>
    
    0 讨论(0)
  • 2020-12-03 16:10

    It would be better to use a java.util.Map to store the key and values instead of two ArrayList, like:

    Map<String, String> foods = new HashMap<String, String>();
    
    // here key stores the food codes
    // and values are that which will be visible to the user in the drop-down
    foods.put("man", "mango");
    foods.put("app", "apple");
    foods.put("gra", "grapes");
    
    // if this is your servlet or action class having access to HttpRequest object then
    httpRequest.setAttribute("foods", foods); // so that you can retrieve in JSP
    

    Now to iterate the map in the JSP use:

    <select id="food" name="fooditems">
        <c:forEach items="${foods}" var="food">
            <option value="${food.key}">
                ${food.value}
            </option>
        </c:forEach>
    </select>
    

    Or without JSTL:

    <select id="food" name="fooditems">
    
    <%
    Map<String, String> foods = (Map<String, String>) request.getAttribute("foods");
    
    for(Entry<String, String> food : foods.entrySet()) {
    %>
    
        <option value="<%=food.getKey()%>">
            <%=food.getValue() %>
        </option>
    
    <%
    }
    %>
    
    </select>
    

    To know more about iterating with JSTL here is a good SO answer and here is a good tutorial about how to use JSTL in general.

    0 讨论(0)
  • 2020-12-03 16:12
    <c:forEach items="${list}" var="foodItem">
     ${foodItem.propertyOfBean}
    </c:forEach>
    

    This will solve your problem.

    0 讨论(0)
提交回复
热议问题