java web 中 頁面/業務重定向

情形一:地址欄簡潔明瞭(列表頁面須要局部查詢,分頁) 服務器

  頁面初始化:library方法,沒有任何過濾條件,顯示第一頁;app

    頁面刷新:library方法,page依舊保留,在地址欄中顯示,但局部查詢條件會清空(地址欄中不存在);ui

    點擊頁面刷新:進入search方法,page之後綴傳遞到 library中對應的局部條件藉助 RedirectAttributes 用map傳遞到 library中;spa

    局部查詢:點擊查詢進入search方法,page清空(另外一角度考慮條件不一樣,分頁從新構建也算合理);code

/** 供應商庫 */
@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;
}

 

情形二:公共方法進入到一個頁面,但要經過具體業務進行區分開來單獨業務處理(圖片上傳業務,經過圖片上傳服務器處理好,統一的入口接收,分發到對應的業務中); blog

 

@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 ? "新增成功!" : "修改爲功!");
}
相關文章
相關標籤/搜索