一. 创建数据库和表
在数据库studyproject下创建一个book表
CREATE TABLE `studyproject`.`book` (
`id` INT NOT NULL AUTO_INCREMENT,
`bookname` VARCHAR(45),
`price` DECIMAL(11,2),
`author` VARCHAR(45),
`sale` INT,
`stock` INT,
`img` VARCHAR(45),
PRIMARY KEY (`id`));
insert into book(id, bookname, author, price, sale, stock, img)
values(null , '数据结构与算法' , '严敏君' , 58.5 , 6 , 13 , 'static/img/default.jpg');
insert into book(id, bookname, author, price, sale, stock, img)
values(null , 'Java从入门到精通' , '清华' , 78.9 , 80 , 20 , 'static/img/default.jpg');
insert into book(id, bookname, author, price, sale, stock, img)
values(null , 'C++编程思想' , 'Bruce' , 45.5 , 14 , 95 , 'static/img/default.jpg');
insert into book(id, bookname, author, price, sale, stock, img)
values(null , 'JavaScript高级程序设计' , '李松峰' , 98.8, 12 , 53 , 'static/img/default.jpg');
insert into book(id, bookname, author, price, sale, stock, img)
values(null , 'python编程' , '袁国忠' , 69.8, 73 , 135 , 'static/img/default.jpg');
二. 编写JavaBean对象
1. 编写book表对应的JavaBean对象
package server.pojo;
import java.math.BigDecimal;
public class Book {
private Integer id;
private String bookname;
private BigDecimal price;
private String author;
private Integer sale;
private Integer stock;
private String img = "static/img/default.jpg";
public Book() {
}
public Book(Integer id, String bookname, BigDecimal price, String author, Integer sale, Integer stock, String img) {
this.id = id;
this.bookname = bookname;
this.price = price;
this.author = author;
this.sale = sale;
this.stock = stock;
//要求给定的图书封面图片路径不能为空
if(img != null && !"".equals(img)){
this.img = img;
}
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getBookname() {
return bookname;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Integer getSale() {
return sale;
}
public void setSale(Integer sale) {
this.sale = sale;
}
public Integer getStock() {
return stock;
}
public void setStock(Integer stock) {
this.stock = stock;
}
public String getImg() {
return img;
}
public void setImg(String img) {
this.img = img;
}
@Override
public String toString() {
return "Book{" +
"id=" + id +
", bookname='" + bookname + '\'' +
", price=" + price +
", author='" + author + '\'' +
", sale=" + sale +
", stock=" + stock +
", img='" + img + '\'' +
'}';
}
}
2. 编写page对应的JavaBean对象
package server.pojo;
import java.util.List;
public class Page<T> {
public static final Integer PAGE_SIZE = 2;
private Integer pageNumber;
private Integer pageTotalNumber;
private Integer pageSize = PAGE_SIZE;
private Integer dataTotalCount;
private List<T> items;
public Integer getPageNumber() {
return pageNumber;
}
public void setPageNumber(Integer pageNumber) {
//防止跳到不在页码数范围内的其他页码
if(pageNumber < 1){
pageNumber = 1;
}
if(pageNumber > pageTotalNumber){
pageNumber = pageTotalNumber;
}
this.pageNumber = pageNumber;
}
public Integer getPageTotalNumber() {
return pageTotalNumber;
}
public void setPageTotalNumber(Integer pageTotalNumber) {
this.pageTotalNumber = pageTotalNumber;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public Integer getDataTotalCount() {
return dataTotalCount;
}
public void setDataTotalCount(Integer dataTotalCount) {
this.dataTotalCount = dataTotalCount;
}
public List<T> getItems() {
return items;
}
public void setItems(List<T> items) {
this.items = items;
}
@Override
public String toString() {
return "Page{" +
"pageNumber=" + pageNumber +
", pageTotalNumber=" + pageTotalNumber +
", pageSize=" + pageSize +
", dataTotalCount=" + dataTotalCount +
", items=" + items +
'}';
}
}
三. Dao层 —— 操作数据库
1.在dao包下编写BookDao接口
package server.dao;
import server.pojo.Book;
import java.util.List;
public interface BookDao {
public int addBook(Book book);
public int deleteBookById(Integer id);
public int updateBook(Book book);
public Book queryBookById(Integer id);
public List<Book> queryBooks();
public Integer queryDataTotalCount();
public List<Book> queryPageItems(int begin, int pageSize);
public Integer queryDataTotalCountByPrice(int min, int max);
public List<Book> queryPageItemsByPrice(int begin, int pageSize, int min, int max);
}
2. 在dao.impl包下编写一个BookDaoImpl类,继承BaseDao类并实现BookDao接口
package server.dao.impl;
import server.dao.BookDao;
import server.pojo.Book;
import java.util.List;
public class BookDaoImpl extends BaseDao implements BookDao {
@Override
public int addBook(Book book) {
String sql = "insert into book(bookname, price, author, sale, stock, img) values(?,?,?,?,?,?)";
return updateData(sql,book.getBookname(),book.getPrice(),book.getAuthor(),book.getSale(),book.getStock(),book.getImg());
}
@Override
public int deleteBookById(Integer id) {
String sql = "delete from book where id = ?";
return updateData(sql,id);
}
@Override
public int updateBook(Book book) {
String sql = "update book set bookname=?, price=?, author=?, sale=?, stock=?, img=? where id=?";
return updateData(sql,book.getBookname(),book.getPrice(),book.getAuthor(),book.getSale(),book.getStock(),book.getImg(),book.getId());
}
@Override
public Book queryBookById(Integer id) {
String sql = "select * from book where id = ?";
return queryOneData(Book.class, sql, id);
}
@Override
public List<Book> queryBooks() {
String sql = "select * from book";
return queryList(Book.class, sql);
}
@Override
public Integer queryDataTotalCount() {
String sql = "select count(*) from book";
Number count = (Number) querySingleValue(sql);
return count.intValue();
}
@Override
public List<Book> queryPageItems(int begin, int pageSize) {
String sql = "select * from book limit ?,?";
return queryList(Book.class,sql,begin,pageSize);
}
@Override
public Integer queryDataTotalCountByPrice(int min, int max) {
String sql = "select count(*) from book where price between ? and ?";
Number count = (Number) querySingleValue(sql,min,max);
return count.intValue();
}
@Override
public List<Book> queryPageItemsByPrice(int begin, int pageSize, int min, int max) {
String sql = "select * from book where price between ? and ? order by price limit ?,?";
return queryList(Book.class,sql,min,max,begin,pageSize);
}
}
四. Service层
1. 在Service包下编写BookService接口
package server.service;
import server.pojo.Book;
import server.pojo.Page;
import java.util.List;
public interface BookService {
public void addBook(Book book);
public void deleteBookById(Integer id);
public void updateBook(Book book);
public Book queryBookById(Integer id);
public List<Book> queryBooks();
public Page<Book> page(int pageNumber, int pageSize);
public Page<Book> pageByPrice(int pageNumber, int pageSize, int min, int max);
}
2. 在service.impl包下编写BookServiceImpl类来实现BookService接口
package server.service.impl;
import server.dao.BookDao;
import server.dao.impl.BookDaoImpl;
import server.pojo.Book;
import server.pojo.Page;
import server.service.BookService;
import java.util.List;
public class BookServiceImpl implements BookService {
private BookDao bookDao = new BookDaoImpl();
@Override
public void addBook(Book book) {
bookDao.addBook(book);
}
@Override
public void deleteBookById(Integer id) {
bookDao.deleteBookById(id);
}
@Override
public void updateBook(Book book) {
bookDao.updateBook(book);
}
@Override
public Book queryBookById(Integer id) {
return bookDao.queryBookById(id);
}
@Override
public List<Book> queryBooks() {
return bookDao.queryBooks();
}
@Override
public Page<Book> page(int pageNumber, int pageSize) {
Page<Book> page = new Page<>();
//设置每页显示的数量
page.setPageSize(pageSize);
//求总的记录数
Integer dataTotalCount = bookDao.queryDataTotalCount();
//设置总记录数
page.setDataTotalCount(dataTotalCount);
//求总页码数
Integer pageTotalNumber = dataTotalCount / pageSize;
if(dataTotalCount % pageSize > 0){
pageTotalNumber += 1;
}
//设置总页码数
page.setPageTotalNumber(pageTotalNumber);
//设置当前页码
page.setPageNumber(pageNumber);
//求当前页数据的开始索引
int begin = (page.getPageNumber() - 1) * pageSize;
//求当前页的数据
List<Book> items = bookDao.queryPageItems(begin,pageSize);
//设置当前页的每一个数据
page.setItems(items);
return page;
}
@Override
public Page<Book> pageByPrice(int pageNumber, int pageSize, int min, int max) {
Page<Book> page = new Page<>();
//设置每页显示的数量
page.setPageSize(pageSize);
//求总的记录数
Integer dataTotalCount = bookDao.queryDataTotalCountByPrice(min,max);
//设置总记录数
page.setDataTotalCount(dataTotalCount);
//求总页码数
Integer pageTotalNumber = dataTotalCount / pageSize;
if(dataTotalCount % pageSize > 0){
pageTotalNumber += 1;
}
//设置总页码数
page.setPageTotalNumber(pageTotalNumber);
//设置当前页码
page.setPageNumber(pageNumber);
//求当前页数据的开始索引
int begin = (page.getPageNumber() - 1) * pageSize;
//求当前页的数据
List<Book> items = bookDao.queryPageItemsByPrice(begin,pageSize,min,max);
//设置当前页的每一个数据
page.setItems(items);
return page;
}
}
五. Web层 —— 实现Servlet程序
1. 编写BookServlet程序来实现后台分页显示、删除、添加、修改数据
package server.web;
import server.pojo.Book;
import server.pojo.Page;
import server.service.BookService;
import server.service.impl.BookServiceImpl;
import server.utils.WebUtils;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/manager/bookServlet")
public class BookServlet extends BaseServlet{
private BookService bookService = new BookServiceImpl();
protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取请求的参数pageNumber和pageSize
int pageNumber = WebUtils.parseInt(req.getParameter("pageNumber"),1);
int pageSize = WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
//调用BookService类的Page(pageNumber,pageSize)方法
Page<Book> page = bookService.page(pageNumber,pageSize);
//保存Page对象到Request域中
req.setAttribute("page",page);
//重定向到/pages/manager/book_manager.jsp页面
req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req,resp);
}
protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取请求的id
String id = req.getParameter("id");
//调用BookService类的deleteBookById()方法删除对应的图书
bookService.deleteBookById(Integer.parseInt(id));
//重定向到图书列表页面
resp.sendRedirect(req.getContextPath() + "/manager/bookServlet?action=page&pageNumber="+req.getParameter("pageNumber"));
}
protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
int pageNumber = WebUtils.parseInt(req.getParameter("pageNumber"),0);
pageNumber += 1;
//获取请求的参数
Book book = WebUtils.copyParamToBean(req.getParameterMap(),new Book());
//调用BookService类的addBook()方法保存图书
bookService.addBook(book);
//重定向到图书列表页面
resp.sendRedirect(req.getContextPath() + "/manager/bookServlet?action=page&pageNumber="+pageNumber);
}
protected void getBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取请求的参数(图书id)
String id = req.getParameter("id");
//调用BookService类的queryBookById()方法查询图书
Book book = bookService.queryBookById(Integer.parseInt(id));
//保存图书到Request作用域中
req.setAttribute("book",book);
//请求转发到/pages/manager/book_edit.jsp页面
req.getRequestDispatcher("/pages/manager/book_edit.jsp").forward(req,resp);
}
protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取请求的参数
Book book = WebUtils.copyParamToBean(req.getParameterMap(),new Book());
//调用BookService类的updateBook()方法修改图书信息
bookService.updateBook(book);
//重定向到图书列表页面
resp.sendRedirect(req.getContextPath() + "/manager/bookServlet?action=page&pageNumber="+req.getParameter("pageNumber"));
}
}
2. 编写ClientBookServlet程序来实现用户端分页显示数据和按价格查询数据
package server.web;
import server.pojo.Book;
import server.pojo.Page;
import server.service.BookService;
import server.service.impl.BookServiceImpl;
import server.utils.WebUtils;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/client/clientBookServlet")
public class ClientBookServlet extends BaseServlet{
private BookService bookService = new BookServiceImpl();
protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取请求的参数pageNumber和pageSize
int pageNumber = WebUtils.parseInt(req.getParameter("pageNumber"),1);
int pageSize = WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
//调用BookService类的Page(pageNumber,pageSize)方法
Page<Book> page = bookService.page(pageNumber,pageSize);
//保存Page对象到Request域中
req.setAttribute("page",page);
//重定向到pages/client/index.jsp页面
req.getRequestDispatcher("/pages/client/index.jsp").forward(req,resp);
}
protected void pageByPrice(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取请求的参数pageNumber和pageSize
int pageNumber = WebUtils.parseInt(req.getParameter("pageNumber"),1);
int pageSize = WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
int min = WebUtils.parseInt(req.getParameter("min"),0);
int max = WebUtils.parseInt(req.getParameter("max"),Integer.MAX_VALUE);
//调用BookService类的Page(pageNumber,pageSize)方法
Page<Book> page = bookService.pageByPrice(pageNumber,pageSize, min, max);
//保存Page对象到Request域中
req.setAttribute("page",page);
//重定向到pages/client/index.jsp页面
req.getRequestDispatcher("/pages/client/index.jsp").forward(req,resp);
}
}
3. 编写WebUtils类
package server.utils;
import org.apache.commons.beanutils.BeanUtils;
import java.util.Map;
public class WebUtils {
public static <T> T copyParamToBean(Map value, T bean){
try {
BeanUtils.populate(bean, value); //把所有请求注入到JavaBean中
} catch (Exception e) {
e.printStackTrace();
}
return bean;
}
//将字符串转化成整型
public static int parseInt(String strInt,int defaultValue){
try {
return Integer.parseInt(strInt);
} catch (Exception e) {
//e.printStackTrace();
}
return defaultValue;
}
}
六. 编写前端页面
1.index.jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:forward page="/client/clientBookServlet?action=page"></jsp:forward>
2.client目录下的index.jsp页面,用户客户端分页显示数据、按价格搜索并分页显示满足条件的数据
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
<title>首页</title>
<% //获取当前工程的路径
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
%>
<base href="<%=basePath%>">
</head>
<body>
<div align="center">
<%-- 如果用户还没有登录,显示登录和注册菜单--%>
<c:if test="${empty sessionScope.user}">
<a href="pages/user/login.jsp"><input type = "button" value = "登录"/></a>
<a href="pages/user/regist.jsp"><input type = "button" value = "注册"/></a>
</c:if>
<%-- 如果用户已经登录,显示欢迎用户、注销、后台管理--%>
<c:if test="${not empty sessionScope.user}">
欢迎${sessionScope.user.username}
<a href="userServlet?action=logout"><input type = "button" value = "注销"/></a>
<a href="pages/manager/manager.jsp"><input type = "button" value = "后台管理"/></a>
</c:if>
</div>
<div align="center">
<form action="client/clientBookServlet" method="get">
<input type="hidden" name="action" value="pageByPrice">
价格:<input id="min" type="text" name="min" value="${param.min}"/>元 — <input id="max" type="text" name="max" value="${param.max}"/>元 <input type="submit" value="查询"/>
</form>
<c:forEach items="${requestScope.page.items}" var="book">
<div>
<div>
<img src="${book.img}" width="150px"/>
</div>
<div>
<div class="book_name">
<span class="sp1">书名:</span>
<span class="sp2">${book.bookname}</span>
</div>
<div class="book_author">
<span class="sp1">作者:</span>
<span class="sp2">${book.author}</span>
</div>
<div class="book_price">
<span class="sp1">价格:</span>
<span class="sp2">${book.price}</span>
</div>
<div class="book_sales">
<span class="sp1">销量:</span>
<span class="sp2">${book.sale}</span>
</div>
<div class="book_amount">
<span class="sp1">库存:</span>
<span class="sp2">${book.stock}</span>
</div>
<div class="book_add">
<button>加入购物车</button>
</div>
</div>
</div>
</c:forEach>
</div>
<div align="center">
<%-- 如果当前页面是第一页,则不显示首页和上一页 --%>
<c:if test="${requestScope.page.pageNumber > 1}">
<a href = "client/clientBookServlet?action=page&pageNumber=1">首页</a>
<a href = "client/clientBookServlet?action=page&pageNumber=${requestScope.page.pageNumber-1}">上一页</a>
</c:if>
<%-- 页码输出的开始 --%>
<c:choose>
<%-- 情况1:如果总页码小于等于3,则显示页码的范围是:1 —— 总页码数 --%>
<c:when test="${requestScope.page.pageTotalNumber <= 3}">
<c:set var="begin" value="1"/>
<c:set var="end" value="${requestScope.page.pageTotalNumber}"/>
</c:when>
<%-- 情况2:如果总页码大于3 --%>
<c:when test="${requestScope.page.pageTotalNumber > 3}">
<c:choose>
<%-- 情况2.1:如果当前页码为前两页时,则显示页码的范围是:1 —— 3 --%>
<c:when test="${requestScope.page.pageNumber <= 2}">
<c:set var="begin" value="1"/>
<c:set var="end" value="3"/>
</c:when>
<%-- 情况2.2:如果当前页码为最后两页时,则显示页码的范围是:总页数减2 —— 最后一页 --%>
<c:when test="${requestScope.page.pageNumber >= requestScope.page.pageTotalNumber-2}">
<c:set var="begin" value="${requestScope.page.pageTotalNumber-2}"/>
<c:set var="end" value="${requestScope.page.pageTotalNumber}"/>
</c:when>
<%-- 情况2.3:如果当前页码为其他页码时,则显示页码的范围是:当前页码减1 —— 当前页码加1 --%>
<c:otherwise>
<c:set var="begin" value="${requestScope.page.pageNumber-1}"/>
<c:set var="end" value="${requestScope.page.pageNumber+1}"/>
</c:otherwise>
</c:choose>
</c:when>
</c:choose>
<c:forEach begin="${begin}" end="${end}" var="i">
<c:if test="${i == requestScope.page.pageNumber}">
【${i}】
</c:if>
<c:if test="${i != requestScope.page.pageNumber}">
<a href="client/clientBookServlet?action=page&pageNumber=${i}">${i}</a>
</c:if>
</c:forEach>
<%-- 页码输出的结束 --%>
<%-- 如果当前页面已经是最后一页,则不显示下一页和末页 --%>
<c:if test="${requestScope.page.pageNumber < requestScope.page.pageTotalNumber}">
<a href = "client/clientBookServlet?action=page&pageNumber=${requestScope.page.pageNumber+1}">下一页</a>
<a href = "client/clientBookServlet?action=page&pageNumber=${requestScope.page.pageTotalNumber}">末页</a>
</c:if>
共${requestScope.page.pageTotalNumber}页,${requestScope.page.dataTotalCount}条记录,到第<input value="${param.pageNumber}" id="page_input">页
<input type="button" value="确定" onclick="location.href='client/clientBookServlet?action=page&pageNumber='+document.getElementById('page_input').value">
<!--location.href表示链接的路径,document.getElementById('page_input').value表示获取输入框中的内容-->
</div>
</body>
</html>
3.manager目录下的manager.jsp页面,用来指向book_manager.jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
<title>后台管理</title>
<base href="http://localhost:8080/StudyProject_war_exploded/">
</head>
<body>
<div>
<h1>欢迎管理员进入后台管理系统</h1>
<a href="manager/bookServlet?action=page">图书管理</a>
<a href="">订单管理</a>
<a href="index.jsp">返回首页</a>
</div>
</body>
</html>
4.manager目录下的book_manager.jsp页面,用来分页显示数据、并拥有添加、删除、修改功能
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
<% //获取当前工程的路径
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
%>
<base href="<%=basePath%>">
<title>图书管理</title>
</head>
<body>
<div align="center">图书管理系统</div>
<table border = "1" width = "600" height = "300" cellspacing = "0" cellpadding = "10" align="center">
<tr align = "center" >
<td>名称</td>
<td>价格</td>
<td>作者</td>
<td>销量</td>
<td>库存</td>
<td colspan="2">操作</td>
</tr>
<c:forEach items="${requestScope.page.items}" var="book">
<tr align = "center">
<td>${book.bookname}</td>
<td>${book.price}</td>
<td>${book.author}</td>
<td>${book.sale}</td>
<td>${book.stock}</td>
<td><a href="manager/bookServlet?action=getBook&id=${book.id}&method=update&pageNumber=${requestScope.page.pageNumber}">修改</a></td>
<td><a href="manager/bookServlet?action=delete&id=${book.id}&pageNumber=${requestScope.page.pageNumber}" onclick="return (confirm('确定删除吗?'));">删除</a></td>
</tr>
</c:forEach>
<tr align = "right">
<td colspan="7"><a href="pages/manager/book_edit.jsp?method=add&pageNumber=${requestScope.page.pageTotalNumber}">添加图书</a></td>
</tr>
</table>
<div align="center">
<%-- 如果当前页面是第一页,则不显示首页和上一页 --%>
<c:if test="${requestScope.page.pageNumber > 1}">
<a href = "manager/bookServlet?action=page&pageNumber=1">首页</a>
<a href = "manager/bookServlet?action=page&pageNumber=${requestScope.page.pageNumber-1}">上一页</a>
</c:if>
<%-- 页码输出的开始 --%>
<c:choose>
<%-- 情况1:如果总页码小于等于3,则显示页码的范围是:1 —— 总页码数 --%>
<c:when test="${requestScope.page.pageTotalNumber <= 3}">
<c:set var="begin" value="1"/>
<c:set var="end" value="${requestScope.page.pageTotalNumber}"/>
</c:when>
<%-- 情况2:如果总页码大于3 --%>
<c:when test="${requestScope.page.pageTotalNumber > 3}">
<c:choose>
<%-- 情况2.1:如果当前页码为前两页时,则显示页码的范围是:1 —— 3 --%>
<c:when test="${requestScope.page.pageNumber <= 2}">
<c:set var="begin" value="1"/>
<c:set var="end" value="3"/>
</c:when>
<%-- 情况2.2:如果当前页码为最后两页时,则显示页码的范围是:总页数减2 —— 最后一页 --%>
<c:when test="${requestScope.page.pageNumber >= requestScope.page.pageTotalNumber-2}">
<c:set var="begin" value="${requestScope.page.pageTotalNumber-2}"/>
<c:set var="end" value="${requestScope.page.pageTotalNumber}"/>
</c:when>
<%-- 情况2.3:如果当前页码为其他页码时,则显示页码的范围是:当前页码减1 —— 当前页码加1 --%>
<c:otherwise>
<c:set var="begin" value="${requestScope.page.pageNumber-1}"/>
<c:set var="end" value="${requestScope.page.pageNumber+1}"/>
</c:otherwise>
</c:choose>
</c:when>
</c:choose>
<c:forEach begin="${begin}" end="${end}" var="i">
<c:if test="${i == requestScope.page.pageNumber}">
【${i}】
</c:if>
<c:if test="${i != requestScope.page.pageNumber}">
<a href="manager/bookServlet?action=page&pageNumber=${i}">${i}</a>
</c:if>
</c:forEach>
<%-- 页码输出的结束 --%>
<%-- 如果当前页面已经是最后一页,则不显示下一页和末页 --%>
<c:if test="${requestScope.page.pageNumber < requestScope.page.pageTotalNumber}">
<a href = "manager/bookServlet?action=page&pageNumber=${requestScope.page.pageNumber+1}">下一页</a>
<a href = "manager/bookServlet?action=page&pageNumber=${requestScope.page.pageTotalNumber}">末页</a>
</c:if>
共${requestScope.page.pageTotalNumber}页,${requestScope.page.dataTotalCount}条记录,到第<input value="${param.pageNumber}" id="page_input">页
<input type="button" value="确定" onclick="location.href='manager/bookServlet?action=page&pageNumber='+document.getElementById('page_input').value">
<!--location.href表示链接的路径,document.getElementById('page_input').value表示获取输入框中的内容-->
</div>
</body>
</html>
5.manager目录下的book_edit.jsp页面,用来添加、修改数据
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
<title>编辑图书</title>
<base href="http://localhost:8080/StudyProject_war_exploded/">
</head>
<body>
<div align="center">编辑图书</div>
<div align="center">
<form action="manager/bookServlet" method="post">
<input type="hidden" name="action" value="${param.method}">
<input type="hidden" name="pageNumber" value="${param.pageNumber}">
<input type="hidden" name="id" value="${requestScope.book.id}">
<table>
<tr>
<td>名称</td>
<td>价格</td>
<td>作者</td>
<td>销量</td>
<td>库存</td>
<td colspan="2">操作</td>
</tr>
<tr>
<td><input type="text" name="bookname" value="${requestScope.book.bookname}"/></td>
<td><input type="text" name="price" value="${requestScope.book.price}"/></td>
<td><input type="text" name="author" value="${requestScope.book.author}"/></td>
<td><input type="text" name="sale" value="${requestScope.book.sale}"/></td>
<td><input type="text" name="stock" value="${requestScope.book.stock}"/></td>
<td><input type="submit" value="提交"/></td>
</tr>
</table>
</form>
</div>
</body>
</html>