Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSP EL: dynamic creation of property name [duplicate]

Tags:

java

jsp

el

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?

like image 851
bulk Avatar asked Sep 15 '10 21:09

bulk


1 Answers

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>
like image 55
BalusC Avatar answered Sep 22 '22 10:09

BalusC