java实现选中删除功能

分析:
通过form表单传递数据,删除数据根据id编号删除。
前台

 "btn btn-primary" href="javascript:void(0);" id="delSelected">删除选中

{pageContext.request.contextPath}/delSelectedServlet" method="post"> {users}" var="user" varStatus="s">
编号 姓名 性别 年龄 籍贯 QQ 邮箱 操作
{user.id}"> ${s.count} ${user.name} ${user.gender} ${user.age} ${user.address} ${user.qq} ${user.email} {pageContext.request.contextPath}/findUserServlet?id=${user.id}">修改 ; {user.id});">删除
Js

后台

Servlet

 @WebServlet("/delSelectedServlet")
 public class DelSelectedServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    //获取所有id
    String[] ids=request.getParameterValues("uid");

    //调用service删除
    UserService service=new UserServiceImpl();
    service.delSelectedUser(ids);
    
    //跳转查询Servlet
    response.sendRedirect(request.getContextPath()+"/userListServlet");
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    this.doPost(request, response);
}

}

Service

 public void delSelectedUser(String[] ids) {
 
 //遍历数组
for (String id:ids){
     //调用dao删除
    dao.deleteUser(Integer.parseInt(id));
  }

}

Dao

public void deleteUser(int uid) {
  String sql="delete from user where uid = ? ";
  template.update(sql,uid);
}

你可能感兴趣的:(java实现选中删除功能)