package com.bysj.controller; import java.io.IOException; import java.io.OutputStream; import java.beans.PropertyEditorSupport; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.propertyeditors.CustomDateEditor; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.InitBinder; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import com.bysj.po.Book; import com.bysj.po.BookType; import com.bysj.service.BookService; import com.bysj.service.BookTypeService; import com.bysj.utils.ExportExcelUtil; import com.bysj.utils.UserException; //图书控制层 @Controller @RequestMapping("/Book") public class BookController extends BaseController { //注入业务层对象 @Resource BookService bookService; @Resource BookTypeService bookTypeService; @InitBinder("bookType") public void initBinderBookType(WebDataBinder binder) { binder.setFieldDefaultPrefix("bookType."); } @InitBinder // 必须有一个参数WebDataBinder public void initBinder(WebDataBinder binder) { //System.out.println(binder.getFieldDefaultPrefix()); binder.registerCustomEditor(Date.class, new CustomDateEditor( new SimpleDateFormat("yyyy-MM-dd"), false)); binder.registerCustomEditor(Integer.class, new PropertyEditorSupport() { @Override public String getAsText() { return getValue() == null ? "" : getValue().toString(); } @Override public void setAsText(String text) throws IllegalArgumentException { Integer value = 0; if (null != text && !text.equals("")) { try { value = Integer.valueOf(text); } catch(Exception ex) { throw new UserException("数据格式输入不正确!"); } } setValue(value); } }); } @RequestMapping(value = "/add", method = RequestMethod.GET) public String add(Model model,HttpServletRequest request) throws Exception { model.addAttribute(new Book()); /*查询所有的BookType信息*/ List<BookType> bookTypeList = bookTypeService.queryAllBookTypeInfo(); request.setAttribute("bookTypeList", bookTypeList); return "Book_add"; } @RequestMapping(value = "/add", method = RequestMethod.POST) public String add(@Validated Book book, BindingResult br, Model model, HttpServletRequest request) throws Exception { if (br.hasErrors()) { model.addAttribute(book); /*查询所有的BookType信息*/ List<BookType> bookTypeList = bookTypeService.queryAllBookTypeInfo(); request.setAttribute("bookTypeList", bookTypeList); return "Book_add"; } try { book.setPhotoBook(this.handlePhotoFileUpload(request, "photoBookFile")); bookService.addBook(book); request.setAttribute("message", java.net.URLEncoder.encode("图书添加成功!", "GBK")); return "message"; } catch (Exception e) { e.printStackTrace(); request.setAttribute("error", java.net.URLEncoder .encode("图书添加失败!")); return "error"; } } /*查询图书*/ @RequestMapping(value = { "/list" }, method = {RequestMethod.GET,RequestMethod.POST}) public String list(String bookName,@ModelAttribute BookType bookType,String publishDate,String barcode,Integer currentPage, Model model, HttpServletRequest request) throws Exception { if (currentPage==null || currentPage == 0) currentPage = 1; if(bookName == null) bookName = ""; if(publishDate == null) publishDate = ""; if(barcode == null) barcode = ""; List<Book> bookList = bookService.queryBookInfo( bookName, bookType, publishDate, barcode,currentPage); /*计算总的页数和总的记录数*/ bookService.queryTotalPageAndRecordNumber( bookName, bookType, publishDate, barcode); /*获取到总的页码数目*/ int totalPage = bookService.getTotalPage(); /*当前查询条件下总记录数*/ int recordNumber = bookService.getRecordNumber(); request.setAttribute("bookList", bookList); request.setAttribute("totalPage", totalPage); request.setAttribute("recordNumber", recordNumber); request.setAttribute("currentPage", currentPage); request.setAttribute("bookName", bookName); request.setAttribute("bookType", bookType); List<BookType> bookTypeList = bookTypeService.queryAllBookTypeInfo(); request.setAttribute("bookTypeList", bookTypeList); request.setAttribute("publishDate", publishDate); request.setAttribute("barcode", barcode); return "Book_query_result"; } /*前台查询图书*/ @RequestMapping(value = { "/frontlist" }, method = {RequestMethod.GET,RequestMethod.POST}) public String frontlist(String bookName,@ModelAttribute BookType bookType,String publishDate,String barcode,Integer currentPage, Model model, HttpServletRequest request) throws Exception { if (currentPage==null || currentPage == 0) currentPage = 1; if(bookName == null) bookName = ""; if(publishDate == null) publishDate = ""; if(barcode == null) barcode = ""; List<Book> bookList = bookService.queryBookInfo( bookName, bookType, publishDate, barcode,currentPage); /*计算总的页数和总的记录数*/ bookService.queryTotalPageAndRecordNumber( bookName, bookType, publishDate, barcode); /*获取到总的页码数目*/ int totalPage = bookService.getTotalPage(); /*当前查询条件下总记录数*/ int recordNumber = bookService.getRecordNumber(); request.setAttribute("bookList", bookList); request.setAttribute("totalPage", totalPage); request.setAttribute("recordNumber", recordNumber); request.setAttribute("currentPage", currentPage); request.setAttribute("bookName", bookName); request.setAttribute("bookType", bookType); List<BookType> bookTypeList = bookTypeService.queryAllBookTypeInfo(); request.setAttribute("bookTypeList", bookTypeList); request.setAttribute("publishDate", publishDate); request.setAttribute("barcode", barcode); return "Book_frontquery_result"; } /*根据主键barcode获取Book对象*/ @RequestMapping(value="/{barcode}/update",method=RequestMethod.GET) public String update(@PathVariable String barcode,Model model,HttpServletRequest request) throws Exception { /*根据主键barcode获取Book对象*/ Book book = bookService.getBookByBarcode(barcode); List<BookType> bookTypeList = bookTypeService.queryAllBookTypeInfo(); request.setAttribute("bookTypeList", bookTypeList); request.setAttribute("book", book); return "Book_modify"; } /*根据主键barcode获取Book对象*/ @RequestMapping(value="/{barcode}/frontshow",method=RequestMethod.GET) public String frontshow(@PathVariable String barcode,Model model,HttpServletRequest request) throws Exception { /*根据主键barcode获取Book对象*/ Book book; try { book = bookService.getBookByBarcode(barcode); List<BookType> bookTypeList = bookTypeService.queryAllBookTypeInfo(); request.setAttribute("bookTypeList", bookTypeList); request.setAttribute("book", book); } catch (Exception e) { e.printStackTrace(); } return "Book_frontshow"; } /*更新修改图书信息*/ @RequestMapping(value = "/{barcode}/update", method = RequestMethod.POST) public String update(@Validated Book book, BindingResult br, Model model, HttpServletRequest request) throws Exception { if (br.hasErrors()) { model.addAttribute(book); /*查询所有的BookType信息*/ List<BookType> bookTypeList = bookTypeService.queryAllBookTypeInfo(); request.setAttribute("bookTypeList", bookTypeList); return "Book_modify"; } String photoBookFileName = this.handlePhotoFileUpload(request, "photoBookFile"); if(!photoBookFileName.equals("upload/NoImage.jpg"))book.setPhotoBook(photoBookFileName); try { BookType bookType = bookTypeService.getBookTypeByBookTypeId(book.getBookType().getBookTypeId()); book.setBookType(bookType); bookService.updateBook(book); request.setAttribute("message", java.net.URLEncoder.encode( "图书更新成功!", "GBK")); return "message"; } catch (Exception e) { e.printStackTrace(); request.setAttribute("error", java.net.URLEncoder.encode("图书更新失败!","GBK")); return "error"; } } /*删除图书信息*/ @RequestMapping(value="/{barcode}/delete",method=RequestMethod.GET) public String delete(@PathVariable String barcode,HttpServletRequest request) throws UnsupportedEncodingException { try { bookService.deleteBook(barcode); request.setAttribute("message", java.net.URLEncoder.encode("Book删除成功!", "GBK")); return "message"; } catch (Exception e) { e.printStackTrace(); request.setAttribute("error", java.net.URLEncoder.encode("Book删除失败!", "GBK")); return "error"; } } /*后台导出到excel*/ @RequestMapping(value = { "/OutToExcel" }, method = {RequestMethod.GET,RequestMethod.POST}) public String OutToExcel(String bookName,@ModelAttribute BookType bookType,String publishDate,String barcode,Model model,HttpServletRequest request,HttpServletResponse response) throws Exception { if(bookName == null) bookName = ""; if(publishDate == null) publishDate = ""; if(barcode == null) barcode = ""; List<Book> bookList = bookService.queryBookInfo( bookName, bookType, publishDate, barcode); ExportExcelUtil ex = new ExportExcelUtil(); String title = "Book信息记录"; String[] headers = {"图书名称","图书所在类别","图书价格","库存","出版社","出版日期","图书条形码","图书图片"}; List<String[]> dataset = new ArrayList<String[]>(); for(int i=0;i<bookList.size();i++) { Book book = bookList.get(i); dataset.add(new String[]{book.getBookName(),book.getBookType().getBookTypeName(),book.getPrice() + "",book.getCount() + "",book.getPublish(),book.getPublishDate(),book.getBarcode(),book.getPhotoBook()}); } /* OutputStream out = null; try { out = new FileOutputStream("C://output.xls"); ex.exportExcel(title,headers, dataset, out); out.close(); } catch (Exception e) { e.printStackTrace(); } */ OutputStream out = null;//创建一个输出流对象 try { out = response.getOutputStream();// response.setHeader("Content-disposition","attachment; filename="+"Book.xls");//filename是下载的xls的名,建议最好用英文 response.setContentType("application/msexcel;charset=UTF-8");//设置类型 response.setHeader("Pragma","No-cache");//设置头 response.setHeader("Cache-Control","no-cache");//设置头 response.setDateHeader("Expires", 0);//设置日期头 String rootPath = request.getSession().getServletContext().getRealPath("/"); ex.exportExcel(rootPath,title,headers, dataset, out); out.flush(); } catch (IOException e) { e.printStackTrace(); }finally{ try{ if(out!=null){ out.close(); } }catch(IOException e){ e.printStackTrace(); } } return null; } }

星予宝藏666 LV6
2023年6月7日
hwz050059 LV1
2023年5月30日
zcx12345678 LV6
2023年5月29日
不足挂齿 LV2
2023年2月1日
and123456 LV11
2022年7月15日
stdtta LV8
2022年6月28日
huyyyyy LV8
2021年12月24日
1727779658 LV7
2021年12月20日
1178995129 LV6
2021年12月12日
wanglinddad LV55
2021年12月7日