@@ -19,7 +19,7 @@ public class BookServlet extends BaseServlet{
1919
2020 protected void add (HttpServletRequest req , HttpServletResponse resp ) throws ServletException , IOException {
2121 //1.获取请求参数 , 封装成 Book 对象
22- Book book = WebUtils .copyParamTOBean (req .getParameterMap (), new Book ());
22+ Book book = WebUtils .copyParamTOBean (req .getParameterMap (), new Book ());//调用WebUtils工具类
2323 //2.调用BookServlet.add()方法 , 保存图书
2424 bookService .addBook (book );
2525 //3.请求重定向 到 图书列表 页面 /manager/bookServlet?action=list
@@ -28,19 +28,43 @@ protected void add(HttpServletRequest req, HttpServletResponse resp) throws Serv
2828 }
2929
3030 protected void delete (HttpServletRequest req , HttpServletResponse resp ) throws ServletException , IOException {
31+ //1.获取请求参数id,图书编号
32+ int id = WebUtils .parseInt (req .getParameter ("id" ), 0 );
33+ //2.调用bookService.deleteBookById(); 删除图书
34+ bookService .deleteBookById (id );
35+ //3.重定向 回图书管理页面
36+ // /manager/bookServlet?action=list
37+ resp .sendRedirect (req .getContextPath ()+ "/manager/bookServlet?action=list" );
3138
3239 }
3340
34- protected void update (HttpServletRequest req , HttpServletResponse resp ) throws ServletException , IOException {
3541
42+ protected void getBook (HttpServletRequest req , HttpServletResponse resp ) throws ServletException , IOException {
43+ //1.获取请求参数 id = 图书编号
44+ int id = WebUtils .parseInt (req .getParameter ("id" ), 0 );
45+ //2.调用 bookService.queryBookById() 查询图书
46+ Book book = bookService .queryBookById (id );
47+ //3.保存图书到Request域中
48+ req .setAttribute ("book" ,book );
49+ //4.请求转发到 /pages/manager/book_edit.jsp
50+ req .getRequestDispatcher ("/pages/manager/book_edit.jsp" ).forward (req ,resp );
51+ }
52+
53+ protected void update (HttpServletRequest req , HttpServletResponse resp ) throws ServletException , IOException {
54+ //1.获取请求参数 , 封装成Book对象
55+ Book book = WebUtils .copyParamTOBean (req .getParameterMap (), new Book ());
56+ //2.调用bookService.updateBook(),将修改好的数据保存到数据库中
57+ bookService .updateBook (book );
58+ //3.重定向回 图书管理页面 list那个
59+ resp .sendRedirect (req .getContextPath ()+"/manager/bookServlet?action=list" );
3660 }
3761
3862 protected void list (HttpServletRequest req , HttpServletResponse resp ) throws ServletException , IOException {
3963 //1.通过BookService 查询全部图书
4064 List <Book > books = bookService .queryBooks ();
4165 //2.把全部图书 保存到Request域
4266 req .setAttribute ("books" ,books );
43- //3.请求转发到 /pages/manager/book_manager .jsp
67+ //3.请求转发到 /pages/manager/book_edit .jsp
4468 req .getRequestDispatcher ("/pages/manager/book_manager.jsp" ).forward (req ,resp );
4569 }
4670}
0 commit comments