首先修改list.jsp
<a href="javascript:deleteOne('<%=basePath%>','${message.id}');">删除</a>
编写js方法deleteOne()
function deleteOne(basePath,id) {目前只要通过window.location.reload()重新请求list数据,同样也能达到目标效果
var url=basePath+"del_One.action";
$.ajax({
type:"post",
url:url,
data:{"id":id},
async:false,
success:function(data){
if(jQuery.isEmptyObject(data)){
return;
}else{
if(data=="del_success"){
alert("删除成功");
}else if(data=="del_false"){
alert("删除失败");
}
window.location.reload();
}
},
error:function(data){
alert("请求失败");
}
});
}
修改MessageDao的deleteOne
public boolean deleteOne(int id){
DBAccess dbAccess = new DBAccess();
SqlSession sqlSession=null;
boolean is_ok=false;
try {
sqlSession=dbAccess.getSqlSession();
//通过sqlSession执行SQL语句
if(sqlSession.delete("Message.deleteOne",id)>0){
is_ok=true;
}
sqlSession.commit();
} catch (Exception e) {
e.printStackTrace();
}finally{
if(sqlSession!=null){
sqlSession.close();
}
}
return is_ok;
}
修改MaintainService
public class MaintainService {
public boolean deleteOne(String id){
boolean is_ok=false;
if(id!=null&&!"".equals(id)){
MessageDao messageDao=new MessageDao();
is_ok=messageDao.deleteOne(Integer.valueOf(id));
}
return is_ok;
}
}
修改deleteOneServlet
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
//设置编码
req.setCharacterEncoding("UTF-8");
PrintWriter writer = resp.getWriter();
String id=req.getParameter("id");
MaintainService maintainService = new MaintainService();
if(maintainService.deleteOne(id)){
writer.write("del_success");
}else{
writer.write("del_false");
}
writer.flush();
writer.close();
}