The moose likes Struts and the fly likes problem with struts2 checkbox Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "problem with struts2 checkbox " Watch "problem with struts2 checkbox " New topic
Author

problem with struts2 checkbox

reshmi aneesh
Greenhorn

Joined: Feb 01, 2011
Posts: 15
Hi All,

I am having a jsp page which displays values in the database table using <s:iterator> tag as follows:

<s:iterator value="myList" var="bill">
<tr>
<td><s: property value="#bill.billId" /></td>
<td><s: property value="#bill.customerName" /></td>
<td><s: property value="#bill.amount" /></td>
<td><s: property value="#bill.paymentDate" /></td>
</tr>
</s:iterator>

I want to add a checkbox to each row. In the action class i want to get the id of the row whose checkbox has been clicked.

Is there any way to do this?

Thanks in advance.

Raneesh
Raza Mohd
Ranch Hand

Joined: Jan 20, 2010
Posts: 247

Hi raneesh,

Of course you can do it by using struts checkbox tag.
you can render the id through Value Parameter in Tag.


Best Regards
Mohd Raza


Good luck!!
A small leak can sink a Gigantic ship.>
reshmi aneesh
Greenhorn

Joined: Feb 01, 2011
Posts: 15
Thank you for your reply.

i added the following code inside <tr></tr> tag

<td><s:checkbox name="selectedId" value="#bill.billId"/></td>

In the action class, i created a string array with the name selectedId with its getter and setter methods.
i tried to iterate through that array. but i am getting NullPointerException
Raza Mohd
Ranch Hand

Joined: Jan 20, 2010
Posts: 247

Can you please put your action code...and where are you getting null pointer ..please specify.
reshmi aneesh
Greenhorn

Joined: Feb 01, 2011
Posts: 15
Thanks for your reply.

This is my Action class

import com.opensymphony.xwork2.ActionSupport;

public class ViewBill extends ActionSupport{
private String[] selectedId;

public void setSelectedId(String[] selectedId) {
this.selectedId = selectedId;
}

public String[] getSelectedId() {
return selectedId;
}
public String execute(){
for(String s: selectedId){//getting NullPointerException here
System.out.println(s);
}
return SUCCESS;
}
}

Raza Mohd
Ranch Hand

Joined: Jan 20, 2010
Posts: 247

I suspect that you are not having interceptors stack in your struts.xml
reshmi aneesh
Greenhorn

Joined: Feb 01, 2011
Posts: 15
thanks for your reply.

i'm not using any interceptor stacks right now.Kindly advice me on how to do that.

Raza Mohd
Ranch Hand

Joined: Jan 20, 2010
Posts: 247

One things you need to confirm .

Whether your form is actually submitted or not?
might be you are navigating a URL

Interceptors by default works whether you explicitly override or not . So interceptor is not a matter to panic.

Best Regards
Mohd Raza
reshmi aneesh
Greenhorn

Joined: Feb 01, 2011
Posts: 15
I'm navigating a url.
Raza Mohd
Ranch Hand

Joined: Jan 20, 2010
Posts: 247

ok if you are navigating a URL ..so do you think you are posting a Form .

the answer is No , so how can you be able to get the form parameters on action .

try using Submit.

Best regards
Mohd Raza
reshmi aneesh
Greenhorn

Joined: Feb 01, 2011
Posts: 15
Thanks for the reply.

I tried submitting the form. In the action class i'm getting #bill.billId as such.I want to get the value from the database in the action class.

Raza Mohd
Ranch Hand

Joined: Jan 20, 2010
Posts: 247

try using fieldValue in place of value..

or i will suggest not to use var ..





reshmi aneesh
Greenhorn

Joined: Feb 01, 2011
Posts: 15
Without using 'var',how can we get values in the list of iterator.
I tried using fieldValue also but not getting.
Raza Mohd
Ranch Hand

Joined: Jan 20, 2010
Posts: 247

no need to use var with itterator

use
reshmi aneesh
Greenhorn

Joined: Feb 01, 2011
Posts: 15
GOT IT!!!

Thank you so much.

Regards
Raneesh.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem with struts2 checkbox