Search code examples
jspstruts2ognl

Struts2: Using a variable value as property name


In my jsp, I have a label inside an iterator over a list of objects which have a "text" property:

<s:iterator value="survey.questions">
   <s:label for="d_%{id}" value="%{text}"></s:label>

   .....

<s:iterator/>

Now, since I want to localize the text inside the label, I added a property for every single language I support (textEN, textIT, etc...), and so I want the property name to be read from a local variable I previously set in the page, for example with:

<s:set var="loc" value="textEN" />

I cannot manage to do this, I tried with the following expression

<s:label for="d_%{id}" value="%{#loc}"></s:label>   

But it prints nothing.


Solution

  • SOLVED.

    I had to add an id to the iterator

    <s:iterator value="survey.questions" id="question">
    

    and then directly access the property with this syntax:

    <s:label for="d_%{id}" value="%{#question[#loc]}"/>
    

    Thank you all.