0

In a code that follows Spring MVC, I have 2 lists in my java code that I would like to use in my JSP view. I set them like this:

public ModelAndView circularListView(HttpServletRequest request, Principal principal, HttpSession session, Locale locale, ModelAndView mav, int startOffset) {

    //some code

    mav.addObject("circularsList", circularsList);
    mav.addObject("documentNameList", documentNameList);        


    return mav;
    }

Now I would like to iterate on both lists circularsList and documentNameList in a single for loop in the JSP page, but it seems that I can only set one variable name like this:

        <c:forEach items="${circularsList}" var="circular" varStatus="status">

To access a value in the second list which is documentNameList, I do it like this:

        <input type="hidden" id="circDocNam" value="${documentNameList[status.index]}"/>

Unfortunately, this does not seem to work, and the value in the above line is empty.

What to do?

In conclusion: How to access a list item using its index in JSP?

Traveling Salesman
  • 2,209
  • 11
  • 46
  • 83
  • 1
    does [this](https://stackoverflow.com/questions/8637285/get-specific-element-in-a-list-or-array-using-el) as well as [this](https://stackoverflow.com/questions/24019845/getting-elements-of-arraylist-based-on-index-in-jstl) answer your question ? – Swati Feb 05 '20 at 13:08
  • Yeah, kind of....except for the indexing. Anyway, I used this: value=" – Traveling Salesman Feb 09 '20 at 09:26

1 Answers1

0

This worked for me:

            <input type="hidden" id="circDocNam" value="<c:out value="${documentNameList[status.index]}"/>"/>
Traveling Salesman
  • 2,209
  • 11
  • 46
  • 83