views:

1562

answers:

3

Hello there. I have the following problem in Struts 2.

Let's assume i have a class like this

 class User {
       private String name;
       private String address;
       public String getName() { return name;}
       public String getAddress() { return address;}
    }

and a list of users available on ValueStack named : users and a list of user propertiesavailable also on ValueStack as: userProps. In this case userProps would be {name, address}.

Now I want to iterate over the users list and dinamycally access one user properties via userProps.

Here is how:

 <s:iterator value="#users" var="user">
    <s:iterator value="#userProps" var="prop">    
      **<%--- HOW to get user.name, user.address ???%>**
       <s:property value="#user.%{#prop}"/>
    </s:iterator>
 </s:iterator>

I dont know how to evaluate #user.#data to obtain the value for #user.name or #user.address ??

Thank you.

A: 

First, is the line in your example correct? Shouldn't you be trying:

<s:property value="#user.%{#prop}"/>

Second, have you tried using parens? Something like:

<s:property value="#user.(#prop)"/>

According to the OGNL docs it might do what you want (I haven't tried it myself).

http://www.opensymphony.com/ognl/html/LanguageGuide/paren.html

http://www.opensymphony.com/ognl/html/LanguageGuide/chainedSubexpressions.html

If that doesn't help, maybe OGNL's indexed property support would help. See the OGNL Object Indexed Properties section at the end.

http://www.opensymphony.com/ognl/html/LanguageGuide/indexing.html

Nate
Thank's for the suggestion, but it doesn't work.
Flueras Bogdan
A: 

It seems that Struts2 does not support double evaluation... just what I have tried to do.

But here is my workaround!

<% 
 ValueStack vs = ActionContext.getContext.getValueStack();
 %>
<s:iterator value="#users" var="user">
    <s:iterator value="#userProps" var="prop">    

       <%
        String userProp =(String) vs.findValue("#prop");
        Object userPropVal = vs.findValue("#users."+userProp);
        vs.getContext().put("userPropValKey", userPropVal);
       %>
       <s:property value="#userPropValKey"/>
    </s:iterator>
 </s:iterator>

Tadaaa.. and it works!

Flueras Bogdan
A: 

Great,double evaluation should have been provided.Anyways..what i did is..

1> Created method that takes params in Action class, Bec action is in Valustack,you can call methods directly passing values for which you need double validation.

and you can call from anywhere.. public Object findValueFromValueStack(String objectRef,String expression){

//get valuStack //Get value for expression //Then pass the value and scoped object reference to get the final value..

}

Kiran