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.BookType; import com.bysj.service.BookTypeService; import com.bysj.utils.ExportExcelUtil; import com.bysj.utils.UserException; //图书类型控制层 @Controller @RequestMapping("/BookType") public class BookTypeController extends BaseController { //注入业务层对象 @Resource BookTypeService bookTypeService; @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 BookType()); return "BookType_add"; } @RequestMapping(value = "/add", method = RequestMethod.POST) public String add(@Validated BookType bookType, BindingResult br, Model model, HttpServletRequest request) throws Exception { if (br.hasErrors()) { model.addAttribute(bookType); return "BookType_add"; } try { bookTypeService.addBookType(bookType); 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(Integer currentPage, Model model, HttpServletRequest request) throws Exception { if (currentPage==null || currentPage == 0) currentPage = 1; List<BookType> bookTypeList = bookTypeService.queryBookTypeInfo(currentPage); /*计算总的页数和总的记录数*/ bookTypeService.queryTotalPageAndRecordNumber(); /*获取到总的页码数目*/ int totalPage = bookTypeService.getTotalPage(); /*当前查询条件下总记录数*/ int recordNumber = bookTypeService.getRecordNumber(); request.setAttribute("bookTypeList", bookTypeList); request.setAttribute("totalPage", totalPage); request.setAttribute("recordNumber", recordNumber); request.setAttribute("currentPage", currentPage); return "BookType_query_result"; } /*前台查询图书类型*/ @RequestMapping(value = { "/frontlist" }, method = {RequestMethod.GET,RequestMethod.POST}) public String frontlist(Integer currentPage, Model model, HttpServletRequest request) throws Exception { if (currentPage==null || currentPage == 0) currentPage = 1; List<BookType> bookTypeList = bookTypeService.queryBookTypeInfo(currentPage); /*计算总的页数和总的记录数*/ bookTypeService.queryTotalPageAndRecordNumber(); /*获取到总的页码数目*/ int totalPage = bookTypeService.getTotalPage(); /*当前查询条件下总记录数*/ int recordNumber = bookTypeService.getRecordNumber(); request.setAttribute("bookTypeList", bookTypeList); request.setAttribute("totalPage", totalPage); request.setAttribute("recordNumber", recordNumber); request.setAttribute("currentPage", currentPage); return "BookType_frontquery_result"; } /*根据主键bookTypeId获取BookType对象*/ @RequestMapping(value="/{bookTypeId}/update",method=RequestMethod.GET) public String update(@PathVariable int bookTypeId,Model model,HttpServletRequest request) throws Exception { /*根据主键bookTypeId获取BookType对象*/ BookType bookType = bookTypeService.getBookTypeByBookTypeId(bookTypeId); request.setAttribute("bookType", bookType); return "BookType_modify"; } /*根据主键bookTypeId获取BookType对象*/ @RequestMapping(value="/{bookTypeId}/frontshow",method=RequestMethod.GET) public String frontshow(@PathVariable int bookTypeId,Model model,HttpServletRequest request) { /*根据主键bookTypeId获取BookType对象*/ BookType bookType; try { bookType = bookTypeService.getBookTypeByBookTypeId(bookTypeId); request.setAttribute("bookType", bookType); } catch (Exception e) { e.printStackTrace(); } return "BookType_frontshow"; } /*更新修改图书类型信息*/ @RequestMapping(value = "/{bookTypeId}/update", method = RequestMethod.POST) public String update(@Validated BookType bookType, BindingResult br, Model model, HttpServletRequest request) throws Exception { if (br.hasErrors()) { model.addAttribute(bookType); return "BookType_modify"; } try { bookTypeService.updateBookType(bookType); 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="/{bookTypeId}/delete",method=RequestMethod.GET) public String delete(@PathVariable int bookTypeId,HttpServletRequest request) throws UnsupportedEncodingException { try { bookTypeService.deleteBookType(bookTypeId); request.setAttribute("message", java.net.URLEncoder.encode("BookType删除成功!", "GBK")); return "message"; } catch (Exception e) { e.printStackTrace(); request.setAttribute("error", java.net.URLEncoder.encode("BookType删除失败!", "GBK")); return "error"; } } /*后台导出到excel*/ @RequestMapping(value = { "/OutToExcel" }, method = {RequestMethod.GET,RequestMethod.POST}) public String OutToExcel(Model model,HttpServletRequest request,HttpServletResponse response) throws Exception { List<BookType> bookTypeList = bookTypeService.queryBookTypeInfo(); ExportExcelUtil ex = new ExportExcelUtil(); String title = "BookType信息记录"; String[] headers = {"图书编号","图书名称","可借阅天数"}; List<String[]> dataset = new ArrayList<String[]>(); for(int i=0;i<bookTypeList.size();i++) { BookType bookType = bookTypeList.get(i); dataset.add(new String[]{bookType.getBookTypeId() + "",bookType.getBookTypeName(),bookType.getDays() + ""}); } /* 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="+"BookType.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日
最近浏览更多
krispeng LV13
8月25日
799743530 LV11
7月11日
微信网友_7051335135711232
6月24日
暂无贡献等级
LIwEIvvANg
6月12日
暂无贡献等级
ZDFSSB
6月2日
暂无贡献等级
poilkj1313 LV1
5月10日
李俊雄 LV3
5月8日
xy007007 LV1
5月4日
pangzhihui LV14
4月11日
2131234536546 LV7
3月31日