|
| 1 | +package com.atguigu.web; |
| 2 | + |
| 3 | +import com.atguigu.pojo.Book; |
| 4 | +import com.atguigu.service.BookService; |
| 5 | +import com.atguigu.service.impl.BookServiceImpl; |
| 6 | +import com.atguigu.utils.WebUtils; |
| 7 | + |
| 8 | +import javax.servlet.ServletException; |
| 9 | +import javax.servlet.http.HttpServletRequest; |
| 10 | +import javax.servlet.http.HttpServletResponse; |
| 11 | +import java.io.IOException; |
| 12 | +import java.util.List; |
| 13 | + |
| 14 | +public class BookServlet extends BaseServlet{ |
| 15 | + |
| 16 | + //还是 接口的引用执行 实现了该接口的类的对象 |
| 17 | + //要和数据库交互,需要访问Dao层,但web层不能直接访问dao层,需要先通过访问service层来访问dao层 |
| 18 | + private BookService bookService=new BookServiceImpl(); |
| 19 | + |
| 20 | + protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
| 21 | + //1.获取请求参数 , 封装成 Book 对象 |
| 22 | + Book book = WebUtils.copyParamTOBean(req.getParameterMap(), new Book()); |
| 23 | + //2.调用BookServlet.add()方法 , 保存图书 |
| 24 | + bookService.addBook(book); |
| 25 | + //3.请求重定向 到 图书列表 页面 /manager/bookServlet?action=list |
| 26 | + resp.sendRedirect(req.getContextPath()+"/manager/bookServlet?action=list"); |
| 27 | + |
| 28 | + } |
| 29 | + |
| 30 | + protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
| 31 | + |
| 32 | + } |
| 33 | + |
| 34 | + protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
| 35 | + |
| 36 | + } |
| 37 | + |
| 38 | + protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
| 39 | + //1.通过BookService 查询全部图书 |
| 40 | + List<Book> books = bookService.queryBooks(); |
| 41 | + //2.把全部图书 保存到Request域 |
| 42 | + req.setAttribute("books",books); |
| 43 | + //3.请求转发到 /pages/manager/book_manager.jsp |
| 44 | + req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req,resp); |
| 45 | + } |
| 46 | +} |
0 commit comments