/** * Copyright (C), 2015-2018, XXX有限公司 * FileName: BookController * Author: 沉梦昂志丶 * Date: 2018/7/11 22:44 * Description: * History: * <author> <time> <version> <desc> * 作者姓名 修改时间 版本号 描述 */ package com.test.controller; import com.alibaba.fastjson.JSONObject; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.test.po.BookInfo; import com.test.po.BookType; import com.test.service.BookInfoService; import com.test.service.BookTypeService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.transaction.annotation.Transactional; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.SessionAttributes; import org.springframework.web.context.ServletContextAware; import org.springframework.web.multipart.commons.CommonsMultipartFile; import org.springframework.web.servlet.ModelAndView; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.util.List; /** * 〈一句话功能简述〉<br> * 〈〉 * * @author 沉梦昂志丶 * @create 2018/7/11 * @since 1.0.0 */ @Controller @Transactional @SessionAttributes({"bookTypes"}) public class BookController implements ServletContextAware { @Autowired private BookTypeService bookTypeService; @Autowired private BookInfoService bookInfoService; private ServletContext servletContext; @RequestMapping("/index.do") public String goadd(ModelMap map){ System.out.println("进入跳转到add.jsp页面..."); List<BookType> bookTypes = bookTypeService.selectAll(); map.put("bookTypes",bookTypes); return "add"; } @Override public void setServletContext(ServletContext arg0) { this.servletContext=arg0; } @RequestMapping("imageupload.htm") @ResponseBody public String imageupload(@RequestParam("fileImage") CommonsMultipartFile file){ String path=servletContext.getRealPath("/resource/upload/"); System.out.println("上传的路径为:" + path); String name=file.getOriginalFilename(); File file1=new File(path,name); try { file.getFileItem().write(file1); } catch (Exception e) { e.printStackTrace(); } String imagePath = "upload/" + name; JSONObject jsonObject=new JSONObject(); jsonObject.put("imagePath",imagePath); String json=jsonObject.toJSONString(); System.out.println("json"+json); return json; } @RequestMapping("/addbook.htm") public void addBook(BookInfo bookInfo, HttpServletResponse response) throws IOException { bookInfo.setIsBorrow(0); int insert = bookInfoService.insertSelective(bookInfo); response.setContentType("text/html; charset=UTF-8"); PrintWriter printWriter=response.getWriter(); if(insert>0){ printWriter.write("<script type='text/javascript'>alert('新增成功');location.href='/querybook.htm'</script>"); } else { printWriter.write("<script type='text/javascript'>alert('新增失败');location.href='/login.do'</script>"); } } @RequestMapping("/querybook.htm") public String queryBook(@RequestParam(value="bookTypeid",required=false,defaultValue="0")Integer bookTypeid, String bookname, @RequestParam(value="borrow",required=false,defaultValue="-1") Integer borrow, @RequestParam(value = "now", required = false, defaultValue = "1")Integer now, ModelMap map) { System.out.println("进入....."+bookname); PageHelper.startPage(now,10); List<BookInfo> books=bookInfoService.selectAll(bookTypeid, bookname, borrow); for (BookInfo bookInfo:books){ System.out.println(bookInfo); } PageInfo<BookInfo> pageInfo=new PageInfo<BookInfo>(books); map.put("pageInfo",pageInfo); List<BookType> bookTypes=bookTypeService.selectAll(); map.put("bookTypes",bookTypes); map.put("bookTypeid", bookTypeid); map.put("bookname", bookname); map.put("borrow", borrow); return "listbook"; } @RequestMapping("/detail.htm") public ModelAndView detail(Integer id){ BookInfo bookInfo=bookInfoService.selectByPrimaryKey(id); ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("detail"); modelAndView.addObject("book", bookInfo); return modelAndView; } @RequestMapping("/delect.do") private void delete(Integer id, HttpServletResponse response) throws IOException { int i = bookInfoService.deleteByPrimaryKey(id); response.setContentType("text/html; charset=UTF-8"); PrintWriter printWriter=response.getWriter(); if(i>0){ printWriter.write("<script type='text/javascript'>alert('删除成功');location.href='/querybook.htm'</script>"); } else { printWriter.write("<script type='text/javascript'>alert('删除失败');location.href='/querybook.htm'</script>"); } } @RequestMapping("/queryEdit.htm") public ModelAndView queryEdit(Integer id) { BookInfo bookInfo=bookInfoService.selectByPrimaryKey(id); ModelAndView modelAndView=new ModelAndView(); modelAndView.setViewName("update"); modelAndView.addObject("book",bookInfo); return modelAndView; } @RequestMapping("/updateBook.htm") public String update(BookInfo bookInfo,ModelMap map){ bookInfoService.updateByPrimaryKey(bookInfo); return "querybook.htm"; } @RequestMapping("/deleteids.htm") public String deleteids(String[] bookid){ bookInfoService.deleteids(bookid); return "forward:querybook.htm"; } }