Spring mvc Controller间跳转/重定向/传参

Spring mvc Controller常用写法

1.ModelAndView

@RequestMapping(value = "/getxxxList.html")
public ModelAndView getxxxList(XxxDTO xxxDTO,WebPage webPage){
    //ModelAndView modelAndView = new ModelAndView("/xxx/xxxList");//跳转
    ModelAndView modelAndView = new ModelAndView("redirect:/xxx/xxxList");//重定向
    try{
        //检索参数回显
        modelAndView.addObject("xxxDTO",xxxDTO);
        //执行查询
        modelAndView.addObject("xxxList",xxxList);
    }catch (Exception e){
        e.printStackTrace();
    }
    return modelAndView;
}

2.String

@RequestMapping(value = "/getxxxList.html")
public String getxxxList(XxxDTO xxxDTO,WebPage webPage,Model model){
    try{
        //检索参数回显
        model.addAttribute("xxxDTO",xxxDTO);
        //执行查询
        model.addAttribute("xxxList",xxxList);
    }catch (Exception e){
        e.printStackTrace();
    }
    //return "/xxx/xxxList";//跳转
    return "redirect:/xxx/xxxList";//重定向
}
返回地址参数拼接

1.手动拼接URL

"redirect:/xxx/xxxList?param1="+value1+"&param2="+value2"

2-1.使用RedirectAttributes自动拼接重定向URL

@RequestMapping(value = "/getxxxList.html")
public String getxxxList(RedirectAttributes redirectAttributes){
    redirectAttributes.addAttribute("param1", value1);
    redirectAttributes.addAttribute("param2", value2);
    return "redirect:/xxx/toController";//重定向
}

Tip:曾经在项目中遇到过很诡异的重定向问题,业务代码执行无错无异常并且顺利到达 return,重定向结果就是页面报错500,后台并未有异常抛出,且在众多Controller方法中,只有两三个Controller方法遇到了这样的问题,使用RedirectAttributes后解决了这个重定向异常的问题。

2-2.使用RedirectAttributes的addFlashAttribute方法

@RequestMapping("/save")
public String save(@ModelAttribute("form") XxxBean 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:/xxx/toController";
    }
}

注意:

  1. 在2-1中使用addAttribute方法传参,参数会自动拼接在URL后面,而使用addFlashAttribute方法会把参数值暂存于session,待重定向URL获取该参数后从session中移除,这里的redirect必须是方法映射路径,jsp无效。重定向后参数值只会出现一次,刷新页面后不再出现。
  2. 如果使用了RedirectAttributes作为参数,但是没有进行redirect,这种情况下不会将RedirectAttributes参数进行传递,默认还是传递forward对应的model,官方的建议是可以设置RequestMappingHandlerAdapter的ignoreDefaultModelOnRedirect属性,这样可以提高效率,避免不必要的检索。
  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring MVC 中,可以使用重定向和转发来进行请求的跳转。下面是设定重定向和转发的几种方式: 1. 重定向: - 使用 `RedirectView` 类:可以在控制器方法中返回一个 `RedirectView` 对象,设置重定向的目标 URL。 - 使用 `RedirectAttributes` 类:可以在控制器方法中将重定向的目标 URL 添加到 `RedirectAttributes` 对象中,并使用 `redirect:` 前缀来指示重定向。 2. 转发: - 使用 `ModelAndView` 类:可以在控制器方法中返回一个 `ModelAndView` 对象,设置转发的视图名称。 - 使用 `forward:` 前缀:可以在控制器方法中使用 `return "forward:/path"` 的方式来指示转发到指定的路径。 下面是一个示例,展示如何在控制器方法中设定重定向和转发: ```java @Controller public class MyController { @GetMapping("/redirect") public RedirectView redirectToUrl() { RedirectView redirectView = new RedirectView(); redirectView.setUrl("https://www.example.com"); return redirectView; } @GetMapping("/redirectWithAttributes") public String redirectWithAttributes(RedirectAttributes attributes) { attributes.addAttribute("param", "value"); return "redirect:/targetUrl"; } @GetMapping("/forward") public ModelAndView forwardToView() { ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("forward:/targetView"); return modelAndView; } } ``` 上述代码中,`/redirect` 路径的请求会被重定向到 `https://www.example.com`,`/redirectWithAttributes` 路径的请求会带着参数重定向到 `/targetUrl`,`/forward` 路径的请求会被转发到 `targetView` 视图。 需要注意的是,在设定重定向和转发时,可以使用绝对路径或相对路径,具体根据需求来确定。同时,还可以在路径中使用占位符和路径参数来实现动态的跳转

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值