|
|
|
@ -15,72 +15,137 @@ import java.util.List;
|
|
|
|
|
/**
|
|
|
|
|
* @author yj
|
|
|
|
|
* @create 2020-08-24 15:25
|
|
|
|
|
*
|
|
|
|
|
* BookServlet类继承自BaseServlet,用于处理与书籍相关的请求。
|
|
|
|
|
* 通过调用BookService接口的实现类BookServiceImpl,完成书籍的增删改查、分页查询等操作。
|
|
|
|
|
*/
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|