Java – JSP El: dynamically create attribute names
•
Java
See the English answer > how to get value of bean property when property name itself is a dynamic variable1
I have a < C: foreach > loop in which I dynamically create bean accessors The skeleton is similar to:
<c:forEach var="type" items="${bean.positionTypes}"> ${bean.table} // append 'type' to the "table" property </c:forEach>
My problem is: I want to change ${bean. Table} according to the type For example, if the type is {"janitor", "Chef}, I want to produce:
${bean.tableJanitor} ${bean.tableChef}
How can I do this?
Solution
You can use braces [] to access bean properties using dynamic keys
${bean[property]}
So, according to your example:
<c:forEach var="type" items="${bean.positionTypes}"> <c:set var="property" value="table${type}" /> ${bean[property]} </c:forEach>
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码