首页>代码>servlet+jdbc实现的java web图书馆借阅管理平台>/lib-mgr/src/com/dreambamboo/controller/BookServlet.java
package com.dreambamboo.controller;

import com.dreambamboo.entities.Book;
import com.dreambamboo.entities.BookCase;
import com.dreambamboo.service.BookAdminService;
import com.dreambamboo.service.BookService;
import com.dreambamboo.service.impl.BookAdminServiceImpl;
import com.dreambamboo.service.impl.BookServiceImpl;
import com.dreambamboo.vo.BookVO;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @ClassName:BookServlet
 * @Description:图书控制层
 * @Author:dreambamboo
 * @Date:2019/1/6 22:23
 * @version:1.0
 */
public class BookServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        BookService bookService = new BookServiceImpl();
        BookAdminService bookAdminService = new BookAdminServiceImpl();
        Book book;
        String bookIdStr;
        List<BookCase> bookCaseList;
        String name;
        String author;
        String publish;
        String pageStr;
        String priceStr;
        String bookCaseId;
        BookCase bookCase;
        //查询图书
        if (method == null){
            resp.setCharacterEncoding("utf-8");
            pageStr = req.getParameter("page");
            String limitStr = req.getParameter("limit");
            List<Book> bookList = bookService.findAllBooks(Integer.valueOf(pageStr),Integer.valueOf(limitStr));
            BookVO bookVO = new BookVO();
            bookVO.setCode(0);
            bookVO.setMsg(null);
            bookVO.setCount(bookService.findBookCount());
            bookVO.setData(bookList);
            JSONObject jsonObject = JSONObject.fromObject(bookVO);
            resp.getWriter().write(jsonObject.toString());
        } else {
            switch (method) {
                //编辑图书
                case "preEdit":
                    bookIdStr = req.getParameter("bookId");
                    book = bookService.findBookById(Integer.valueOf(bookIdStr));
                    req.setAttribute("book",book);
                    bookCaseList = bookAdminService.findAllBookCase();
                    req.setAttribute("bookCaseList",bookCaseList);
                    req.getRequestDispatcher("/book/book-edit.jsp").forward(req,resp);
                    break;
                case "edit":
                    bookIdStr = req.getParameter("id");
                    name = req.getParameter("name");
                    author = req.getParameter("author");
                    publish = req.getParameter("publish");
                    pageStr = req.getParameter("pages");
                    priceStr = req.getParameter("price");
                    bookCaseId = req.getParameter("bookCaseId");
                    bookCase = bookAdminService.findBookCaseById(Integer.valueOf(bookCaseId));
                    book = new Book();
                    book.setId(Integer.valueOf(bookIdStr));
                    book.setName(name);
                    book.setAuthor(author);
                    book.setPublish(publish);
                    book.setPages(Integer.valueOf(pageStr));
                    book.setPrice(Float.valueOf(priceStr));
                    book.setBookCase(bookCase);
                    book.setAbled(1);
                    bookService.modifyBook(book);
                    resp.sendRedirect("/book/book-list.jsp");
                    break;
                //添加图书
                case "preAdd":
                    bookCaseList = bookAdminService.findAllBookCase();
                    req.setAttribute("list",bookCaseList);
                    req.getRequestDispatcher("/book/book-add.jsp").forward(req,resp);
                    break;
                case "add":
                    name = req.getParameter("name");
                    author = req.getParameter("author");
                    publish = req.getParameter("publish");
                    pageStr = req.getParameter("pages");
                    priceStr = req.getParameter("price");
                    bookCaseId = req.getParameter("bookCaseId");
                    bookCase = bookAdminService.findBookCaseById(Integer.parseInt(bookCaseId));
                    book = new Book(name,Integer.parseInt(pageStr),Float.parseFloat(priceStr),publish,author,bookCase,1);
                    bookService.addBook(book);
                    resp.sendRedirect("/book/book-list.jsp");
                    break;
                //删除图书
                case "delete":
                    bookIdStr = req.getParameter("bookId");
                    book = bookService.findBookById(Integer.parseInt(bookIdStr));
                    book.setBookCase(null);
                    bookService.deleteBookById(Integer.parseInt(bookIdStr));
                    resp.sendRedirect("/book/book-list.jsp");
                    break;

            }
        }
    }
}
最近下载更多
duoduo1234  LV2 1月15日
人大叔大婶左权  LV2 2023年6月1日
北方菜  LV11 2023年3月21日
ewan007  LV30 2023年2月8日
rain112  LV31 2023年1月3日
2521436308  LV10 2022年11月25日
最代码-宋家辉  LV61 2022年11月20日
Weishenghui  LV7 2022年10月24日
liul101112  LV2 2022年6月30日
testuser1234567  LV24 2022年6月30日
最近浏览更多
krispeng  LV13 8月26日
暂无贡献等级
gujiuzzy 5月25日
暂无贡献等级
玖零定制问题修复  LV34 4月4日
XJH939  LV1 3月28日
duoduo1234  LV2 1月15日
asddwh  LV13 2023年12月26日
颜菜菜  LV2 2023年12月23日
xiaoqian555 2023年12月20日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友