tags:

views:

32

answers:

1

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?

+1  A: 

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>
BalusC
Thank you. This does indeed work.
bulk