How to use jstl el to call a dynamic nested property

769 Views Asked by At

if you wanna call the getter method from a specific bean using a dynamic key you use like that:

${bean[getterName]}

but if you wanna call double nested, or triple nested properties with a dynamic name how does it works, is it possible?

${bean.propertyA.propertyB} WORKS
${bean[propertyA.propertyB]} DOES NOT WORKS
<c:set var="dynamicKey" value="propertyA.propertyB" />
${bean[dynamicKey]} DOES NOT WORKS

UPDATE:

For now we're handling like that:

<c:forTokens items="${property}" delims="." var="item">   
    <c:set var="value" value="${value[item]}" />
</c:forTokens>
2

There are 2 best solutions below

0
On BEST ANSWER

After 2 years we are leaving like this since it does not seems to have a big impact on performance.

<c:forTokens items="${property}" delims="." var="item"> <c:set var="value" value="${value[item]}" /> </c:forTokens>

0
On

Dot notation vs brackets notation with nested properties:

${bean.propertyA.propertyB}
${bean[propertyA.propertyB]} ==> Not right, instead
${bean["propertyA"]["propertyB"]}

Your example with JSTL:

<c:set var="dynamicKey" value="${bean['propertyA']['propertyB']}" />
<c:out value="${dynamicKey}" />