How does one go about printing the values of a nested object/property in a map on a JSP page using JSTL?
<c:foreach items="${survey}" var="survey">
<c:out value="${survey.value}" />
</c:foreach>
Survey has a property called Questions, which is another bean, and I want to print those questions survey.questions.getId() or survey.questions.getTitle()), how would that <c:forEach> statement look like?
In my case, ${survey} is a Map not a Collection.
If your nested property is a single object instance, you just reference it directly, like:
That assumes that you have a collection of
Surveyobjects bound to thesurveysattribute, and that eachSurveyhas a title. It will print the title of each survey.If your nested property is a collection of objects, then you use a
forEachloop to iterate them, just like in your example.That will print the title of each
Question, assuming that you have a singleSurveyobject bound to thesurveyattribute, and that theSurveyobject has a collection ofQuestionobjects as a field (with an appropriate getter method, i.e.getQuestions()).You can also have nested loops, like:
That will print the title of every
Surveyalong with the title of eachQuestionin eachSurvey.And if for some reason you decide to pass a
Map, you can do: