目录
一、前端页面搭建
二、dao层
三:service层开发
四:servlet层开发
一、前端页面搭建
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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">
<%@ include file="/common/head.jsp" %>
<title>Insert title here</title>
</head>
<body>
<!-- 查询条件 -->
<div style="margin-top: 15px; margin-left:10px;">
<input class="easyui-textbox" id="bookName" style="width:300px">
<a id="bookQry" class="easyui-linkbutton" data-options="iconCls:'icon-search'">查询</a>
</div>
<div id="p" class="easyui-panel" style="padding:10px" data-options="fit:true, border:false">
<table id="bookTable" class="easyui-datagrid" style="width:100%;height:90%;">
</table>
</div>
<!-- 列表上方的工具条 -->
<div id="bookTableToolbar" style="text-align: right;">
<a href="#" id="addBookBtn" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true"/a>
<a href="#" id="editBootBtn" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true"/a>
<a href="#" id="delBootBtn" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true"/a>
</div>
<!-- 给弹出窗口定义一个容器,并默认为隐藏,在点击后再显示 -->
<div id="bookDiglog" style="display:none;"></div>
</body>
</html>
enitBook.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<form id="bookForm">
<div style="margin-top: 10px;margin-left:10px;">
<label for="name">书本名称:</label>
<input class="easyui-textbox" type="text" name="bookname" data-options="required:true" />
</div>
<div style="margin-top: 10px;margin-left:10px;">
<label for="price">书本价格:</label>
<input class="easyui-textbox" type="text" name="price" data-options="required:true" />
</div>
<div style="margin-top: 10px;margin-left:10px;">
<label for="booktype">书本类型:</label>
<input class="easyui-textbox" type="text" name="booktype" data-options="required:true" />
</div>
</form>
二、dao层
在原有的基础上编写
@Override
/**
* 增加图书
*/
//图书id是拿到表格最大序号加1
public void addBook(Book book) {
String sql = "insert into t_book(id,bookname,price,booktype)\r\n" +
"select max(id)+1,'"
+ ""+book.getBookname()+"',"
+ ""+book.getPrice()+",'"+
book.getBooktype()+"' from t_book";
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
con = DBHelper.getCon();
ps = con.prepareStatement(sql);
ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.myClose(con, ps,rs);
}
}
三:service层开发
IBookService
/**
* 增加书本
* @param book 书本对象
*/
void addBook(Book book);
BookService
@Override
public void addBook(Book book) {
// TODO Auto-generated method stub
bd.addBook(book);
}
四:servlet层开发
BookAddServlet
package com.zking.euidemo.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
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.alibaba.fastjson.JSON;
import com.zking.euidemo.entity.Book;
import com.zking.euidemo.service.BookService;
import com.zking.euidemo.service.IBookService;
@WebServlet("/bookAddServlet")
public class BookAddServlet extends HttpServlet{
private IBookService service = new BookService();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置字符编码方式
req.setCharacterEncoding("utf-8");
resp.setContentType("application/json; charset=utf-8");
Map<String,Object> rv = new HashMap<>();
try {
//获取表单提交过来的值
String bookname = req.getParameter("bookname");
String price = req.getParameter("price");
String booktype = req.getParameter("booktype");
Book book = new Book();
book.setBookname(bookname);
book.setPrice(new BigDecimal(price));
book.setBooktype(booktype);
service.addBook(book);
rv.put("success", true);
} catch (Exception e) {
e.printStackTrace();
rv.put("success", false);
}
PrintWriter out = resp.getWriter();
String json =JSON.toJSONString(rv);
out.write(json);
out.flush();
out.close();
}
}