作业

/**
 * 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);
        System.out.println(books);
        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>");
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值