2

I have some objects of same type in action class..

E.g.:

LivingThings fish = new LivingThings("FISH");    
LivingThings dog = new LivingThings("DOG");    
LivingThings lion = new LivingThings("LION");

The contents of each objects will be different.

Also in the same action class I have a list with values as the name of the objects i.e.

ArrayList<String> animalsList = new ArrayList<String>();    
animalsList.add("fish");
animalsList.add("dog");
animalsList.add("lion");

=====

Now in the jsp page, I need dynamically get the object contents using the animalsList contents. i.e.

<s:iterator value="animalsList" id="eachAnimal">
    <s:property value="#eachAnimal.lifespan" />
</s:iterator>

Here all I am trying to do is, instead of directly giving the code like

<s:property value="fish.lifespan" />
<s:property value="dog.lifespan" />
<s:property value="lion.lifespan" />

Somehow, I need append the object name from the animalsList list. Is it possible in struts 2.0. I am little bit confused with OGNL concept. I tried these scenarios:

<s:iterator value="animalsList" id="eachAnimal">
    <s:property value="#eachAnimal.lifespan" />
    <s:property value="%{eachAnimal}.lifespan" />
    <s:property value="%{#eachAnimal}.lifespan" />
</s:iterator>

Can someone give me suggestions?

Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
abyin007
  • 361
  • 2
  • 4
  • 14
  • Using 2.0, IIRC `%{#eachAnimal.lifespan}` should work; are you saying it doesn't? I'd recommend not using such an old version of S2, though. – Dave Newton Jun 03 '13 at 13:21
  • Why cannot you store `LivingThings` in your list instead of strings? Anyway you can use OGNL evaluation like in this question: http://stackoverflow.com/q/16712395/1700321. – Aleksandr M Jun 03 '13 at 19:07
  • @DaveNewton yes, its not working.. i am using the latest jar files.. am using struts2-core-2.3.14.jar.... – abyin007 Jun 07 '13 at 11:15
  • @AleksandrM i could hv used LivingThings as list, but my scenario is this.. thts why.. but when i tried with its working – abyin007 Jun 07 '13 at 11:19

1 Answers1

1

Maybe it is not the best way to handle such case, but what you asking for could be done something like so:

<s:iterator value="animalsList" id="eachAnimal">
  <s:property value="#attr[#eachAnimal].lifespan" />
</s:iterator>
Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
  • thanks a lot Aleksandr, its working now... :-), but why did you mention, its not the best way.. any drawback or performance issues.. anything.. – abyin007 Jun 07 '13 at 11:21
  • @abyin007: It may be not the best way, because I don't know what you are trying to achieve. BTW accept/upvote answers that helped you. – Aleksandr M Jun 10 '13 at 07:57