删除的servlet:
package com.zking.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zking.biz.BookBiz;
import com.zking.biz.IBookBiz;
import com.zking.entity.Book;
@WebServlet("/deleteBook.do")
public class DeleteBookServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//三个编码方式
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=UTF-8");
//拿out对象
PrintWriter out = response.getWriter();
//接收前台传递过来的参数 bid
String bid=request.getParameter("bid");//书籍名称
//servlet调用biz
IBookBiz ibb=new BookBiz();
//调用删除方法
int n=ibb.deleteBook(Integer.parseInt(bid));
String str="error";
if(n>0) {//说明删除成功
str="success";
}
//输送到页面
out.write(str);
out.flush();
out.close();
}
}
删除的点击事件
//给工具栏的-号添加点击事件
$("#removeBtn").click(function(){
//获取用户选中的行
var row=$('#myTable').datagrid('getSelected');
if(row!=null){//说明选中了
//alert(row.bid+" "+row.bname);
$.messager.confirm('确认','您确认想要删除记录吗?',function(r){
if(r){
//向后台发送ajax请求 把id传递过去
//url data success
$.post(ctx+"/deleteBook.do",{bid:row.bid},function(data){
//alert(data);
if(data=="success"){
//成功
$.messager.alert('消息','删除成功');
//关闭对话框
$('#myDialog').dialog("close");
//调用刷新方法
myShow();
}
else{
//失败
$.messager.alert('警告','删除失败');
}
})
}
});
}else{//没选中
$.messager.alert('警告','请先选中你要 *** 作的行');
}
})
二、修改
修改的servlet
package com.zking.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zking.biz.BookBiz;
import com.zking.biz.IBookBiz;
import com.zking.entity.Book;
@WebServlet("/UpdateBookServlet")
public class UpdateBookServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 三个编码方式
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=UTF-8");
// 拿out对象
PrintWriter out = response.getWriter();
// 接收前台传递过来的参数 bid bname bprice btype
String bid = request.getParameter("bid");// 书籍名称
String bname=request.getParameter("bname");//书籍名称
String a=request.getParameter("bprice");//书籍价格
String btype=request.getParameter("btype");//书籍类型
double bprice=0;
if(a!=null) {//将String-->double
bprice=Double.parseDouble(a);
}
// servlet调用biz
IBookBiz ibb = new BookBiz();
// 调用删除方法
Book b=new Book(bname, bprice, btype);
int n = ibb.updateBook(Integer.parseInt(bid), b);
String str = "error";
if (n > 0) {// 说明修改成功
str = "success";
}
// 输送到页面
out.write(str);
out.flush();
out.close();
}
}
修改的点击事件:
//给工具栏的编辑选项添加点击事件
$("#editBtn").click(function(){
//获取用户选中的行
var row=$('#myTable').datagrid('getSelected');
if(row!=null){//说明选中了
openDialog(row);//打开对话框
}
else{//没选中
$.messager.alert('警告','请先选中你要 *** 作的行');
}
})
//打开对话框
function openDialog(row){
var title="增加书籍";
var url="/addBook.do";
if(row!=undefined){//说明是修改
title="修改书籍";
url="/UpdateBookServlet";
}
//显示一个对话框
$('#myDialog').dialog({
title: title, //标题
width: 400, //宽度
height: 300,//高度
closed: false,//是否能关闭
cache: false, //是否有缓存
href: 'editBook.jsp',//加载的页面
modal: true ,//是否出现模态框效果
onLoad:function(){
if(row!=undefined){//说明是修改
$('#myForm').form('reset');//有选中编辑效果
$('#myForm').form('load',row);//读取记录填充到表单中
}
},
buttons:[{//按钮们
text:'提交',
handler:function(){
//向后台发送ajax请求
$.ajax({
url:ctx+url,//请求地址 servlet
data:$("#myForm").serialize(),//请求参数
type:"post",//请求方式 默认为get
dataType:"text",//预期可能返回的数据类型
success:function(data){//成功的回调函数
if(data=="success"){
//成功
$.messager.alert('消息','提交成功');
//关闭对话框
$("#myDialog").dialog("close");
//调用刷新方法
myShow();
}
else{
//失败
$.messager.alert('警告','提交失败');
}
},
error:function(){//失败的回调函数
alert("有误");
}
});
}
},{
text:'关闭',
handler:function(){
//关闭对话框
$('#myDialog').dialog("close");
}
}]
});
}
界面展示:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)