重定向處理

1、返回類型是String時通過return “redirect:/toUserInfoPage”;

@RequestMapping("/save")    public String save(@ModelAttribute("form") Bean form,RedirectAttributes attr) throws Exception {
        String code =  service.save(form);
        if(code.equals("000")){
            attr.addFlashAttribute("name", form.getName());  
            attr.addFlashAttribute("success", "添加成功!");
            return "redirect:/index";
        }else{
            attr.addAttribute("projectName", form.getProjectName());  
            attr.addAttribute("enviroment", form.getEnviroment());  
            attr.addFlashAttribute("msg", "添加出錯!錯誤碼爲:"+rsp.getCode().getCode()+",錯誤爲:"+rsp.getCode().getName());
            return "redirect:/maintenance/toAddConfigCenter";
        }
    }

2、返回類型是ModelAndView 時


    1.     @RequestMapping(value = "/user/save", method = RequestMethod.POST)  
    2. 
    public ModelAndView saveUser(HttpServletRequest request, HttpServletResponse response,UserModel user) throws Exception {  
    3. 
        ModelAndView mv = new ModelAndView("/user/save/result");//默認爲forward模式  
    4. 
//      ModelAndView mv = new ModelAndView("redirect:/user/save/result");//redirect模式  
    5. 
        mv.addObject("message","保存用戶成功!");  
    6. 
        return mv;  
    7. 
    }  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章