今天我们进行数据的一个增加(书籍增加)
首先我们搭建后台代码一个自动增加书籍编号的增加方法
/**
* 增加方法
*/
@Override
public int addBook(Book b) {
int n=0;
try {
con=DBHelper.getCon();
String sql="insert into tb_book(bid,bname,bprice,btype) select nvl(max(bid),0)+1,?,?,? from tb_book ";
ps=con.prepareStatement(sql);
ps.setString(1, b.getBnaem());
ps.setDouble(2, b.getBprice());
ps.setString(3, b.getBtype());
n=ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.myClose(con, ps, rs);
}
return n;
}
后台方法写完后我们就可以去前台搭建前台
首先我们先得给增加图标添加点击数事件,然后要弹出一个带有文本框的模态框
假设图片id为addBtn
$("#addBtn").click(function(){
//弹出一个模态框
})
模态框代码(复制进去即可)
《放body里面》
<div id="dd"></div>
《放点击事件里面》
$('#dd').dialog({
title: 'My Dialog',
width: 400,
height: 200,
closed: false,
cache: false,
href: 'get_content.php',
modal: true
});
这样一个简单的弹框就完成了,然后我们就要跳转页面来进行加载页面邦值
这里页面我们命名为addBook.jsp,那么上面的href里面的内容就要改成相对应的路径
然后我们去该界面进行改变值内容
addBook.jsp界面代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="../../common/head.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>增加页面</title>
</head>
<body>
<form id="myFrom" method="post" style="text-align: center;margin: 20px">
<div style="margin-top: 10px">
<label for="name" >书籍名:</label>
<input class="easyui-textbox" type="text" name="bname" data-options="required:true" style="width: 200px"/>
</div>
<div style="margin-top: 10px">
<label for="email">书籍价格:</label>
<input class="easyui-textbox" type="text" name="bprice" data-options="required:true" style="width: 200px"/>
</div>
<div style="margin-top: 10px">
<select id="cc" class="easyui-combobox" name="btype" style="width:200px;">
<option value="励志">励志</option>
<option value="悬疑">悬疑</option>
<option value="惊悚">惊悚</option>
<option value="搞笑">搞笑</option>
<option value="冒险">冒险</option>
</select>
</div>
</form>
</body>
</html>
上面我们的name名和表单id都是命名要跟前台对应的,现在只需要前台给后台发出响应,后台接收传值即可,所以我们需要在前台写一个ajax
buttons:[{
text:'提交',
handler:function(){
//向后台发送ajax请求
$.ajax({
url:ctx+"/addBook.do",//请求路径
data:$("#myFrom").serialize(),//请求参数
type:"post",//请求方法
dataType:"text",//预期可能返回的数据类型
success:function(data){//成功的回掉函数
if(data=="Yes"){
//增加成功
$.messager.alert('消息','增加成功');
//关闭对话框
$('#myDialog').dialog("close");
//刷新数据
myShow();
}
else{
//增加失败
$.messager.alert('警告','增加失败');
//关闭对话框
$('#myDialog').dialog("close");
}
},
error:function(){//失败的回掉函数
$.messsager.alert('警告','有误');
}
});
}
},{
text:'关闭',
handler:function(){
//关闭对话框
$('#myDialog').dialog("close");
}
}]
现在只需要写入我们的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("/addBook.do")
public class AddBookServlet 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();
//获取前台传来的参数
String bnaem=request.getParameter("bname");//书籍名称
String a=request.getParameter("bprice");//书籍价格
String btype=request.getParameter("btype");//书籍类型
double bprice=0;
if(a!=null) {
bprice=Double.parseDouble(a);
}
//调用biz层
IBookBiz ibb=new BookBiz();
int n = ibb.addBook(new Book(bnaem, bprice, btype));
String ss="no";
if(n>0) {
ss="Yes";
}
out.write(ss);
out.flush();
out.close();
}
}