Results 1 to 9 of 9
  1. #1
    sunithamm is offline Member
    Join Date
    Dec 2009
    Posts
    19
    Rep Power
    0

    Default problem with checkbox in struts2

    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.

    Regards,
    Sunitha

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    Yeah Sure. You create a field in Bean class, which contain a array of string and set in property value for checkbox.
    Skype: petrarsentev
    http://TrackStudio.com

  3. #3
    sunithamm is offline Member
    Join Date
    Dec 2009
    Posts
    19
    Rep Power
    0

    Default

    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

    Regards,
    Sunitha

  4. #4
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

  5. #5
    sunithamm is offline Member
    Join Date
    Dec 2009
    Posts
    19
    Rep Power
    0

    Default

    I tried with that example. The problem is when checkbox value is the data from database table.

  6. #6
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    Can you create a simple example that I can check?
    Skype: petrarsentev
    http://TrackStudio.com

  7. #7
    sunithamm is offline Member
    Join Date
    Dec 2009
    Posts
    19
    Rep Power
    0

    Default

    This is my jsp page.

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@taglib prefix="s" uri="/struts-tags"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Delete Room</title>

    </head>
    <body>
    <h3>Bill History</h3>
    <s:form action="viewbill-form" method="post">
    <table border="1">
    <tr>
    <th>View</th>
    <th>Sl.No</th>
    <th>Customer Name</th>
    <th>Amount</th>
    <th>Payment Date</th>

    </tr>
    <s:iterator value="myList" var="bill">
    <tr>

    <td><s: checkbox name="selectedId" fieldValue="#bill.billId"/> </td>
    <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>

    </table>
    <s:submit value="submit"/>
    </s:form>
    <br>

    </body>
    </html>

    This is my action class
    package com.customer;

    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){
    System.out.println(s);
    }
    return SUCCESS;
    }
    }

    In the action class i am getting #bill.billId as such instead of the database table value.

  8. #8
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default

    Probably this should do the trick, %{billId} , no need to use iteratror.
    Swastik

  9. #9
    sunithamm is offline Member
    Join Date
    Dec 2009
    Posts
    19
    Rep Power
    0

Similar Threads

  1. struts2 tags problem
    By videanuadrian in forum New To Java
    Replies: 0
    Last Post: 12-10-2010, 11:47 AM
  2. Struts- checkBox default set problem
    By apoorv.rastogi in forum Web Frameworks
    Replies: 0
    Last Post: 10-25-2010, 12:51 PM
  3. Replies: 3
    Last Post: 07-27-2010, 10:52 AM
  4. checkBox in Jtable renderining problem
    By pothraj in forum AWT / Swing
    Replies: 1
    Last Post: 12-18-2008, 12:39 PM
  5. Struts2.0 Error messages problem
    By Sayed in forum Advanced Java
    Replies: 0
    Last Post: 12-03-2008, 12:38 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •