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

Java 如何传递id从表中删除一条记录,java,jsp,struts2,action,Java,Jsp,Struts2,Action,我从数据库中检索了所有数据,并用表显示了列表。现在我想在表中单击delete时删除一条记录。我不确定如何传递要删除的记录的id。你能检查一下我的密码并给出说明吗?非常感谢您的指导 JSP: 道: Struts.xml <action name="delemp" class="master.struts2.action.EmployeeListAction" method="delete"> <result name="input">inde

我从数据库中检索了所有数据,并用表显示了列表。现在我想在表中单击delete时删除一条记录。我不确定如何传递要删除的记录的id。你能检查一下我的密码并给出说明吗?非常感谢您的指导

JSP:

道:

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>
您只需使用ServletActionContext.getRequest.getParameterparamName;获取action类中的HttpRequest参数值。所以在你的删除方法中

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

我会为你做这项工作。但是看看这里的类似线程

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

如下操作:

<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>

我希望这就是您想要的。

建议选中一个复选框,并将表的主键指定为checkbox的值,当选中checkbox时,使用ajax将值通过js或jquery传递给action类
<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>
public String delete() throws Exception{
    String id=ServletActionContext.getRequest().getParameter("name");
    int i = EmployeeListDao.delete(id);
    if(i>0){
        return SUCCESS;
    }
    return ERROR;
} 
<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>