How to access a list of arrays in jsp if I pass a bean

I am new to JSTL. How can I use JSTL <c:foreach>inside jsp if I go below the bean sample

class B{
    private String value="";
    private ArrayList arrayVals;
    public String getvalue(){
        return value;
    }
    public String getarrayVals(){
        return arrayVals;
    }
}

I will pass only Bean "B". I tried as below, but jsp is not compiled. Please help me.

<c:forEach items="${B.getarrayVals}" var="book"> 
    <c:out value="{book.title}"/> 
</c:forEach>
+5
source share
1 answer

First of all, it getarrayVals()should be written getarrayVals(), and it should obviously return List, not a string.

Now suppose the servlet or action sets the “b” attribute of type B as follows:

request.setAttribute("b", theBInstance);

and then go to JSP, you can access the list in attribute "b" as follows:

${b.arrayVals}

B , . foo, ${foo.arrayVals}. toString . 3 ,

${b.arrayVals[3]}

, c: forEach:

<c:forEach items="${b.arrayVals}" var="element">
    The element value is ${element} <br/>
</c:forEach>
+10

All Articles