コード例 #1
0
  /**
   * @param book
   * @return
   * @throws IllegalStateException
   * @throws IOException
   * @throws JsonMappingException
   * @throws JsonGenerationException
   */
  @RequestMapping(value = "/Book/modifyBook", method = RequestMethod.POST)
  public String modifyBook2(
      @RequestParam("bookid") Integer bookid,
      @RequestParam("bookname") String bookname,
      @RequestParam("author") String author,
      @RequestParam("publish") String publish,
      @RequestParam("isbn") String isbn,
      @RequestParam("publishdate") String publishdate,
      @RequestParam("picturename") String picturename,
      @RequestParam("repertory") String repertory,
      @RequestParam("sales") Integer sales,
      @RequestParam("price") Double price,
      @RequestParam("stock") Integer stock,
      @RequestParam(value = "bookclassid", required = false) Integer bookclassid,
      @RequestParam(value = "isrecommend", required = false) String isrecommend,
      @RequestParam("intro") String intro,
      @RequestParam("picture") MultipartFile file,
      Model model)
      throws IllegalStateException, IOException {
    // 修改图片
    logger.info("POST---修改图书---");
    // 对图片进行判断,判断是否新上传了图片
    String newFileName = "";
    if (file.getOriginalFilename() != null) {
      String fileName = file.getOriginalFilename();
      logger.info("原文件名" + fileName);
      String path = "O:\\picc\\";

      if (file != null && fileName != null && fileName.length() > 0) {
        newFileName = UUID.randomUUID() + fileName.substring(fileName.lastIndexOf("."));
        File newFile = new File(path + newFileName);
        file.transferTo(newFile);

        logger.info("上传成功");
      }
    }
    // 将原本的图片名称再次赋值给新的图书
    else {
      newFileName = picturename;
    }

    Book book = new Book();
    book.setBookid(bookid);
    book.setAuthor(author);
    book.setBookclassid(bookclassid);
    book.setBookname(bookname);
    book.setIntro(intro);
    book.setIsbn(isbn);

    if (isrecommend == null) {
      isrecommend = "0";
    }
    logger.info(isrecommend);
    book.setPublishdate(publishdate);
    book.setIsrecommend(isrecommend);
    book.setPicture(newFileName);
    book.setPrice(price);
    book.setPublish(publish);
    book.setRepertory(repertory);
    book.setSales(sales);
    book.setStock(stock);
    ObjectMapper mapper = new ObjectMapper();
    System.out.println("输出修改后的book");
    logger.info(mapper.writeValueAsString(book));

    int i = bookService.updateBook(book);
    System.out.println(i);
    return "redirect:/bookPage";
  }
コード例 #2
0
  @RequestMapping(value = "/addBook", method = RequestMethod.POST)
  public String addBook2(
      @RequestParam("bookname") String bookname,
      @RequestParam("author") String author,
      @RequestParam("publish") String publish,
      @RequestParam("isbn") String isbn,
      @RequestParam("publishdate") String publishdate,
      @RequestParam("repertory") String repertory,
      @RequestParam("price") Double price,
      @RequestParam("stock") Integer stock,
      @RequestParam("bookOneclassid") Integer bookOneclassid,
      @RequestParam(value = "bookTwoclassid", required = false) Integer bookTwoclassid,
      @RequestParam(value = "bookThreeclassid", required = false) Integer bookThreeclassid,
      @RequestParam(value = "isrecommend", required = false) String isrecommend,
      @RequestParam("intro") String intro,
      @RequestParam("picture") MultipartFile file,
      Model model)
      throws JsonGenerationException, JsonMappingException, IOException {
    // 上传图片
    logger.info("POST---添加图书---");

    String fileName = file.getOriginalFilename();
    logger.info("原文件名" + fileName);
    String path = "O:\\picc\\";
    String newFileName = "";
    if (file != null && fileName != null && fileName.length() > 0) {
      newFileName = UUID.randomUUID() + fileName.substring(fileName.lastIndexOf("."));
      File newFile = new File(path + newFileName);
      file.transferTo(newFile);

      logger.info("上传成功");
    }

    Book book = new Book();
    book.setAuthor(author);
    Integer bookclassid = null;
    System.out.println("bookThreeclassid===>" + bookThreeclassid);
    System.out.println("bookTwoclassid===>" + bookTwoclassid);
    System.out.println("bookOneclassid===>" + bookOneclassid);
    if (bookThreeclassid > new Integer(-1)) {
      bookclassid = bookThreeclassid;
    } else if (bookTwoclassid > new Integer(-1)) {
      bookclassid = bookTwoclassid;

    } else {
      bookclassid = bookOneclassid;
    }
    System.out.println("图书价格:" + price);

    Double tsprice = Double.parseDouble(String.format("%.2f", price));
    System.out.println(tsprice);

    System.out.println(bookclassid);
    book.setBookclassid(bookclassid);
    book.setBookname(bookname);
    book.setIntro(intro);
    book.setIsbn(isbn);

    if (isrecommend == null) {
      isrecommend = "0";
    }
    logger.info(isrecommend);
    book.setPublishdate(publishdate);
    book.setIsrecommend(isrecommend);
    book.setPicture(newFileName);
    book.setPrice(tsprice);
    book.setPublish(publish);
    book.setRepertory(repertory);
    book.setSales(0);
    book.setStock(stock);
    ObjectMapper mapper = new ObjectMapper();
    logger.info(mapper.writeValueAsString(book));
    System.out.println(book);
    bookService.addBook(book);
    return "redirect:/bookPage";
  }