简体   繁体   中英

How to pass id to delete one record from table

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>

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

您可以在表中添加一个隐藏字段,例如“。”因此,每次单击以删除记录时,请从该隐藏字段中检索ID,然后传递回控制器并执行删除功能。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM