应用spring mvc 在controller间跳转 重定向

    最近在做项目,对springMVC不是很熟悉,应用spring mvc 在controller间跳转 重定向有几种情况:不带参数跳转,带参数拼接url形式跳转,带参数不拼接参数跳转,页面也能显示,自己做了一下总结,和大家一起分享。

   (1)在后台一个controller跳转到另一个controller,为什么有这种需求呢,是这样的。我有一个列表页面,然后我会进行新增操作,新增在后台完成之后我要跳转到列表页面,不需要传递参数,列表页面默认查询所有的。

     方式一:使用ModelAndView

         return newModelAndView("redirect:/toList");

         这样可以重定向到toList这个方法

     方式二:返回String

             return"redirect:/ toList "; 

     其它方式:其它方式还有很多,这里不再做介绍了,比如说response等等。这是不带参数的重定向。

   (2)第二种情况,列表页面有查询条件,跳转后我的查询条件不能丢掉,这样就需要带参数的了,带参数可以拼接url

     方式一:自己手动拼接url

             newModelAndView("redirect:/toList?param1="+value1+"&param2="+value2);

             这样有个弊端,就是传中文可能会有乱码问题。

       方式二:用RedirectAttributes,这个是发现的一个比较好用的一个类

             这里用它的addAttribute方法,这个实际上重定向过去以后你看url,是它自动给你拼了你的url。

             使用方法:

             attr.addAttribute("param",value);

             return"redirect:/namespace/toController";

            这样在toController这个方法中就可以通过获得参数的方式获得这个参数,再传递到页面。过去的url还是和方式一一样的。

    (3)带参数不拼接url页面也能拿到值(重点是这个)        

<span style="font-family:SimSun;font-size:14px;"> @RequestMapping("/save")
    public Stringsave(@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";
        }
    }
@RequestMapping("/index")
     
    public Stringsave(@ModelAttribute("form") Bean form,RedirectAttributes attr)
                  throws Exception {
            return"redirect:/main/list";
    }</span>

    最底层还是两种跳转,只是spring又进行了封装而已,所以说跳转的方式其实有很多很多种,你自己也可以封一个,也可以用最原始的response来,也没有问题。

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` 视图。 需要注意的是,在设定重定向和转发时,可以使用绝对路径或相对路径,具体根据需求来确定。同时,还可以在路径中使用占位符和路径参数来实现动态的跳转
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值