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?
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>
Related
How can I retrieve a dynamically generated name tag for <s:set> on Struts 2?
E.g.:
In my action class I have an object (currency) that have this fields: 'id' and 'symbol'.
So in my view page I can use this:
<s:iterator value="currency">
<s:set name="var_%{id}" value="symbol"/>
<s:set name="total_%{id}" value="%{0.0}"/>
</s:iterator>
So I can use this to create these PageContext variables like "var_BRL", "var_USD", "var_MXN" and "total_BRL", "total_USD", "total_MXN" etc. If I write the following code:
<s:property value="#var_USD"/> = <s:property value="#total_USD"/>
I'm able to retrive a result like:
USD = 0.0
I'm using these #total_XXX variables to sum some values under some conditions presented by another iterator, in a way that I'll have at the end of this other iteraction, a result of the total spent in every currency (BRL, USD, MXN etc).
But when I try to retrieve these values dynamically, nothing is rendered. Following is the code I'm using to retrieve the values from these variables, at the end of my page. I don't understand OGNL very well, so I've tryed different arrangements like the ones below and had no success with any of them:
<s:iterator value="currency">
<s:property value="#var_%{id}"/> = <s:property value="#total_%{id}"/>
<s:property value="#%{var_%{id}}"/> = <s:property value="#%{total_%{id}}"/>
<s:property value="%{#var_%{id}}"/> = <s:property value="%{#total_%{id}}"/>
</s:iterator>
Is there a solution to retrieve these values in the PageContext? Or can I only solve this inside my Action? I've searched so many posts but I couldn't find anything.
Thank you!
LZ
discussing with a friend, I found the proper OGNL expression to call for the dynamically created variables names at the end of my code:
<s:iterator value="currency">
<s:property value="#attr['var_'+id]"/> = <s:property value="#attr['total_'+id]"/><br>
</s:iterator>
I have a generic jsp which is in charge of printing some values, the values depends on a parameter.
For example, I have an object car with and attribute color and a object house with the same attribute, I want to use the same jsp receiveng the name of the object as a parameter.
<jsp:include page="/jsp/prv/generic/PaintColor.jsp" >
<jsp:param name="element" value="car" />
</jsp:include>
So, I want to print the value of car color. I try to do it like this:
<s:set var="propertyName">${param.element}.color<s:set/>
<s:property value="%{#attr.propertyName}" />
It doesnt work, but if I do
<s:textfield name="%{#attr.propertyName}"/>
it works perfect.
How can I use the parametrized name in avalue?
I finally solve it accesing the value from a java scriptlet
I am using Struts2.
<s:iterator value="empReportFields" var="empReportField"
<s:select name="%{#empReportField.fieldName}" list="%{#empReportField.listName}" listKey="id" listValue="name" cssClass="search" headerValue="All" headerKey="All" />
<s:property value="#empReportField.listName" />
// Here it is displaying proper list name
</s:iterator>
I am fetching out these data from my db. Now I am displaying specific list in select box (<s:select list="<ListName>" />) which is stored in column of my table (Database).
Normally it runs like.
<s:select name="emp" list="locationList"
listKey="id" listValue="name"
headerValue="All" headerKey="All" />
It will work well.
But I find simple select box with no list value in it. So what is the actual problem??
In short I want to call list dynamically.
The s:select tag itslef has a list attribute where you can directly give the name of the list (in action class) you want to fill the dropdown with. You do not need an iterator for packing values into s:select dropdown.
Try this:
<s:select label="Select from here"
headerKey="-1" headerValue="Select"
list="listNameHere"
name="feildNameHere" />
Here 'listNameHere' is the list in action layer and the 'feildNameHere' is a instance variable in action class which recieves the value selected by the user.
According to how I understand the question you want to load <s:select list dynamically according to the iterator value.
If so just use the <s:select list like this <s:select list="#empReportField.listName".
Where <s:iterator value="empReportFields" is a list and inside that list another list or map named listName.
I have following code:
<s:iterator value="reviews">
<img src="<s:property value="#request.restaurant.portalImage.url" />" />
<s:property value="user.firstName" />
<s:property value="user.lastName" />
<s:property value="rating" />
<s:property value="review" />
</s:iterator>
reviews is a list of review objects which contain details of a review, such as rating and name of user.
My problem is that I'm not able to access any of the objects present on the ValueStack within the loop.
Outside the loop <s:property value="#request.restaurant.portalImage.url" /> works correctly. But within the loop it prints null.
AFAIK an iterator pushes it's collection on the ValueStack so that all OGNL expressions resolve against it. But I've used # which means I'm explicitly specifying the root object for resolution.
Why is it still not working?
I just spent my whole afternoon fighting against a similar issue.
In my case, the issue was that my iterator variable (In your case reviews) had a field with the same name as the outer variable.
No matter how hard I tried to break out of the local stack of the iterator, it would not access the outer variable.
If your reviews iterator has a field called restaurant its going to override the outer restaurant variable, no matter how hard you try :(
The solution I found was to simply rename the outer variable. (eg: theRestaurant)
Once I renamed it, it was no longer clashing with the inner variable, and I was able to access it from within the iterator.
I am not sure what object is contained in the collection you are using in the Iterator.As per the iterator when you iterate the collection using the S2 iterator tag it will push the object on the top of value stack, which means, say you have collection of user object on which you are iterating like
<s:iterator value="userCollection">
</s:iterator>
so when this will iterate, S2 iterator will push the user object on top of value stack and if we want to refer to the name property of user object, all we need to refer to the name property
<s:property name="userName"/>
since OGNL will try to resolve userName in the value stack and we already have user object as top level object in value stack.I suggest to go through the Iterator tag documentation and how it push values
S2 Iterator tag
You should change it like below:
<s:iterator value="reviews" var="varrequest">
<img src="<s:property value="#varrequest.restaurant.portalImage.url" />" />
<s:property value="#varrequest.user.firstName" />
<s:property value="#varrequest.user.lastName" />
<s:property value="#varrequest.rating" />
<s:property value="#varrequest.review" />
</s:iterator>
Using a iterator reference variable to fetch the unique index object.
I think it resolves your issue
s:iterator value="reviews" status="position">
<img src="<s:property value="#request.restaurant.portalImage.url"/>"/>
<s:property value="#position.user.firstName"/>
<s:property value="#position.user.lastName"/>
<s:property value="#position.rating"/>
<s:property value="#position.review"/>
</s:iterator>
your requirement is little confusing
I have a list of Strings that are attribute names and a Map.
I'm trying to access a model(ex.project) in the map using attribute name in string list.
Here is what I have now.
<s:iterator value="theMap" var="element">
<tr>
<s:iterator value="attributeList" var="attrName">
<td><p><s:property value="#element.Project.#attrName" /></p></td>
</s:iterator>
</tr>
</s:iterator>
If I hard code the attribute name it works fine:
<td><p><s:property value="#element.Project.projectName" /></p></td>
Any advice is appreciated.
Using OGNL <s:property value="#element.Project[#attrName]" />