Increment a variable in struts2 - struts2

Is it possible to increment a variable in struts 2?
I have a group of check boxes, each checkbox comes under different group. And I name the check boxes based on the group id and its own id from the database
//namesHead : list contains all the group Ids and names
//subHead : list contains all the subgroups id,Name and the reference of main group
<s:iterator status="status" value="namesHead">
<input type="checkbox" onclick="selectSimilarSubGroup('<s:property value="%{id}" />')" />Select All
<s:set name="itrVar" value="1"></s:set>
<s:iterator status="status1" value="subHead" >
<s:set name="var1" value="%{refer_id}"></s:set>
<s:set name="var2" value="%{id}"></s:set>
<s:if test="%{#var1==#var2}">
<s:set name="itrVar" value="%{#status1.count}"></s:set>
<input type="checkbox" multiple id="chk_grp<s:property value="%{id}"/>_<s:property value="%{#status1.count}" />" name="chk_grp" value="<s:property value="%{id_grp}"/>" />
<s:property value="%{name_grp}"/>
</s:if>
</s:iterator>
<s:hidden name="grp_count_%{id}" value="%{#itrVar}" />
</s:iterator>
I think its because the iterator is skipping the initial counts in the second loop. How can i modify the code to get that expected output. Or is there any way to increment a variable inside the page itself?

Mmm... sub-iterator status should reset on every iteration of the parent iterator, with both "two separate lists" or "a list inside another list"...
Something is messing up the counter. Try removing the <s:set line inside the <s:if, just for debugging, and ignoring the <s:hidden for now:
<s:iterator status="status" value="namesHead">
<input type="checkbox" onclick="selectSimilarSubGroup('<s:property value="%{id}" />')" />Select All
<s:set name="itrVar" value="1"></s:set>
<s:iterator status="status1" value="subHead" >
<s:set name="var1" value="%{refer_id}"></s:set>
<s:set name="var2" value="%{id}"></s:set>
<s:if test="%{#var1==#var2}">
<!-- Nothing here -->
<input type="checkbox" multiple id="chk_grp<s:property value="%{id}"/>_<s:property value="%{#status1.count}" />" name="chk_grp" value="<s:property value="%{id_grp}"/>" />
<s:property value="%{name_grp}"/>
</s:if>
</s:iterator>
<!-- Nothing here -->
</s:iterator>
What do you see ?

Related

struts2 TreeMap: keys with spaces not appearing in action

JSP Code:
<s:iterator value="#currentRequisitionGroup.plFldWrap.allPFields" var="pMap" status="hStatus">
<s:iterator value="#pMap.value.paramMdlList" var="paramModel" status="fStat">
<li>
<label>
<s:property value="#paramModel.parameterName" />
</label>
<s:set var="cEdit" value="%{#paramModel.isEditable}"> </s:set>
<s:if test="%{#cEdit == true}">
<s:textfield id="paramId_%{#paramModel.parameterId}" name="rqPGPrmMdl.rqGrp[% {#cGStat.index}].plFldWrap.allPFields['%{(#pMap.key)}'].paramMdlList[%{#fStat.index}].prmValue"/>
</s:if>
<s:else>
<s:textfield id="paramId_%{#paramModel.parameterId}" name="rqPGPrmMdl.rqGrp[% {#cGStat.index}].plFldWrap.allPFields['%{(#pMap.key)}'].paramMdlList[%{#fStat.index}].prmValue" readonly="true"/>
</s:else>
</li>
<s:hidden id="prmId_%{#paramModel.parameterId}" name="rqPGPrmMdl.rqGrp[%{#cGStat.index}].plFldWrap.allPFields['%{(#pMap.key)}'].paramMdlList[%{#fStat.index}].parameterId"></s:hidden>
<s:hidden id="paramName_%{#paramModel.parameterId}" name="rqPGPrmMdl.rqGrp[%{#cGStat.index}].plFldWrap.allPFields['%{(#pMap.key)}'].paramMdlList[%{#fStat.index}].parameterName"></s:hidden>
<s:hidden id="pGId_%{#paramModel.parameterId}" name="rqPGPrmMdl.rqGrp[%{#cGStat.index}].plFldWrap.allPFields['%{(#pMap.key)}'].paramMdlList[%{#fStat.index}].parameterGroupId"></s:hidden>
<s:hidden id="seqNo_%{#paramModel.parameterId}" name="rqPGPrmMdl.rqGrp[%{#cGStat.index}].plFldWrap.allPFields['%{(#pMap.key)}'].paramMdlList[%{#fStat.index}].sequenceNumber"></s:hidden>
</s:iterator>
</s:iterator>
The problem is that some values do not appear in action.
Investigation till now indicates that, if the corresponding html input has space in the key of allPFields, that value doesn't appear in action.
<input id="prmId_30" type="hidden" value="30"
name="rqPGPrmMdl.rqGrp[1].plFldWrap.allPFields['Emp System'].paramMdlList[0].parameterId">
However, if the corresponding html input has no space in the key of allPFields, that value appears in action.
<input id="prmId_46" type="hidden" value="30" name="rqPGPrmMdl.rqGrp[1].plFldWrap.allPFields['Emp'].paramMdlList[0].parameterId">
Here is what the logs say:
xwork2.interceptor.ParametersInterceptor - Parameter [rqPGPrmMdl.rqGrp[1].plFldWrap.allPFields['Emp System'].paramMdlList[0].parameterId] **didn't match acceptedPattern pattern!**
This is happening because white spaces are not accepted in parameter names. You can change acceptParamNames parameter of the ParametersInterceptor, BUT as the documentation states
acceptParamNames - a comma delimited list of regular expressions to describe a whitelist of accepted parameter names. Don't change the default unless you know what you are doing in terms of security implications
So I suggest you to get rid of white spaces in parameters names.

How to pass an <s:iterator> iterated list back from a jsp to the same action class?

<s:iterator value="newIssues">
<s:property value="is.description"/>
<s:property value="is.component"/>
</s:iterator>
<input type="submit" align="bottom" value="Done" id="mysubmit"/>
Now, NewTickets is my action class.This class provided newIssues arraylist to iterate on it..I want the same list to work with when i press submit..how do i do it?

Date comparision in struts2 using OGNL

enter code hereIn my JSP I have display a hyperlink depending upon the date field if it falls between certain range. I am using struts 2 and ONGL
<s:set name="cDate" value="accDate"/>
<s:if test="%{#cDate gt '4/4/11' && #cDate gt '4/4/12}">
<td class="viewCellLast"><s:url id="editURL" action="editActivities">
<s:param name="id" value="%{id}"></s:param>
</s:url> <s:a href="%{editURL}" onclick="return nanCompany();" >Edit</s:a></td>
</s:if>
its failing, can anybody suggest the me how this can achieved.
thanks
smitha.
I got it resolved; I missed # in front fsDate:
<s:set name="fsDate" value="finStartDate"/>
<s:if test="%{#acDate.before(#fsDate}">
<td class="viewCellLast"><s:url id="editURL" action="editActivities">
<s:param name="id" value="%{id}"></s:param>
</s:url> <s:a href="%{editURL}" >Edit</s:a> </td>
</s:if>

How to use property value as textarea value in struts 2

Instead of just displaying list values using <s:property/>, i need to display it in textarea.The reason why i am doing this is <s:property/> does not have "name" attribute in order to submit the values to the action class. Is there any way to do that.I wrote down piece fo code below to make it clear so that you can understand my requirement though it is wrong.Please help me.
<s:iterator value="map" status="stat">
<s:textarea value="<s:property/>" />
</s:iterator>
As Jagan said, assume that map is java.util.List
<s:iterator value="map" status="stat">
<s:textarea value="%{top}" />
</s:iterator>
or
<s:iterator value="map" status="stat">
<s:textarea>
<s:param name="value">
<s:property/>
</s:param>
</s:textarea>
</s:iterator>
or
<s:iterator value="map" status="stat">
<s:textarea>
<s:param name="value">
${top}
</s:param>
</s:textarea>
</s:iterator>
#Jagan : if it is map how to do this ?
Assume map is java.util.Map
<s:iterator value="map" status="stat">
<s:textarea value="%{key}" />
<!-- or -->
<s:textarea value="%{value}" />
</s:iterator>
IteratorComponent
if (value == null && begin == null && end == null) {
value = "top";
}
top is not a attribute of iterator and not a keyword of OGNL. (I may be wrong)
top : top of Stack / element of the current iteration
e.g.
<s:property value="top" />
or
<s:property value="[0].top" />
Iterator tag examples of Struts2 Cookbook have better explanation

Struts2: How to set corresponding values of selected row in the VO class from JSP report?

In my Struts2 application I am generating a textual report (in jsp) using iterator tag like
<table>
<tr>
<td>ID</td>
<td>PROOF</td>
<td>DELETE</td>
</tr>
<s:iterator value="listOfVOClassObjects">
<tr>
<td><s:property value="requestId" /></td>
<td><s:property value="requestChecker" /></td>
<td><s:property value="requestProof" /></td>
<td><s:checkbox name="deleteStatus" onclick="submit()"/></td>
</tr>
</s:iterator>
</table>
When user click checkbox page submits and control goes to action class and I need at the same time values of the corresponding row that user has checked should set in setter methods written in VO class so that I can get all these values in my action class.
I tried this by writing a hidden field for every value under iterator tag like
<s:hidden name=" requestId" />
<s:hidden name=" requestChecker" />
<s:hidden name=" requestProof" />
but its not returning the values of corresponding row but the values of all rows separated by commas.
I also tried this by writing these hidden fields outside the iterator tag and that is returning null for every filed.
Please help.
You could update the submit() method to get the correct values and pass them to the action.
In your javascript function, you would set the current values then submit. So you'd have something like this:
<form method="post" action="myAction">
<script>
function submitform(requestId, requestChecker, requestProof) {
document.findElementById('requestId').value = requestId;
document.findElementById('requestChecker').value = requestChecker;
document.findElementById('requestProof').value = requestProof;
document.forms[0].submit();
}
</script>
<s:hidden id="requestId" name="requestId" />
<s:hidden id="requestChecker" name="requestChecker" />
<s:hidden id="requestProof" name="requestProof" />
<table>
<tr>
<td>ID</td>
<td>PROOF</td>
<td>DELETE</td>
</tr>
<s:iterator value="listOfVOClassObjects">
<tr>
<td><s:property value="requestId" /></td>
<td><s:property value="requestChecker" /></td>
<td><s:property value="requestProof" /></td>
<td><s:checkbox name="deleteStatus" onclick="submitForm('<s:property value="requestId" />', '<s:property value="requestChecker" />', '<s:property value="requestProof" />');"/></td>
</tr>
</s:iterator>
</table>
</form>
It depends on what you are doing. If you can recreate the List on the server side then all you need to do is POST the index of the list that you want to get a handle on. For example:
<s:iterator value="myList" status="row">
...
<s:checkbox onclick="deleteRow(%{#row.index})"/>
</s:iterator>
<script>
function deleteRow(index){
location.href=delete?listIndex=index
}
</script>
You would have a setter for listIndex to "receive" the value. This is using a GET, if you want a POST you can do that and make the listIndex a hidden field.
BUT, if you can't recreate the list on the server because the user has modified the list somehow in the browser and you need to preserve that, then you need Struts2 to create the list based on the POST'd parameters. You do that with some special syntax.
<s:iterator value="myList" status="row">
<s:hidden name="myList[%{#row.index}].requestId"/>
<s:hidden name="myList[%{#row.index}].requestChecker"/>
<s:hidden name="myList[%{#row.index}].requestProof"/>
</s:iterator>
This creates fields with names like myList[0].requestId ... myList[5].requestProof. If you have a setter for myList in your action and you POST these parameters to the action, Struts2 will build the List and populate the properties of the objects in the List. You can use Generics List myList to tell Struts2 what kinds of "things" are in your List. You can also look at Type Conversion documentation for Struts2 if Generics doesn't work for you.

Resources