一、系统介绍
软件环境
Operating System:Windows10
IDEA:2018.2
Java:jdk1.8
Mysql:8.0.13
Tomcat:8.5.23
本次更新添加了管理员的增删改查,图书管理员可以进行图书的新增,修改,删除,查询。然后登录部分添加了区分是普通用户登录还是管理登录的功能。
下面是新增页面之后的工程的截图
二、系统展示
1.管理员主页
2.管理员查询图书页面
3.管理员图书明细页面
三、代码实现
1.前端
admin.jsp
<%-- Created by IntelliJ IDEA. User: shuijianshiqing Date: 2020/5/24 Time: 10:50 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %> <%@ page import="java.util.List" %> <%@ page import="com.sjsq.po.Book" %> <%@ page import="com.sjsq.service.impl.BookServiceImpl" %> <%@ page import="com.sjsq.po.User" %> <!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> <style type="text/css"> h1{ text-align: center; } body{ background-color: antiquewhite; } #before{ text-align: center; } #head{ background: #eeeeee;height: 80px; } #headLink{ font-size: 20px; } #headWelLink{ font-size: 20px; } </style> </head> <body> <%--头部信息--%> <% User user =(User)session.getAttribute("user"); if(user == null){ response.sendRedirect("login.jsp"); }else { %> <div id="head"> <table width="100%"> <td id="headWelLink">欢迎您:<%=user.getName()%></td> <td align="right" id="headLink"> <a href="logout.jsp">安全退出</a> </td> </table> </div> <% } %> <%--图书信息--%> <% // 设置获取注册时的编码为UTF-8 request.setCharacterEncoding("UTF-8"); Book book = new Book(); // 获取上一个页面传过来的值 String boookname=request.getParameter("bookname"); book.setBookname(boookname); BookServiceImpl service = new BookServiceImpl(); List<Book> list = service.select(book); %> <h1>图书列表</h1> <div id="before"> <form action="admin.jsp" method="post"> 请输入姓名:<input type="text" name="bookname" placeholder="输入图书名称搜索"> <input type="submit" value="查询" /> </form> <a href="javascript: window.history.go(-1)">返回上一级</a> </div> <table align="center" cellpadding="10" cellspacing="10"> <tr bgcolor="green"> <td>编号</td> <td>书名</td> <td>价格</td> <td>作者</td> <td>封皮</td> <td>出版社</td> <td>操作图书</td> </tr> <% String bg = null; for (int i = 0;i<list.size();i++){ Book b =list.get(i); if(i%2 == 0){ bg = "pink"; }else{ bg = "yellow"; } %> <tr bgcolor="<%=bg%>"> <td><%=b.getBookid()%></td> <td><a href="admin_do_info.jsp?bookid=<%=b.getBookid()%>"><%=b.getBookname()%></a></td> <td><%=b.getPrice() %></td> <td><%=b.getAuthor() %></td> <td><%=b.getPicture() %></td> <td><%=b.getPublish() %></td> <td> <a href="admin_update.jsp?bookid=<%=b.getBookid()%>">修改</a> <a href="admin_delete.jsp?bookid=<%=b.getBookid()%>">删除</a> </td> </tr> <% } %> </table> <table align="center"> <tr> <td><a href="admin_addbook.jsp">新增图书</a></td> </tr> </table> </body> </html>
admin_addbook.jsp
<%@ page import="com.sjsq.po.User" %><%-- Created by IntelliJ IDEA. User: shuijianshiqing Date: 2020/5/21 Time: 23:14 To change this template use File | Settings | File Templates. --%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>新增图书</title> <style type="text/css"> h1{ text-align: center; } h4{ text-align: center;color: red; } body{ background-color: antiquewhite; } div{ text-align: center; } #before{ text-align: center; } #head{ background: #eeeeee;height: 80px; } #headLink{ font-size: 20px; } #headWelLink{ font-size: 20px; } </style> </head> <body> <%--头部信息--%> <% User user =(User)session.getAttribute("user"); if(user == null){ response.sendRedirect("login.jsp"); }else { %> <table width="100%" id="head"> <td id="headWelLink">欢迎您:<%=user.getName()%></td> <td align="right" id="headLink"> <a href="logout.jsp">安全退出</a> </td> </table> <% } %> <h1>新增图书</h1> <hr/> <div id="before"> <a href="javascript: window.history.go(-1)">返回上一级</a> </div> </br> <form action="admin_do_addbook.jsp" method="post" name="registerForm"> <div> <tr> <label>图书名称:</label> <input type="text" name="bookname" id="bookname" placeholder="图书名称" autofocus="autofocus"> </tr> </div> <div> <tr> <label>图书价格:</label></td> <input type="text" name="price" id="price" placeholder="图书价格(数字)"> </tr> </div> <div> <tr> <label>图书作者:</label> <input type="text" name="author" id="author" placeholder="图书作者"> </tr> </div> <div> <tr> <label>出版公司:</label> <input type="text" name="publish" id="publish" placeholder="出版公司"> </tr> </div> <div id="submitbtn"> <tr> <button type="submit" onclick="return checkForm()">添加</button> <button type="reset">重置</button> </tr> </div> </form> <script type="text/javascript"> function checkForm() { var bookname = registerForm.bookname.value; var price = registerForm.price.value; //alert(name + pwd + repwd); if (bookname == "" || bookname == null) { alert("请输入图书名称"); registerForm.bookname.focus(); return false; } else if (price == "" || price == null) { alert("请输入图书价格"); registerForm.price.focus(); return false; } alert('添加成功!'); return true; } </script> </body> </html>
admin_delete.jsp
<%@ page import="com.sjsq.po.Book" %> <%@ page import="com.sjsq.dao.impl.BookDaoImpl" %> <%@ page import="com.sjsq.dao.BookDao" %> <%@ page import="com.sjsq.po.User" %><%-- Created by IntelliJ IDEA. User: shuijianshiqing Date: 2020/6/10 Time: 22:45 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>删除图书</title> <style type="text/css"> #before{ text-align: center; } body{ background-color: antiquewhite; } #head{ background: #eeeeee;height: 80px; } #headLink{ font-size: 20px; } #headWelLink{ font-size: 20px; } </style> </head> <body> <%--头部信息--%> <% User user =(User)session.getAttribute("user"); if(user == null){ response.sendRedirect("login.jsp"); }else { %> <div id="head"> <table width="100%"> <td id="headWelLink">欢迎您:<%=user.getName()%></td> <td align="right" id="headLink"> <a href="logout.jsp">安全退出</a> </td> </table> </div> <% } %> <% // 设置获取注册时的编码为UTF-8 request.setCharacterEncoding("UTF-8"); //获取admin.jsp页面的bookid Integer bookid=Integer.parseInt(request.getParameter("bookid")); //引入数据交互层 BookDao dao=new BookDaoImpl(); Book book=new Book(); book = dao.getBook(bookid); System.out.println("删除的图书信息:"); System.out.println(book); boolean flag=dao.deleteBook(bookid); if(flag){ response.sendRedirect("admin.jsp"); }else{ response.sendRedirect("error.jsp"); } %> </body> </html>
admin_detail.jsp
<%-- Created by IntelliJ IDEA. User: shuijianshiqing Date: 2020/5/24 Time: 10:51 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %> <%@ page import="com.sjsq.po.Book" %> <%@ page import="com.sjsq.po.User" %> <% // 获取绝对路径路径 ,开发项目一定要使用绝对路径,不然肯定出错 String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <base href="<%=basePath %>" /> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>图书详情</title> <style type="text/css"> h1{ text-align: center; } body{ background-color: antiquewhite; } a{ text-align:center;font-size: 24px;text-decoration: none; } a:hover{ text-decoration: underline;font-size: 20px; } #before{ text-align: center; } #head{ background: #eeeeee;height: 80px; } #headLink{ font-size: 20px; } #headWelLink{ font-size: 20px; } </style> </head> <body> <%--头部信息--%> <% User user =(User)session.getAttribute("user"); if(user == null){ response.sendRedirect("login.jsp"); }else { %> <div id="head"> <table width="100%"> <td id="headWelLink">欢迎您:<%=user.getName()%></td> <td align="right" id="headLink"> <a href="logout.jsp">安全退出</a> </td> </table> </div> <% } %> <h1>图书详情</h1> <div id="before"> <a href="javascript: window.history.go(-1)">返回上一级</a> </div> <% Book book = (Book)session.getAttribute("book"); %> <table align="center" cellpadding="20" cellspacing="20"> <tr style="font-size: 20px"> <td>图书编号</td> <td>图书名称</td> <td>图书价格</td> <td>图书作者</td> <td>图书封皮</td> <td>图书出版社</td> </tr> <tr> <td><%=book.getBookid()%></td> <td><%=book.getBookname()%></td> <td><%=book.getPrice()%></td> <td><%=book.getAuthor()%></td> <td><%=book.getPicture()%></td> <td><%=book.getPublish()%></td> </tr> <tr> <td colspan="3"></td> <td></td> <td colspan="2"></td> </tr> </table> <div style="text-align:center;font-size: 36px;"> <a href="admin.jsp">图书列表</a> </div> </body> </html>
admin_do_addbook.jsp
<%-- Created by IntelliJ IDEA. User: shuijianshiqing Date: 2020/5/21 Time: 23:45 To change this template use File | Settings | File Templates. --%> <%@page import="com.sjsq.dao.impl.UserDaoImpl"%> <%@page import="com.sjsq.dao.UserDao"%> <%@page import="com.sjsq.po.User"%> <%@ page import="com.sjsq.po.Book" %> <%@ page import="com.sjsq.dao.BookDao" %> <%@ page import="com.sjsq.dao.impl.BookDaoImpl" %> <%@ page contentType="text/html;charset=UTF-8" language="java" 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"> <title>处理新增图书</title> <style type="text/css"> #before{ text-align: center; } #head{ background: #eeeeee;height: 80px; } #headLink{ font-size: 20px; } #headWelLink{ font-size: 20px; } </style> </head> <body> <%--头部信息--%> <% User user =(User)session.getAttribute("user"); if(user == null){ response.sendRedirect("login.jsp"); }else { %> <div id="head"> <table width="100%"> <td id="headWelLink">欢迎您:<%=user.getName()%></td> <td align="right" id="headLink"> <a href="logout.jsp">安全退出</a> </td> </table> </div> <% } %> <% // 设置获取注册时的编码为UTF-8 request.setCharacterEncoding("UTF-8"); Book book=new Book(); //获取admin_addbook.jsp页面提交的账号和密码 String bookname=request.getParameter("bookname"); Double price= Double.parseDouble(request.getParameter("price")); String author=request.getParameter("author"); String publish=request.getParameter("publish"); //获取admin_addbook.jsp页面提交的账号和密码设置到实体类User中 book.setBookname(bookname); book.setPrice(price); book.setAuthor(author); book.setPublish(publish); System.out.println("添加的图书信息"); System.out.println(book); //引入数据交互层 BookDao dao=new BookDaoImpl(); boolean flag=dao.addBook(book); if(flag){ response.sendRedirect("admin.jsp"); }else{ response.sendRedirect("error.jsp"); } %> </body> </html>
admin_do_info.jsp
<%-- Created by IntelliJ IDEA. User: shuijianshiqing Date: 2020/5/24 Time: 10:51 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %> <%@ page import="com.sjsq.po.Book" %> <%@ page import="com.sjsq.service.BookService" %> <%@ page import="com.sjsq.service.impl.BookServiceImpl" %> <% // 获取绝对路径路径 ,开发项目一定要使用绝对路径,不然肯定出错 String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <base href="<%=basePath %>" /> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>书籍信息</title> </head> <body> <% Book book = new Book(); String sid = request.getParameter("bookid"); Integer id = Integer.parseInt(sid); BookService service = new BookServiceImpl(); book.setBookid(id); Book bookCur = service.getBook(book); // 控制台打印出类的信息(日志的前身) System.out.print("admin_do_info.jsp的信息-->"); System.out.println(bookCur); session.setAttribute("book", bookCur); response.sendRedirect("admin_detail.jsp"); %> </body> </html>
admin_do_updatebook.jsp
<%-- Created by IntelliJ IDEA. User: shuijianshiqing Date: 2020/5/21 Time: 23:45 To change this template use File | Settings | File Templates. --%> <%@page import="com.sjsq.dao.impl.UserDaoImpl"%> <%@page import="com.sjsq.dao.UserDao"%> <%@page import="com.sjsq.po.User"%> <%@ page import="com.sjsq.po.Book" %> <%@ page import="com.sjsq.dao.BookDao" %> <%@ page import="com.sjsq.dao.impl.BookDaoImpl" %> <%@ page contentType="text/html;charset=UTF-8" language="java" 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"> <title>处理修改图书</title> <style type="text/css"> #before{ text-align: center; } #head{ background: #eeeeee;height: 80px; } #headLink{ font-size: 20px; } #headWelLink{ font-size: 20px; } </style> </head> <body> <%--头部信息--%> <% User user =(User)session.getAttribute("user"); if(user == null){ response.sendRedirect("login.jsp"); }else { %> <div id="head"> <table width="100%"> <td id="headWelLink">欢迎您:<%=user.getName()%></td> <td align="right" id="headLink"> <a href="logout.jsp">安全退出</a> </td> </table> </div> <% } %> <% // 设置获取注册时的编码为UTF-8 request.setCharacterEncoding("UTF-8"); //获取admin_addbook.jsp页面提交的账号和密码 Integer bookid=Integer.parseInt(request.getParameter("bookid")); String bookname = request.getParameter("bookname"); Double price = Double.parseDouble(request.getParameter("price")); String author=request.getParameter("author"); String publish=request.getParameter("publish"); //引入数据交互层 BookDao dao=new BookDaoImpl(); Book book=new Book(); book = dao.getBook(bookid); book.setBookname(bookname); book.setPrice(price); book.setAuthor(author); book.setPublish(publish); System.out.println("修改的图书信息"); System.out.println(book); boolean flag=dao.updateBook(book); if(flag){ response.sendRedirect("admin.jsp"); }else{ response.sendRedirect("error.jsp"); } %> </body> </html>
admin_update.jsp
<%@ page import="com.sjsq.po.Book" %> <%@ page import="com.sjsq.dao.impl.BookDaoImpl" %> <%@ page import="com.sjsq.dao.BookDao" %> <%@ page import="com.sjsq.po.User" %><%-- Created by IntelliJ IDEA. User: shuijianshiqing Date: 2020/5/21 Time: 23:14 To change this template use File | Settings | File Templates. --%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>修改图书</title> <style type="text/css"> h1{ text-align: center; } h4{ text-align: center;color: red; } body{ background-color: antiquewhite; } div{ text-align: center; } #before{ text-align: center; } #head{ background: #eeeeee;height: 80px; } #headLink{ font-size: 20px; } #headWelLink{ font-size: 20px; } </style> </head> <body> <%--头部信息--%> <% User user =(User)session.getAttribute("user"); if(user == null){ response.sendRedirect("login.jsp"); } %> <h1>修改图书</h1> <hr/> <% //获取admin.jsp页面的bookid Integer bookid=Integer.parseInt(request.getParameter("bookid")); BookDao dao=new BookDaoImpl(); Book book=new Book(); book = dao.getBook(bookid); %> <form action="admin_do_updatebook.jsp" method="post" name="registerForm"> <div> <tr> <input type="hidden" name="bookid" id="bookid" value="<%=book.getBookid()%>"> </tr> </div> <div> <tr> <label>图书名称:</label> <input type="text" name="bookname" id="bookname" value="<%=book.getBookname()%>" autofocus="autofocus"> </tr> </div> <div> <tr> <label>图书价格:</label></td> <input type="text" name="price" id="price" value="<%=book.getPrice()%>"> </tr> </div> <div> <tr> <label>图书作者:</label> <input type="text" name="author" id="author" value="<%=book.getAuthor()%>"> </tr> </div> <div> <tr> <label>出版公司:</label> <input type="text" name="publish" id="publish" value="<%=book.getPublish()%>"> </tr> </div> <div> <tr> <button type="submit" onclick="return checkForm()">修改</button> <button type="reset">重置</button> </tr> </div> </form> <script type="text/javascript"> function checkForm() { var bookname = registerForm.bookname.value; var price = registerForm.price.value; //alert(name + pwd + repwd); if (bookname == "" || bookname == null) { alert("请输入图书名称"); registerForm.bookname.focus(); return false; } else if (price == "" || price == null) { alert("请输入图书价格"); registerForm.price.focus(); return false; } alert('修改成功!'); return true; } </script> </body> </html>
error.jsp
<%-- Created by IntelliJ IDEA. User: shuijianshiqing Date: 2020/6/10 Time: 22:28 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>错误页面</title> </head> <body> 操作出错,请回到上一个页面! </hr> </body> </html>