简体   繁体   English

如何传递ID从表中删除一条记录

[英]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. 我不确定如何传递要删除的记录的ID。 Could you please check my code and give instructions? 您能否检查我的代码并提供指示? I'm really appreciate for your instructions. 非常感谢您的指导。

JSP: 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 在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"); 您可以简单地使用ServletActionContext.getRequest().getParameter("paramName"); to get the HttpRequest parameter value in the action class. 获取action类中的HttpRequest参数值。 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 但是在这里看看类似的线程如何在Action类Struts 2中访问url参数

您可以在表中添加一个隐藏字段,例如“。”因此,每次单击以删除记录时,请从该隐藏字段中检索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. 我希望这就是你想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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