Reading contents from application map dynamically IN JSP page. - struts 2.0

634 views Asked by At

I am using ApplicationAware in my struts 2.0 action class. So, i placed an entry in then application map as

application.put("animalDTO", animalDTO);

animalDTO:

public class AnimalDTO {

    private String name_N1;
    private String name_N2;
    private String name_N3;
    private String name_N4;

    // getters and setters

}

Now before going to my jsp page i am populating the values in each of the variables.

In JSP page: i am using iterator to get N1, N2 till N4. Now i need to create s:textfield and populate these variables content DYNAMICALLY.

ie.

<s:iterator value="application.nameList" var="nameList">
    <s:textfield theme="simple" value="%{application.animalDTO.name_#nameList}" />
</s:iterator>

Here, i am not getting any values populated.

If i hard code as -> value="%{application.animalDTO.**name_N1**}"

Then all the text-fields will be populated with value of name_N1, but that not what i need. I need for fetch the values dynamically.

Can anyone help me with this OGNL requirement? Thanks in advance

=====================

I tried this also, i created string variable which is of the required ognl format, but i dont know how to make the string as a lookup to valuestack. This is what i did:

<s:iterator value="application.nameList" var="nameList">
    <s:set var="varUrl" value="%{'application.animalDTO.name_' + #nameList}" />
    <s:property value="varUrl" />
    <s:textfield name="NAME__%{#nameList}" theme="simple" id="NAME-%{#nameList}" value="%{varUrl}" />
</s:iterator>

but instead the text box is getting values like:

application.animalDTO.name_1
application.animalDTO.name_2
application.animalDTO.name_3
application.animalDTO.name_4

what is need is the values corresponding to these variables.

Any idea how to convert this string as an object/key to lookup into valuestack.

1

There are 1 answers

0
abyin007 On

Finally i got the solution. I tried many trail and error methods. And finally hit the answers. We can dynamically read the property values by doing:

<s:iterator value="application.nameList" var="nameList">
    <s:textfield theme="simple" value="%{application.animalDTO['name_' + #nameList]}" />
</s:iterator>

I tried %{application.animalDTO['name_' + #nameList]} and it worked. :-)