1

I retrieved all data from database and show the list with table. now I would like to delete a single record when click "Delete" in the table. I'm not sure how to pass the id of the record I want to delete. Could you please check my code and give instructions? I'm really appreciate for your instructions.

JSP:

    <table id="employee" class="table">
                    <thead>
                        <tr>
                            <th><span>ID</span></th>
                            <th><span>Name</span></th>
                            <th><span>DOB</span></th>
                            <th><span>Address</span></th>
                            <th><span>Position</span></th>
                            <th colspan="2"><span></span></th>
                        </tr>
                    </thead>
                    <tbody>
                        <s:iterator value="emplistList" var="emplist">
                        <tr>
                        <td><s:property value="id"></s:property></td>
                        <td><s:property value="name"></s:property></td>
                        <td><s:property value="dateofbirth"></s:property></td>
                        <td><s:property value="address"></s:property></td>
                        <td><s:property value="position"></s:property></td>
                        <td><span><a href="delemp?name=<s:property value='id'/>">Delete</a></span>
                    </td>
                        </tr>
                        </s:iterator>
                    </tbody>
                </table>

Action:

public String delete() throws Exception{
    int i = EmployeeListDao.delete(this);
    if(i>0){
        return SUCCESS;
    }
    return ERROR;
}

Dao:

public static int delete(EmployeeListAction emp) {
    // TODO Auto-generated method stub
    int status = 0;
    Connection conn = null;
    try {
        String url = "jdbc:mysql://localhost:3306/Test";
        Class.forName("com.mysql.jdbc.Driver");
        conn = DriverManager.getConnection(url, "root", "root");
        System.out.println(conn);
        PreparedStatement ps = conn
                .prepareStatement("Delete from employee where emp_id=?");
        ps.setInt(1, emp.getId());
        status = ps.executeUpdate();

    } catch (Exception e) {
        // TODO: handle exception
        System.out.println(e);
    }
    return status;

}

Struts.xml

<action name="delemp" class="master.struts2.action.EmployeeListAction"
        method="delete">
        <result name="input">index.jsp</result>
        <result name="success" type="dispatcher">index.jsp</result>
        <result name="error">error.jsp</result>
    </action>
Roman C
  • 49,761
  • 33
  • 66
  • 176
user3898594
  • 123
  • 2
  • 4
  • 8
  • a suggestion make a checkbox and assign the primary key of table as value of checkbox and when checkbox is checked pass the value to action class by js or jquery using ajax – amit bhardwaj Nov 27 '14 at 05:25

3 Answers3

0

You can simply use ServletActionContext.getRequest().getParameter("paramName"); to get the HttpRequest parameter value in the action class. so in your delete method,

public String delete() throws Exception{
    String id=ServletActionContext.getRequest().getParameter("name");
    int i = EmployeeListDao.delete(id);
    if(i>0){
        return SUCCESS;
    }
    return ERROR;
} 

will do the work for you . But have a look at similar thread here How to access url parameters in Action classes Struts 2

Community
  • 1
  • 1
Santhosh
  • 8,181
  • 4
  • 29
  • 56
0

You can add a hidden field in your table, for example, ". So each time when you click to delete the record, retrieve the id from the hidden field and pass back to controller and execute the delete function.

CE ZHANG
  • 527
  • 4
  • 7
0

do it like this:

<td><s:url action="delemp.action" var="urltag">
        <s:param name="name">
                <s:property value="id" />
        </s:param>
        </s:url> <a href="<s:property value="#urltag" />" >delete</a>
</td>

i hope this is what you want.

Govinda Sakhare
  • 5,009
  • 6
  • 33
  • 74