second iterator property value to first iterator - struts2

here the first iterators<s:property value="second iterators <s:property value="heading">
how to assign
here is the sample code:
<s:iterator status="one" value="firstList">
<tr>
<s:iterator var="second" status="heads" value="secondList">
<td>
<s:property value="<s:property value="heading"/>"/>
</td>
</s:iterator>
</tr>
</s:iterator>
Is it possible to give the property value dynamically...
here the value is a variable which is increments dynamically.....

Related

What is the strut 2 equivalent of strut 1 logic:iterate tag property attribute

In strut 1 we use logic:iterate tag to iterate over the list. While defining this tag we use property attribute and strut 1 call getter method of this attribute to get Iterable Object. What is the equivalent in strut 2 for same thing ?
As show in below code in strut 1 we are using logic:iterate tag to iterate over iterable object. And Strut 1 get this itearble object by calling getter method of property attribute(records in this example) which is defined by name attribute(DataForm in this example).
i.e. in below example, strut 1 internally calls getRecords() method which is present in DataForm class to get the iterable object.
How can we achive same thing in strut 2 ?
<div STYLE=" height: 300px; width: 860;font-size:12px; overflow: auto;">
<html:form action="/discardHorisBulk.do" target="content">
<table width="840">
**<logic:iterate id="horisList" name="DataForm" property="records" indexId="indexId" type="com.waghtech.client.isTech.model.Horis" >**
<tr>
<%
String bgColor="#fffafa";
int size = indexId.intValue();
if (indexId.intValue() %2 == 0)
{
bgColor="#dcdcdc";
}
java.util.HashMap params = new java.util.HashMap();
params.put("key", horisList.getKey() );
params.put("clientName", horisList.getClientName() );
pageContext.setAttribute("paramsName", params);
%>
<td bgcolor=<%=bgColor%> width="200">
<html:link page="/mapClient.do" name="paramsName" scope="page" >
<bean:write name="horisList" property="clientName"/>
</html:link>
</td>
<td bgcolor=<%=bgColor%> width="60">
<bean:write name="horisList" property="startDate"/>
</td>
<td bgcolor=<%=bgColor%> width="60">
<bean:write name="horisList" property="endDate"/>
</td>
<!--
<td bgcolor=<%=bgColor%> width="200">
<bean:write name="horisList" property="displayFd"/>
</td>
-->
<td bgcolor=<%=bgColor%> width="200">
<bean:write name="horisList" property="userfileName"/>
</td>
<td bgcolor=<%=bgColor%> width="40">
<html:multibox name="dataForm" property="markedRecords" value="<%=horisList.getClientName()%>">
<bean:write name="horisList" property="key"/>
</html:multibox>
</td>
</tr>
</logic:iterate>
</table>
</div>
You can use value attribute for it. something like this :
<ol>
<s:iterator value="records">
<li><s:property value="userfileName"/></li>
</s:iterator>
</ol>
This will call the getter method of this list and within those when you use value attribute it will call getter for those fields in records class.

Struts2 tag If using IteratorStatus

I am starting with Struts2 and I need help to solve an issue on my IF tag.
This my code
public class InventoryRow {
private String title;
private int[] qty = new int[5];
private boolean[] warningFlag = new boolean[5];
}
In Action class I have this property:
private List<InventoryRow> parts = new ArrayList<InventoryRow>();
In my JSP, I would like to apply different style according with qty[] value or warningFlag[] value.
<s:iterator value="parts" var="product">
<tr>
<td><s:property value="title" /></td>
<s:iterator value="qty" var="val" status="idStatus">
<td class="qty <s:if test="#val==-99"> none</s:if>
<s:elseif test="#warningdFlag[%{#idStatus.index}] == true"> warning</s:elseif>
" >
<s:property />
</td>
</s:iterator>
</tr>
</s:iterator>
The first test (equal -99) works. But not the second one (using warningFlag)
Thanks in advance for your help and your advertisement.
Mickael
The # before warningFlag is not needed and you have typo warningdFlag should be warningFlag.
<s:iterator value="parts">
<tr>
<td><s:property value="title" /></td>
<s:iterator value="qty" var="val" status="idStatus">
<td class="qty <s:if test="#val==-99"> none</s:if>
<s:elseif test="warningFlag[#idStatus.index]"> warning</s:elseif>" >
<s:property />
</td>
</s:iterator>
</tr>
</s:iterator>
The %{...} notation on getting status index inside test attribute is not needed because test takes expression as a value.
Also see this link: http://struts.apache.org/development/2.x/docs/ognl.html.

Calculate the sum of a particular table cell's value created by s:iterator tag in JSP from Action class of Struts2?

I want to calculate the sum of the particular property from action class which is displaying in JSP as HTML Table using <s:iterator> tag.
JSP code is:
<table border="1">
<tr>
<th>Date</th>
<th>Material</th>
<th>Quantity</th>
<th>Buyer</th>
<th>Total</th>
<th>Remarks</th>
<th colspan="2">Action</th>
</tr>
<s:set var="sumTotal" value="%{0}" />
<s:iterator value="eal" status="entries">
<tr>
<td><s:property value="date"/></td>
<td><s:property value="materialName"/></td>
<td><s:property value="quantity"/><s:property value="unitName"/></td>
<td><s:property value="buyer"/></td>
<td>₹<s:property value="total"/></td>
<s:set var="sumTotal" value="%{+#attr.total}" />
<td><s:property value="remarks"/></td>
<td>
Edit
</td>
<td>
Delete
</td>
</tr>
</s:iterator>
<tr>
<th colspan="4">Sum Total</th>
<td colspan="4"><s:property value="#sumTotal"/></td>
</tr>
</table>
I want to calculate the sum of table heading "Total" and display that below in last row with the heading "Sum Total".
Please help me..
Change
<s:set var="sumTotal" value="%{+#attr.total}" />
to
<s:set var="sumTotal" value="#sumTotal + total" />
And you do not need to use %{0} inside first <s:set> tag, simple 0 will work.
<s:set var="sumTotal" value="0" />

How do I iterate and index a List of Lists in OGNL/Struts2?

I have a property with a profile like this:
public List<List<String>> getAvailablePassengersJS()
I wish to create a table with one row for each element in the outer List, and a column for each of the first five positions in the inner List.
I tried using this:
<s:iterator value="ssn.docked.AvailablePassengersJS" var="line">
<tr>
<td><s:property value="line[0]"/></td>
<td><s:property value="line[1]"/></td>
<td><s:property value="line[2]"/></td>
<td><s:property value="line[3]"/></td>
<td><s:property value="line[4]"/></td>
</tr>
</s:iterator>
However, the output is 100+ rows of entirely blank columns:
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
I know the data is correctly populated. When I do this:
<s:iterator value="ssn.docked.AvailablePassengersJS" var="line">
<tr>
<td><s:property/></td>
</tr>
</s:iterator>
I get 100+ rows like this:
<tr>
<td>[2236, Middle, Onbenbosin Bawed, true, You can book Tritho Fonand]</td>
</tr>
And when I do this:
<s:iterator value="ssn.docked.AvailablePassengersJS" var="line">
<tr>
<td><s:property value="line"/></td>
</tr>
</s:iterator>
I get an exception:
java.lang.ClassCastException: java.util.ArrayList incompatible with java.lang.String
So I know "line" resolves to an ArrayList. Everything I've read about OGNL seems to imply that I should be able to index an array via the [] notation. I've tried more combinations of {}, #, etc, that I won't burden you with to no avail.
Can anyone explain why this doesn't work and what I need to do to make it work?
UPDATE
Trying Quaternion's suggestion below I used this code:
<s:iterator value="ssn.docked.AvailablePassengersJS" var="line">
<tr id="availPass<s:property value="#line[0]"/>">
<td><s:property value="#line[1]"/></td>
<td><s:property value="#line[2]"/></td>
<td><s:checkbox key="#line[3]" value="false" theme="simple"/> Book</td>
</tr>
</s:iterator>
It mostly works. Unfortunately it fails for the checkbox:
<tr id="availPass151570">
<td>Low</td>
<td>Andadicko Ostan</td>
<td><input type="checkbox" name="#line[3]" value="true" id="#line_3_"/><input type="hidden" id="__checkbox_#line_3_" name="__checkbox_#line[3]" value="true" /> Book</td>
</tr>
If I don't use key, no combination of id or value works either.
If I just both name and id:
<td><s:checkbox name="#line[3]" id="#line[3]" value="false" theme="simple"/> Book</td>
if doesn't resolve it:
<td><input type="checkbox" name="Book" value="true" id="#line3"/><input type="hidden" id="__checkbox_#line3" name="__checkbox_Book" value="true" /></td>
If I just use name:
<td><s:checkbox name="#line[3]" value="false" theme="simple"/> Book</td>
it doesn't work either:
<td><input type="checkbox" name="#line[3]" value="true" id="#line_3_"/><input type="hidden" id="__checkbox_#line_3_" name="__checkbox_#line[3]" value="true" /> Book</td>
if I just use id, I get some internal error:
<td><s:checkbox id="#line[3]" value="false" theme="simple"/> Book</td>
<td><input type="checkbox" name="<!-- FREEMARKER ERROR MESSAGE STARTS HERE -->...
Expression parameters.name is undefined on line 23, column 32 in template/simple/checkbox.ftl.
The problematic instruction:
----------
==> ${parameters.name?html} [on line 23, column 30 in template/simple/checkbox.ftl]
Why does the parameter for checkbox require a different syntax than for property?
private List<List<String>> listOfLists;
public List<List<String>> getListOfLists() {
return listOfLists;
}
public void setListOfLists(List<List<String>> listOfLists) {
this.listOfLists = listOfLists;
}
<s:iterator var="list" value="%{listOfLists}">
<s:iterator var="list" value="#list" status="stat">
<s:property value="#list[#stat.index]"/> //by index.
</s:iterator><br/>
</s:iterator>
It's an old question but for others, you need to use %{listname[index]}" for checkbox e.g.
<input type="checkbox" name="%{#line_3_[1]}" value="true" id="%{#line_3_[1]}"/>

Struts 2.0 pass selected checkbox mapping value to JavaScript function

I am using struts 2 framework and iterating 4 fields (checkbox,rollnumber,name,location) in Jsp. It is working fine. Now i need to delete the selected checkbox record, for that i require rollnumber(which is primary key in table) object to be pass to javascript function. How can I pass the rollnumber object to javascript function. I have already pass the checkbox object(document.myForm.subCheckBox) to java script function, i need to pass one more rollnumber object.
<table border="1">
<tr>
<s:if test="%{mode != 'view'}">
<td><input type="checkbox" id="mainCheckBox" onclick="return checkAll(document.myForm.subCheckBox)"/></td>
</s:if>
<th>Roll Number</th>
<td>Name</td>
<td>Location</td>
</tr>
<s:iterator value="beanList" >
<tr>
<s:if test="%{mode != 'view'}">
<td>
<input type="checkbox" name="subCheckBox"/>
</td>
</s:if>
<td>
<s:property value="rollnumber" />
</td>
<td>
<s:property value="name"/>
</td>
<td>
<s:property value="location"/>
</td>
</tr>
</s:iterator>
</table>
<table>
<s:if test="%{mode != 'view'}">
<tr>
<input type="button" value="Delete" onclick="return deleteRecord(document.myForm.subCheckBox) "/>
</tr>
</s:if>
</table>

Resources