[英]How to pass id to delete one record from table
我從數據庫中檢索了所有數據,並顯示了帶有表的列表。 現在,我想在單擊表中的“刪除”時刪除一條記錄。 我不確定如何傳遞要刪除的記錄的ID。 您能否檢查我的代碼並提供指示? 非常感謝您的指導。
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>
行動:
public String delete() throws Exception{
int i = EmployeeListDao.delete(this);
if(i>0){
return SUCCESS;
}
return ERROR;
}
道:
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>
您可以簡單地使用ServletActionContext.getRequest().getParameter("paramName");
獲取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;
}
會為你工作。 但是在這里看看類似的線程如何在Action類Struts 2中訪問url參數
您可以在表中添加一個隱藏字段,例如“。”因此,每次單擊以刪除記錄時,請從該隱藏字段中檢索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>
我希望這就是你想要的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.