tags:

views:

3186

answers:

4

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.

+1  A: 

You could update the submit() method to get the correct values and pass them to the action.

Nate
This is the technique I've used in the past. Have some hidden fields that have no value, and write to them in your onclick handler, before calling submit().
Brian Yarger
Dear Nate and Brain, rather than calling submit() method directly onclick event of checkbox I tried this by calling a function say abc() of javascript and see below my abc() function - <script language="javascript" type=""> function abc() { document.frm1.action = "myAction"; document.frm1.method = "post"; document.frm1.submit(); }</script>friends but no gain. Same problem, I mean if hiddens are written in iterator tag then returning values of all rows separated by commas and if hiddens are written outside iterator tag then returning null. Help.
vivmal
+1  A: 

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>
Brian Yarger
Brain, thanks for your reply. I tried your solution, its getting the selected value in the function submitform(requestId, requestChecker, requestProof) but sending NULL in my action class. I made a change like I wrote the hidden fileds in <iterator> tag and this time it is sending the valus of all properties who's object is iterating in <iterator> tag. Brain, you know just 2 months back I had done this. It was running smooth without any javascript. But by mistake code has lost. So, I sure this feature is available in struts2 and its very simple but currently not coming in mind.Try n thx a lot.
vivmal
A: 

Try this

ID PROOF DELETE
A: 

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.

Dusty Pearce