Iterate ArrayList in JSP

后端 未结 5 1635
遥遥无期
遥遥无期 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:10

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

    Map foods = new HashMap();
    
    // 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:

    
    

    Or without JSTL:

    
    

    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.

提交回复
热议问题