// _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ // _/_/_/_/_/_/ 下面将产生JSON格式的返回值 // _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ @RequestMapping(value = "/select") @ResponseBody public Page select( @ModelAttribute Page page, @ModelAttribute User user, Model uiModel, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) { super.select(page, uiModel, httpServletRequest, httpServletResponse); UserCriteria userCriteria = new UserCriteria(); UserCriteria.Criteria criteria = userCriteria.createCriteria(); userCriteria.setPage(page); userCriteria.setOrderByClause("id desc"); if (null != user.getId() && !"".equals(user.getId())) { criteria.andIdLike("%" + user.getId() + "%"); } if (null != user.getUsername() && !"".equals(user.getUsername())) { criteria.andUsernameLike("%" + user.getUsername() + "%"); } if (null != user.getPassword() && !"".equals(user.getPassword())) { criteria.andPasswordLike("%" + user.getPassword() + "%"); } page = userService.select(userCriteria); return page; }
@RequestMapping(value = "/{id}", produces = "text/html") public String show(@PathVariable("id") String id, Model uiModel) { User user = userService.selectByPrimaryKey(id); uiModel.addAttribute("user", user); uiModel.addAttribute("itemId", id); return "view/user/user_show"; }
@RequestMapping(value = "/update") @ResponseBody public String update( @ModelAttribute User user, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) { userService.update(user); String id = user.getId(); return id; }
@RequestMapping(value = "updateForm") public String update( @Valid User user, BindingResult result, Model uiModel, HttpServletRequest httpServletRequest) { uiModel.asMap().clear(); userService.update(user); populateEditForm(uiModel, user); return "redirect:edit/" + encodeUrlPathSegment(user.getId().toString(), httpServletRequest); }
@RequestMapping(value = "createForm", method = RequestMethod.POST) public String create( @Valid User user, BindingResult result, Model uiModel, HttpServletRequest httpServletRequest) { if (result.hasErrors()) { populateEditForm(uiModel, user); return "view/user/user_create"; } userService.insert(user); populateEditForm(uiModel, user); return "redirect:/user/new"; // return "redirect:/user/" // + encodeUrlPathSegment(user.getId().toString(), // httpServletRequest); }
@RequestMapping(value = "/deleteById") @ResponseBody public int deleteById( @RequestParam(value = "idstring") String idstring, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException { String[] ids = idstring.split(","); List<String> list = new ArrayList<String>(); for (String idstr : ids) { list.add(idstr); } int result = userService.deleteBatch(list); return result; }
@RequestMapping(value = "/edit/{id}") public String updateForm(@PathVariable("id") String id, Model uiModel) { User user = userService.selectByPrimaryKey(id); populateEditForm(uiModel, user); return "view/user/user_update"; }
/** * 全部导出Excel. * * @param user * @param httpServletRequest * @param httpServletResponse * @throws IOException */ @RequestMapping(value = "/exportExcel") public void exportExcel( @ModelAttribute User user, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException { httpServletResponse.setCharacterEncoding("UTF-8"); String filename = new String("用户信息".getBytes("GBK"), "iso8859-1"); List<User> list = userService.selectAll(); List<Map<String, Object>> maps = createExcelRecord(list); String columnNames[] = { "Id", "Username", "Password", "Status", "Description", "Enabled", "CreateDate", "UpdateDate", "Ip", "Telephone", "Salt", "Locked", "Email", "Sex", "Address", "UserGroup_id" }; // 列名 String keys[] = { "Id", "Username", "Password", "Status", "Description", "Enabled", "CreateDate", "UpdateDate", "Ip", "Telephone", "Salt", "Locked", "Email", "Sex", "Address", "UserGroup_id" }; // map中的key Workbook hwb = ExcelUtil.createWorkBook(maps, keys, columnNames); SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss"); // 等价于now.toLocaleString() filename += "_" + sdf.format(new Date()) + ".xls"; httpServletResponse.setContentType("APPLICATION/OCTET-STREAM"); httpServletResponse.setHeader( "Content-Disposition", "attachment; filename=\"" + filename + "\""); OutputStream os = httpServletResponse.getOutputStream(); hwb.write(os); os.flush(); os.close(); }
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE, produces = "text/html") public String delete(@PathVariable("id") String id, Model uiModel) { userService.deleteByPrimaryKey(id); uiModel.asMap().clear(); return "redirect:/user"; }