java web 中 页面/业务重定向
情形一:地址栏简洁明了(列表页面需要局部查询,分页)
页面初始化:library方法,没有任何过滤条件,显示第一页;
页面刷新:library方法,page依旧保留,在地址栏中显示,但局部查询条件会清空(地址栏中不存在);
点击页面刷新:进入search方法,page以后缀传递到 library中对应的局部条件借助 RedirectAttributes 用map传递到 library中;
局部查询:点击查询进入search方法,page清空(另一角度考虑条件不同,分页重新构建也算合理);
/** 供应商库 */ @RequestMapping("/library") @SuppressWarnings("unchecked") public String library(HttpServletRequest request, ModelMap model, @RequestParam(value = "page", required = false, defaultValue = "1") Integer page) { Map<String, Object> map = new HashMap<String, Object>(); if (model.containsKey("map")) { map = (Map<String, Object>) model.get("map"); model.addAllAttributes(map); model.remove("map"); } model.addAttribute("moneys", dictRangeService.queryForListByType("MONEY")); model.addAttribute("areas", dictService.queryDictByType("AREA")); model.addAttribute("states", CompanyStateEnum.values()); map.put("putinState", 1); model.addAttribute("page", companyService.queryLibraryForListByPage(page, 10, map)); return "/epurchaser/supplier/library"; } /** * 供应商库 查询 */ @RequestMapping("/search") public String search(HttpServletRequest request, RedirectAttributes attr, @RequestParam(value = "page", required = true, defaultValue = "1") Integer page) { Map<String, Object> map = new HashMap<String, Object>(); Set<String> keys = request.getParameterMap().keySet(); for (String key : keys) { if (StringUtils.isNotEmpty(request.getParameter(key))) { map.put(key, request.getParameter(key)); } } attr.addFlashAttribute("map", map); return "redirect:/pur/supplier/library?page="+page; }
情形二:公共方法进入到一个页面,但要通过具体业务进行区分开来单独业务处理(图片上传业务,通过图片上传服务器处理好,统一的入口接收,分发到对应的业务中);
@GetMapping("/upload/save") public @ResponseBody Object save(HttpServletRequest request, RedirectAttributes attr, @RequestParam Integer type) throws ParseException { String name = request.getParameter("name"); String picture = request.getParameter("picture"); ... if (StringUtils.isBlank(name) || StringUtils.isBlank(picture)) { return new ApiResult<Object>(MsgCons.C_600, "名称和图片不能为空"); } Long companyId = currentUser().getCompanyId(); if (currentUser().getCompanyId() == null) { return new ApiResult<Object>(MsgCons.C_600, "请先填写基本信息"); } if (type == 1) { // 1.业务1 ... attr.addFlashAttribute("entity", entity1); return new ModelAndView("redirect:/entity1/save"); } else if (type == 2) { //2:业务2 ... attr.addFlashAttribute("entity", entity2); return new ModelAndView("redirect:/entity2/save"); } else if (type == 3) { //3:业务3 ... attr.addFlashAttribute("entity", entity3); return new ModelAndView("redirect:/entity3/save"); } else if (type == 4) { //4:业务4 ... return new ModelAndView("redirect:/entity4/save"); } else { return new ApiResult<Object>(MsgCons.C_600, MsgCons.M_600); } return new ModelAndView("redirect:/entity/save"); } @GetMapping("/entity/save") public @ResponseBody ApiResult<Object> saveEntity(ModelMap model) { AbstractModel entity = (AbstractModel) model.get("entity"); Boolean isAdd = (entity.getId() == null); try { if (entity instanceof Entity1) { if (isAdd) entity = service1.insert((Entity1) entity); else service1.update((CompanyAptitude) entity); } else if (entity instanceof Entity2) { if (isAdd) entity = service2.insert((Entity2) entity); else service2.update((CompanyHonor) entity); } } catch (Exception e) { logger.error(e.getMessage()); return new ApiResult<Object>(MsgCons.C_500, MsgCons.M_500); } return new ApiResult<Object>(entity, isAdd == true ? "新增成功!" : "修改成功!"); }