feature/hy
heyou 3 weeks ago
parent 12cf9b7cc1
commit 3d55fed611

@ -5,88 +5,176 @@ import java.math.BigDecimal;
/**
* @author yj
* @create 2020-10-02 19:51
*
* BookID
*/
public class Book {
private Integer id;//id
private String name;//姓名
private String author;//作者
// 书籍的唯一标识符
private Integer id;
// 书籍的名称
private String name;
// 书籍的作者
private String author;
// 书籍的价格使用BigDecimal类型以确保精度
private BigDecimal price;
// 书籍的销量
private Integer sales;
// 书籍的库存数量
private Integer stock;
// 书籍的分类
private String classification;
private String imgPath="static/img/default.jpg";
// 书籍的图片路径,默认值为"static/img/default.jpg"
private String imgPath = "static/img/default.jpg";
/**
*
* @return
*/
public String getClassification() {
return classification;
}
/**
*
* @param classification
*/
public void setClassification(String classification) {
this.classification = classification;
}
/**
* ID
* @return ID
*/
public Integer getId() {
return id;
}
/**
* ID
* @param id ID
*/
public void setId(Integer id) {
this.id = id;
}
/**
*
* @return
*/
public String getName() {
return name;
}
/**
*
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
*
* @return
*/
public String getAuthor() {
return author;
}
/**
*
* @param author
*/
public void setAuthor(String author) {
this.author = author;
}
/**
*
* @return
*/
public BigDecimal getPrice() {
return price;
}
/**
*
* @param price
*/
public void setPrice(BigDecimal price) {
this.price = price;
}
/**
*
* @return
*/
public Integer getSales() {
return sales;
}
/**
*
* @param sales
*/
public void setSales(Integer sales) {
this.sales = sales;
}
/**
*
* 00
* @return
*/
public Integer getStock() {
if(stock<0) stock=0;return stock;
if (stock < 0) stock = 0;
return stock;
}
/**
*
* 00
* @param stock
*/
public void setStock(Integer stock) {
if(stock<0) stock=0;
if (stock < 0) stock = 0;
this.stock = stock;
}
/**
*
* @return
*/
public String getImgPath() {
return imgPath;
}
/**
*
* null使"static/img/default.jpg"
* @param imgPath
*/
public void setImgPath(String imgPath) {
if(imgPath==null||"".equals(imgPath)) {
if (imgPath == null || "".equals(imgPath)) {
this.imgPath = "static/img/default.jpg";
} else {
this.imgPath = imgPath;
}
}
/**
* toString
* @return
*/
@Override
public String toString() {
return "Book{" +
@ -101,9 +189,23 @@ public class Book {
'}';
}
/**
*
*/
public Book() {
}
/**
*
* @param id ID
* @param name
* @param author
* @param price
* @param sales
* @param stock
* @param classification
* @param imgPath
*/
public Book(Integer id, String name, String author, BigDecimal price, Integer sales, Integer stock, String classification, String imgPath) {
this.id = id;
this.name = name;
@ -114,4 +216,4 @@ public class Book {
this.classification = classification;
this.imgPath = imgPath;
}
}
}

@ -8,34 +8,108 @@ import java.util.List;
/**
* @author yj
* @create 2020-08-23 21:08
*
* BookDao
*/
public interface BookDao {
/**
*
* @param book
* @return 10
*/
public int addBook(Book book);
/**
* ID
* @param id ID
* @return 10
*/
public int deleteBookById(Integer id);
/**
*
* @param book
* @return 10
*/
public int updateBook(Book book);
/**
* ID
* @param id ID
* @return null
*/
public Book queryBookById(Integer id);
/**
*
* @return
*/
public List<Book> queryBooks();
/**
*
* @return
*/
Integer queryForPageTotalCount();
/**
*
* @param begin
* @param pageSize
* @return
*/
List<Book> queryForPageItems(int begin, int pageSize);
/**
*
* @param min
* @param max
* @return
*/
Integer queryForPageTotalCountByPrice(int min, int max);
/**
*
* @param begin
* @param pageSize
* @param min
* @param max
* @return
*/
List<Book> queryForPageItemsByPrice(int begin, int pageSize, int min, int max);
/**
*
* @param nameorauthor
* @return
*/
Integer queryForPageTotalCountByNameOrAuthor(String nameorauthor);
/**
*
* @param begin
* @param pageSize
* @param nameorauthor
* @return
*/
List<Book> queryForPageItemsByNameOrAuthor(int begin, int pageSize, String nameorauthor);
/**
*
* @return
*/
List<Book> queryForPageItemsOrder();
/**
*
* @return
*/
BigDecimal queryTotalMoney();
/**
*
* @return
*/
public Integer queryBooknums();
}
}

@ -9,28 +9,84 @@ import java.util.List;
/**
* @author yj
* @create 2020-08-24 14:40
*
* BookService
*/
public interface BookService {
/**
*
* @param book
*/
public void addBook(Book book);
/**
*
* @param book
*/
public void updateBook(Book book);
/**
* ID
* @param id ID
*/
public void deleteBookById(Integer id);
/**
* ID
* @param id ID
* @return null
*/
public Book queryBookById(Integer id);
/**
*
* @return
*/
public List<Book> queryBooks();
/**
*
* @param pageNo
* @param pageSize
* @return
*/
Page<Book> page(int pageNo, int pageSize);
/**
*
* @param pageNo
* @param pageSize
* @param min
* @param max
* @return
*/
Page<Book> pageByPrice(int pageNo, int pageSize, int min, int max);
/**
*
* @param pageNo
* @param pageSize
* @param nameOrAuthor
* @return
*/
Page<Book> pageByNameOrAuthor(int pageNo, int pageSize, String nameOrAuthor);
/**
*
* @return
*/
Page<Book> pageOrder();
/**
*
* @return
*/
public Integer queryTotalBooks();
/**
*
* @return
*/
BigDecimal queryTotalMoney();
}
}

@ -10,59 +10,102 @@ import java.math.BigDecimal;
/**
* @author yj
* @create 2020-08-24 11:01
*
* BookDaoTestBookDaoBookDaoImpl
* JUnit
*/
public class BookDaoTest {
// 创建BookDao接口的实现类BookDaoImpl对象
BookDao bookDao = new BookDaoImpl();
/**
*
*/
@Test
public void addBook() {
//`name`,`author`,`classification`,`price`,`sales`,`stock`,`img_path`
//Integer id, String name, String author, BigDecimal price, Integer sales, Integer stock, String classification, String imgPath)
bookDao.addBook(new Book(null,"舌的中国","yjjj",new BigDecimal("5"),100,23,"",""));
// 创建一个新的Book对象并调用addBook方法将其添加到数据库中
bookDao.addBook(new Book(null, "舌的中国", "yjjj", new BigDecimal("5"), 100, 23, "", ""));
}
/**
*
*/
@Test
public void deleteBook() {
// 调用deleteBookById方法删除ID为3的书籍
bookDao.deleteBookById(3);
}
/**
* ID
*/
@Test
public void queryById() {
// 调用queryBookById方法查询ID为64的书籍并打印结果
System.out.println(bookDao.queryBookById(64));
}
/**
*
*/
@Test
public void update() {
bookDao.updateBook(new Book(35,"舌尖的中国","yj",new BigDecimal("5"),100,23,"文学",""));
// 创建一个新的Book对象并调用updateBook方法更新ID为35的书籍信息
bookDao.updateBook(new Book(35, "舌尖的中国", "yj", new BigDecimal("5"), 100, 23, "文学", ""));
}
/**
*
*/
@Test
public void queryList() {
// 调用queryBooks方法查询所有书籍并打印结果
System.out.println(bookDao.queryBooks());
}
/**
*
*/
@Test
public void queryForPageTotalCount() {
// 调用queryForPageTotalCount方法查询所有书籍的总记录数并打印结果
System.out.println(bookDao.queryForPageTotalCount());
}
/**
*
*/
@Test
public void queryForPageItems() {
System.out.println(bookDao.queryForPageItems(1,4));
// 调用queryForPageItems方法分页查询书籍并打印结果
System.out.println(bookDao.queryForPageItems(1, 4));
}
/**
*
*/
@Test
public void queryForPageTotalCountByPrice() {
System.out.println(bookDao.queryForPageTotalCountByPrice(10,50));
// 调用queryForPageTotalCountByPrice方法查询价格在10到50之间的书籍总记录数并打印结果
System.out.println(bookDao.queryForPageTotalCountByPrice(10, 50));
}
/**
*
*/
@Test
public void queryForPageItemsByPrice() {
System.out.println(bookDao.queryForPageItemsByPrice(1,4,10,50));
// 调用queryForPageItemsByPrice方法根据价格范围分页查询书籍并打印结果
System.out.println(bookDao.queryForPageItemsByPrice(1, 4, 10, 50));
}
/**
*
*/
@Test
public void queryBooknums() {
// 调用queryBooknums方法查询书籍的总数量并打印结果
System.out.println(bookDao.queryBooknums());
}
}
}

@ -12,43 +12,78 @@ import java.math.BigDecimal;
/**
* @author yj
* @create 2020-08-24 14:57
*
* BookServiceTestBookServiceBookServiceImpl
* JUnit
*/
public class BookServiceTest {
// 创建BookDao接口的实现类BookDaoImpl对象
private BookDao bookDao = new BookDaoImpl();
// 创建BookService接口的实现类BookServiceImpl对象
private BookService bookService = new BookServiceImpl();
/**
*
*/
@Test
public void addBook() {
bookDao.addBook(new Book(null,"舌尖上的中国","yj",new BigDecimal("5"),100,23,"文学",""));
// 创建一个新的Book对象并调用addBook方法将其添加到数据库中
bookDao.addBook(new Book(null, "舌尖上的中国", "yj", new BigDecimal("5"), 100, 23, "文学", ""));
}
/**
*
*/
@Test
public void updateBook() {
bookDao.updateBook(new Book(null,"舌尖上的中国","yj",new BigDecimal("5"),100,23,"文学",""));
// 创建一个新的Book对象并调用updateBook方法更新书籍信息
bookDao.updateBook(new Book(null, "舌尖上的中国", "yj", new BigDecimal("5"), 100, 23, "文学", ""));
}
/**
* ID
*/
@Test
public void deleteBookById() {
// 调用deleteBookById方法删除ID为25的书籍并打印受影响的行数
System.out.println(bookDao.deleteBookById(25));
}
/**
* ID
*/
@Test
public void queryBookById() {
// 调用queryBookById方法查询ID为23的书籍并打印结果
System.out.println(bookDao.queryBookById(23));
}
/**
*
*/
@Test
public void queryBooks() {
// 调用queryBooks方法查询所有书籍并打印结果
System.out.println(bookDao.queryBooks());
}
/**
*
*/
@Test
public void page() {
// 调用queryForPageTotalCount方法查询所有书籍的总记录数并打印结果
System.out.println(bookDao.queryForPageTotalCount());
}
/**
*
*/
@Test
public void queryTotalMoney() {
// 调用queryTotalMoney方法查询所有书籍的总金额并打印结果
System.out.println(bookDao.queryTotalMoney());
}
}

@ -10,26 +10,54 @@ import java.lang.reflect.Method;
/**
* @author yj
* @create 2020-08-23 16:51
*
* BaseServletServletServlet
* ServletdoGetdoPost
*/
public abstract class BaseServlet extends HttpServlet {
/**
* GETdoPost
* @param req HttpServletRequest
* @param resp HttpServletResponse
* @throws ServletException
* @throws IOException
*/
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
doPost(req, resp);
}
/**
* POST
* @param req HttpServletRequest
* @param resp HttpServletResponse
* @throws ServletException
* @throws IOException
*/
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//解决post请求中文乱码
// 解决POST请求中文乱码问题
req.setCharacterEncoding("utf-8");
//解决相应的中文乱码
// 解决响应的中文乱码问题
resp.setContentType("text/html;charset=UTF-8");
// 获取请求参数action该参数用于指定要调用的方法名
String action = req.getParameter("action");
try {
Method method = this.getClass().getDeclaredMethod(action, HttpServletRequest.class,HttpServletResponse.class);
method.invoke(this,req,resp);
// 通过反射获取当前类中指定名称的方法
Method method = this.getClass().getDeclaredMethod(action, HttpServletRequest.class, HttpServletResponse.class);
// 调用方法,传入请求和响应对象
method.invoke(this, req, resp);
} catch (Exception e) {
// 捕获异常并打印堆栈信息
e.printStackTrace();
// 将异常包装为运行时异常抛出,便于上层处理
throw new RuntimeException(e);
}
}
}
}

@ -15,72 +15,137 @@ import java.util.List;
/**
* @author yj
* @create 2020-08-24 15:25
*
* BookServletBaseServlet
* BookServiceBookServiceImpl
*/
public class BookServlet extends BaseServlet {
// 创建BookService接口的实现类BookServiceImpl对象
private BookService bookService = new BookServiceImpl();
/**
*
* @param req HttpServletRequest
* @param resp HttpServletResponse
* @throws ServletException
* @throws IOException
*/
protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
int pageNo = WebUtils.parseInt(req.getParameter("pageNo"),0);
pageNo+=1;
Book book = (Book) WebUtils.copyParamToBean(req.getParameterMap(),new Book());
// 获取当前页码默认从0开始
int pageNo = WebUtils.parseInt(req.getParameter("pageNo"), 0);
// 页码加1表示添加书籍后跳转到下一页
pageNo += 1;
// 将请求参数封装到Book对象中
Book book = (Book) WebUtils.copyParamToBean(req.getParameterMap(), new Book());
// 调用BookService的addBook方法添加书籍
bookService.addBook(book);
//req.getRequestDispatcher("/manager/bookServlet?action=list").forward(req,resp);
resp.sendRedirect(req.getContextPath() + "/manager/bookServlet?action=page&pageNo="+pageNo);
}
// 重定向到书籍管理页面,并跳转到添加书籍后的页码
resp.sendRedirect(req.getContextPath() + "/manager/bookServlet?action=page&pageNo=" + pageNo);
}
/**
*
* @param req HttpServletRequest
* @param resp HttpServletResponse
* @throws ServletException
* @throws IOException
*/
protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String id = req.getParameter("id");
int i = Integer.parseInt(id);
bookService.deleteBookById(i);
resp.sendRedirect(req.getContextPath() + "/manager/bookServlet?action=page&pageNo="+req.getParameter("pageNo"));
}
// 获取要删除的书籍ID
String id = req.getParameter("id");
int i = Integer.parseInt(id);
// 调用BookService的deleteBookById方法删除书籍
bookService.deleteBookById(i);
// 重定向到书籍管理页面,并保持当前页码
resp.sendRedirect(req.getContextPath() + "/manager/bookServlet?action=page&pageNo=" + req.getParameter("pageNo"));
}
/**
*
* @param req HttpServletRequest
* @param resp HttpServletResponse
* @throws ServletException
* @throws IOException
*/
protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Book book = (Book) WebUtils.copyParamToBean(req.getParameterMap(),new Book());
// 将请求参数封装到Book对象中
Book book = (Book) WebUtils.copyParamToBean(req.getParameterMap(), new Book());
// 调用BookService的updateBook方法更新书籍
bookService.updateBook(book);
resp.sendRedirect(req.getContextPath() + "/manager/bookServlet?action=page&pageNo="+req.getParameter("pageNo"));
// 重定向到书籍管理页面,并保持当前页码
resp.sendRedirect(req.getContextPath() + "/manager/bookServlet?action=page&pageNo=" + req.getParameter("pageNo"));
}
/**
*
* @param req
* @param resp
*
* @param req HttpServletRequest
* @param resp HttpServletResponse
* @throws ServletException
* @throws IOException
*/
protected void getBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 获取要查询的书籍ID
String id = req.getParameter("id");
int i = Integer.parseInt(id);
// 调用BookService的queryBookById方法查询书籍
Book book = bookService.queryBookById(i);
req.setAttribute("book",book);
req.getRequestDispatcher("/pages/manager/book_edit.jsp").forward(req,resp);
}
// 将查询到的书籍信息保存到request域中
req.setAttribute("book", book);
// 请求转发到书籍编辑页面
req.getRequestDispatcher("/pages/manager/book_edit.jsp").forward(req, resp);
}
/**
*
* @param req HttpServletRequest
* @param resp HttpServletResponse
* @throws ServletException
* @throws IOException
*/
protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1、通过BookService查询数据
List<Book>books = bookService.queryBooks();
//2、将数据保存在request域中
req.setAttribute("books",books);
//3、请求转发到pages/manager/book_manager.jsp
req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req,resp);
// 调用BookService的queryBooks方法查询所有书籍
List<Book> books = bookService.queryBooks();
// 将查询到的书籍列表保存到request域中
req.setAttribute("books", books);
// 请求转发到书籍管理页面
req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req, resp);
}
/**
*
* @param req HttpServletRequest
* @param resp HttpServletResponse
* @throws ServletException
* @throws IOException
*/
protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1、获取请求的参数pageNo和pageSize
int pageNo = WebUtils.parseInt(req.getParameter("pageNo"),1);
// 获取请求参数pageNo和pageSize
int pageNo = WebUtils.parseInt(req.getParameter("pageNo"), 1);
int pageSize = WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
//2、调用BookService.page(pageNo,pageSize)方法返回page对象
Page<Book> page = bookService.page(pageNo,pageSize);
// 调用BookService的page方法进行分页查询
Page<Book> page = bookService.page(pageNo, pageSize);
// 设置分页请求的URL
page.setUrl("manager/bookServlet?action=page");
//3、保存Page对象到request域中
req.setAttribute("page",page);
//4、请求转发到page/manager/book_manager.jsp页面
req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req,resp);
// 将分页对象保存到request域中
req.setAttribute("page", page);
// 请求转发到书籍管理页面
req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req, resp);
}
}
}

@ -16,18 +16,18 @@
<%@include file="/pages/common/header.jsp"%>
<script type="text/javascript">
$(function () {
//给加入购物车绑定单击事件
// 给加入购物车按钮绑定单击事件
$("button.addToCart").click(function () {
var bookId = $(this).attr("bookId");
//发ajax请求添加商品到购物车
// 发送AJAX请求将商品添加到购物车
$.getJSON("http://localhost:8080/Book/cartServlet", "action=ajaxAddItem&id=" + bookId, function (data) {
// 更新购物车商品总数
$("#cartTotalCount").text("您的购物车中有" + data.totalCount + "件商品");
// 更新购物车中最新添加的商品名称
$("#cartLastName").html("您刚刚将【" + data.lastName + "】加入到了购物车中");
});
});
});
</script>
</head>
<body>
@ -35,25 +35,27 @@
<img class="logo_img" alt="" src="static/img/logo1.jpg" >
<span class="wel_word">Bookstore</span>
<div>
<!-- 判断用户是否登录 -->
<c:if test="${empty sessionScope.user}">
<!-- 未登录状态 -->
<a href="pages/user/login.jsp">登录</a> |
<a href="pages/user/regist.jsp">注册</a> &nbsp;&nbsp;
<a href="client/bookServlet?action=pageOrder">热榜</a>
</c:if>
<c:if test="${not empty sessionScope.user}">
<!-- 已登录状态 -->
<span>欢迎<span class="um_span">${sessionScope.user.username}</span>光临书城</span>
<a href="client/orderServlet?action=myOrders">我的订单</a>
<a href="pages/user/userinfo.jsp">个人信息</a>
<a href="userServlet?action=logout">注销</a>&nbsp;&nbsp;
</c:if>
<!-- 购物车和后台管理链接 -->
<a href="pages/cart/cart.jsp">购物车</a>
<a href="pages/manager/manager.jsp">后台管理</a>
</div>
</div>
<div id="main">
<!-- 图书搜索功能 -->
<div class="book_check">
<form action="client/bookServlet" method="get">
<input type="hidden" name="action" value="pageByNameOrAuthor">
@ -62,6 +64,7 @@
</form>
</div>
<div id="book">
<!-- 价格区间筛选功能 -->
<div class="book_cond">
<form action="client/bookServlet" method="get">
<input type="hidden" name="action" value="pageByPrice">
@ -71,6 +74,7 @@
</form>
</div>
<div style="text-align: center">
<!-- 判断购物车是否为空 -->
<c:if test="${empty sessionScope.cart.items}">
<span id="cartTotalCount"></span>
<div>
@ -78,13 +82,16 @@
</div>
</c:if>
<c:if test="${not empty sessionScope.cart.items}">
<!-- 显示购物车商品总数 -->
<span id="cartTotalCount">您的购物车中有${sessionScope.cart.totalCount}件商品</span>
<div>
<!-- 显示最新加入购物车的商品名称 -->
您刚刚将<span style="color: red" id="cartLastName">${sessionScope.lastName}</span>加入到了购物车中
</div>
</c:if>
</div>
<!-- 遍历书籍列表 -->
<c:forEach items="${requestScope.page.items}" var="book">
<div class="b_list">
<div class="img_div">
@ -112,6 +119,7 @@
<span class="sp2">${book.stock}</span>
</div>
<div class="book_add">
<!-- 加入购物车按钮 -->
<button bookId=${book.id} class="addToCart">加入购物车</button>
</div>
</div>
@ -120,10 +128,12 @@
</div>
<!-- 分页导航 -->
<%@include file="/pages/common/page_nav.jsp"%>
</div>
<!-- 页脚 -->
<%@include file="/pages/common/footer.jsp"%>
</body>
</html>
</html>

@ -7,17 +7,27 @@
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String basePath = request.getScheme()
+"://"
+request.getServerName()
+":"
+request.getServerPort()
+request.getContextPath()
+"/";
pageContext.setAttribute("bastPath",basePath);
// 获取当前请求的协议如http或https
String scheme = request.getScheme();
// 获取服务器的主机名或IP地址
String serverName = request.getServerName();
// 获取服务器的端口号
int serverPort = request.getServerPort();
// 获取当前应用的上下文路径
String contextPath = request.getContextPath();
// 拼接basePath用于构建项目的根路径
String basePath = scheme + "://" + serverName + ":" + serverPort + contextPath + "/";
// 将basePath存储到pageContext中方便在页面中使用
pageContext.setAttribute("basePath", basePath);
%>
<!-- 写base标签永远固定相对路径跳转的结果 -->
<base href=<%=basePath%>>
<link type="text/css" rel="stylesheet" href="static/css/style.css" >
<script type="text/javascript" src="static/script/jquery-1.7.2.js"></script>
<base href="<%=basePath%>">
<!-- 引入外部的CSS样式文件 -->
<link type="text/css" rel="stylesheet" href="static/css/style.css">
<!-- 引入外部的jQuery库 -->
<script type="text/javascript" src="static/script/jquery-1.7.2.js"></script>

@ -8,71 +8,82 @@
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<div id="page_nav">
<!-- 首页链接 -->
<a href="${requestScope.page.url}&pageNo=1">首页</a>
<c:if test="${requestScope.page.pageNo>1}">
<a href="${requestScope.page.url}&pageNo=${requestScope.page.pageNo-1}">上一页</a>
<!-- 上一页链接 -->
<c:if test="${requestScope.page.pageNo > 1}">
<a href="${requestScope.page.url}&pageNo=${requestScope.page.pageNo - 1}">上一页</a>
</c:if>
<!-- 分页逻辑 -->
<c:choose>
<%-- 当总页码小于等于5的情况 --%>
<c:when test="${ requestScope.page.pageTotal <= 5 }">
<c:when test="${requestScope.page.pageTotal <= 5}">
<c:set var="begin" value="1"></c:set>
<c:set var="end" value="${ requestScope.page.pageTotal }"></c:set>
<c:set var="end" value="${requestScope.page.pageTotal}"></c:set>
</c:when>
<c:otherwise>
<%-- 当总页码大于5的情况 --%>
<c:choose>
<%-- 当前页码是前三 --%>
<c:when test="${ requestScope.page.pageNo <= 3 }">
<c:when test="${requestScope.page.pageNo <= 3}">
<c:set var="begin" value="1"></c:set>
<c:set var="end" value="5"></c:set>
</c:when>
<%-- 当前页码是后三 --%>
<c:when test="${ requestScope.page.pageNo >= requestScope.page.pageTotal - 2 }">
<c:set var="begin" value="${ requestScope.page.pageTotal - 4 }"></c:set>
<c:set var="end" value="${ requestScope.page.pageTotal }"></c:set>
<c:when test="${requestScope.page.pageNo >= requestScope.page.pageTotal - 2}">
<c:set var="begin" value="${requestScope.page.pageTotal - 4}"></c:set>
<c:set var="end" value="${requestScope.page.pageTotal}"></c:set>
</c:when>
<c:otherwise>
<c:set var="begin" value="${ requestScope.page.pageNo - 2 }"></c:set>
<c:set var="end" value="${ requestScope.page.pageNo + 2 }"></c:set>
<%-- 当前页码在中间 --%>
<c:set var="begin" value="${requestScope.page.pageNo - 2}"></c:set>
<c:set var="end" value="${requestScope.page.pageNo + 2}"></c:set>
</c:otherwise>
</c:choose>
</c:otherwise>
</c:choose>
<c:forEach begin="${ begin }" end="${ end }" var="i">
<c:if test="${ i == requestScope.page.pageNo }">【${ i }】</c:if>
<c:if test="${ i != requestScope.page.pageNo }"><a href="${requestScope.page.url}&pageNo=${i}">${i}</a></c:if>
<!-- 显示页码 -->
<c:forEach begin="${begin}" end="${end}" var="i">
<c:if test="${i == requestScope.page.pageNo}">【${i}】</c:if>
<c:if test="${i != requestScope.page.pageNo}">
<a href="${requestScope.page.url}&pageNo=${i}">${i}</a>
</c:if>
</c:forEach>
<c:if test="${requestScope.page.pageNo<requestScope.page.pageTotal}">
<a href="${requestScope.page.url}&pageNo=${requestScope.page.pageNo+1}">下一页</a>
<!-- 下一页链接 -->
<c:if test="${requestScope.page.pageNo < requestScope.page.pageTotal}">
<a href="${requestScope.page.url}&pageNo=${requestScope.page.pageNo + 1}">下一页</a>
</c:if>
<!-- 末页链接 -->
<a href="${requestScope.page.url}&pageNo=${requestScope.page.pageTotal}">末页</a>
<!-- 显示总页数和总记录数 -->
共${requestScope.page.pageTotal}页,${requestScope.page.pageTotalCount}条记录
<!-- 跳转页码输入框 -->
到第<input value="${param.pageNo}" name="pn" id="pn_input"/>页
<input id="searchPageBtn" type="button" value="确定">
<!-- 跳转页码的JavaScript脚本 -->
<script type="text/javascript">
$(function () {
// 点击“确定”按钮时触发跳转
$("#searchPageBtn").click(function () {
var pageNo = $("#pn_input").val()
var pageTotal = ${requestScope.page.pageTotal}
if(pageNo<1) pageNo=1
if(pageNo>pageTotal) pageNo=pageTotal
location.href="${requestScope.basePath}${requestScope.page.url}&pageNo="+pageNo;
// 获取用户输入的页码
var pageNo = $("#pn_input").val();
// 获取总页数
var pageTotal = ${requestScope.page.pageTotal};
// 校验页码范围
if (pageNo < 1) pageNo = 1;
if (pageNo > pageTotal) pageNo = pageTotal;
// 跳转到指定页码
location.href = "${requestScope.basePath}${requestScope.page.url}&pageNo=" + pageNo;
});
});
</script>

@ -2,59 +2,74 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>编辑图书</title>
<meta charset="UTF-8">
<title>编辑图书</title>
<%@include file="/pages/common/header.jsp"%>
<style type="text/css">
h1 {
text-align: center;
margin-top: 200px;
}
h1 a {
color:red;
}
input {
text-align: center;
}
</style>
<style type="text/css">
h1 {
text-align: center;
margin-top: 200px;
}
h1 a {
color:red;
}
input {
text-align: center;
}
</style>
</head>
<body>
<div id="header">
<img class="logo_img" alt="" src="static/img/logo1.jpg" >
<span class="wel_word">编辑图书</span>
<%@include file="/pages/common/manager_menu.jsp"%>
</div>
<div id="main">
<form action="manager/bookServlet" method="post">
<input type="hidden" name="pageNo" value="${param.pageNo}">
<input type="hidden" name="action" value="${empty param.id ? "add":"update"}"/>
<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 name="name" type="text" value="${requestScope.book.name}"/></td>
<td><input name="price" type="text" value="${requestScope.book.price}"/></td>
<td><input name="author" type="text" value="${requestScope.book.author}"/></td>
<td><input name="sales" type="text" value="${requestScope.book.sales}"/></td>
<td><input name="stock" type="text" value="${requestScope.book.stock}"/></td>
<td><input type="submit" value="提交"/></td>
</tr>
</table>
</form>
</div>
<%@include file="/pages/common/footer.jsp"%>
<div id="header">
<img class="logo_img" alt="" src="static/img/logo1.jpg" >
<span class="wel_word">编辑图书</span>
<%@include file="/pages/common/manager_menu.jsp"%>
</div>
<div id="main">
<form action="manager/bookServlet" method="post">
<!-- 隐藏域,用于传递当前页码 -->
<input type="hidden" name="pageNo" value="${param.pageNo}">
<!-- 隐藏域,用于判断是添加还是更新操作 -->
<input type="hidden" name="action" value="${empty param.id ? "add":"update"}"/>
<!-- 隐藏域用于传递图书ID -->
<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 name="name" type="text" value="${requestScope.book.name}"/></td>
<!-- 图书价格输入框 -->
<td><input name="price" type="text" value="${requestScope.book.price}"/></td>
<!-- 图书作者输入框 -->
<td><input name="author" type="text" value="${requestScope.book.author}"/></td>
<!-- 图书销量输入框 -->
<td><input name="sales" type="text" value="${requestScope.book.sales}"/></td>
<!-- 图书库存输入框 -->
<td><input name="stock" type="text" value="${requestScope.book.stock}"/></td>
<!-- 提交按钮 -->
<td><input type="submit" value="提交"/></td>
</tr>
</table>
</form>
</div>
<%@include file="/pages/common/footer.jsp"%>
</body>
</html>

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save