I am trying to dynamically generate content using JSP.
I have a <c:forEach>
loop within which I dynamically create bean accessors. The skeleton resembles this:
<c:forEach var="type" items="${bean.positionTypes}">
${bean.table} // append 'type' to the "table" property
</c:forEach>
My problem is: I want to change the ${bean.table}
based on the type. For example, if the types were {"Janitor", "Chef}
, I want to produce:
${bean.tableJanitor}
${bean.tableChef}
How can I achieve this?
You can use the brace notation []
to access bean properties using a dynamic key.
${bean[property]}
So, based on your example:
<c:forEach var="type" items="${bean.positionTypes}">
<c:set var="property" value="table${type}" />
${bean[property]}
</c:forEach>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With