How to get the sum of values inside Struts 2 iterator? - java

My application is shopping website application. In place order case, I need to pass the sum of every item's price as a one parameter. Prices are coming to the iterator correctly. But I failed to get the sum of those prices. shopping cart list is coming from Another action class.
I tried with this:
<s:iterator value="shoppingCarts" var="shoppingCarts">
<s:label name="total" value="%{#total+(item.salesPrice*qty)+item.shippingCost}" label="Total"/>
</s:iterator>
This shows two labels, if shopping cart list has two items. How can I get a total to a one parameter inside the iterator?

Define a context variable total like in this exampe
<s:set var="total" value="0">
<s:iterator value="shoppingCarts">
s:set var="total" value="%{#total+item.salesPrice*qty+item.shippingCost}"/>
</s:iterator>
<s:label name="total" value="%{#total}" label="Total"/>

Related

How to retrieve dynamically generated name tag for s:set for Struts2?

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>

JSP table from ArrayList creation

I need to show some data inside a table (jsp). The data are being passed like this:
request.setAttribute("name", nameVariable);
request.setAttribute("surname", surnameVariable);
request.setAttribute("list", list); //Here are stored ultimately all the data (so name and surname also)
My list is being updated and I need to have the list being updated also. I know my list gets more items, but this code prints only last record from that list. What should I change in my code to be able to print all records from list in table?
My jsp:
<c:forEach items="${list}">
<tr>
<td>${name}</td>
<td>${surname}</td>
</tr>
</c:forEach>
You're always printing the same request attributes, at each iteration of the loop, completely ignoring the current element of the list. Assuming the list contains objects of type Person (for example), which has a getName() and a getSurname() method, the code should be
<c:forEach items="${list}" var="person">
<tr>
<td>${person.name}</td>
<td>${person.surname}</td>
</tr>
</c:forEach>
Just like, in Java, a foreach loop would define a person variable for the current person during the itertion:
for (Person person: list) {
System.out.println(person.getName());
System.out.println(person.getSurname());
}

Access list of list in JSP file

I have created a list of lists that contains content that I want to display through a jsp file. when trying to just display the items through one list, the file works and I see it. But when I split the items in different arraylists and try to iterate over that, nothing shows up. My initialization is,
private final List<ArrayList<DisplayableProduct>> listOfThreeProducts = new ArrayList<ArrayList<DisplayableProduct>>();
I have verified that there is content inside each list of it through debugging.
my model.listofThreePorducts is a list of lists. so I want to loop through the list of lists and then loop inside each loop and so stuff. Is it correct to pass the var="listoflists" value to the second for loop as such below? would it be items="${listoflists}" to access everything in that list ?
<c:forEach items="${model.listOfThreeProducts}" var="listoflists">
<div id="hero-featureSwap">
<c:forEach items="${listoflists}" var="product">
<div class="widget-element-brand"
title='<awsmp:formatText text="${product.vendorName}" />'>
<awsmp:formatText text="${product.vendorName}" maxLength="25" />
</div>
</c:forEach>
</div>
</c:forEach>
You need standard getter in model object for property listOfThreeProducts
further detail discussed in question's comment section

Struts2 <s:select list=""> not working dynamically

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.

How to access ValueStack objects within Struts iterator?

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

Categories