Struts2 tag If using IteratorStatus - struts2

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.

Related

second iterator property value to first iterator

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.....

How to retrieve struts2 checkboxlist values

I will create a list with checkboxlist. For those I use following code:
<s:form action="accept" namespace="/manager/course">
<s:checkboxlist list="courseRequests" name="acceptList" listValue="studentNickname" listKey="studentId" theme="checkbox-fix"/>
<s:url action="accept" namespace="/manager/course" var="accList" />
<s:a href="%{accList}"><s:text name="Accept"/> </s:a>
</s:form>
It work fine a create a check box list, that you can see its pic in the following:
and this is html code generated by above code:
<form id="accept" name="accept" action="/ESA/manager/course/accept.action" method="post">
<table class="wwFormTable">
<table class="gradienttable">
<tr>
<th class="row"><p>Row</p></th>
<th style="width: 240px;"><p>Student</p></th>
<th ><p>Accept</p></th>
</tr>
<tr>
<td id="row"><p><label>1</label></p></td>
<td style="width:250px;"><p>
<label for="acceptList-1" class="checkboxLabel">Mansour Barzegar</label>
</p></td>
<td style="text-align:center;"><p>
<input type="checkbox" name="acceptList" value="5" id="acceptList-1" </p></td>
</tr>
<tr>
<td id="row"><p><label>2</label></p></td>
<td style="width:250px;"><p>
<label for="acceptList-2" class="checkboxLabel">Ali Mahmoudi</label>
</p></td>
<td style="text-align:center;"><p>
<input type="checkbox" name="acceptList" value="6" id="acceptList-2" </p></td>
</tr>
<tr>
<td id="row"><p><label>3</label></p></td>
<td style="width:250px;"><p>
<label for="acceptList-3" class="checkboxLabel">Masih Zare</label>
</p></td>
<td style="text-align:center;"><p>
<input type="checkbox" name="acceptList" value="7" id="acceptList-3" </p></td>
</tr>
</table>
Accept
</table>
</form>
In the Action Class I tried to retrive seleced checkbox value by following code:
private int[] acceptList;
public void setAcceptList(int[] acceptList){
this.acceptList=acceptList;
}
and several other code but I all states I get null.
Do I use wrong code?
in your markup, do this:
<input type="checkbox" name="thename" id="checkbox_id1" value="value1" />
<input type="checkbox" name="thename" id="checkbox_id2" value="value2" />
in your action (or object) do this:
// action/object code...
Set<String> cbox = new HashSet();
public void setThename(String[] thenames) {
for (String thename : thenames) {
cbox.add(thename);
}
}
// action/object code...
notice the checkbox name matches the setter name, e.g. element name == someName and method == setSomeName
Same would apply for Set<Integer>, but you use int[] thenames as the argument. You could also use Integer[] thenames for the argument.
to test output:
if (cbox != null) {
for (String s : cbox) {
log.info(s);
}
}
http://struts.apache.org/release/2.2.x/docs/using-checkboxes.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