springmvc请求重定向

本文详细解析了Spring MVC框架中的重定向与转发机制,包括如何使用ModelAndView与RedirectAttributes进行请求重定向,以及重定向与转发的区别。通过实例展示了参数传递的不同方式及其在控制器中的处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

请求重定向的作用是将请求,重定向至另外一个处理程序。它的特点是两次请求,浏览器地址会改变,用户可以感知

转发操作,可以使用ModelAndView对象

return new ModelAndView("redirect:viewName",modelMap);

也可以直接返回字符串视图名

return "redirect:viewName";

当重定向需要携带参数时可以使用RedirectAttributes

package com.example.redirect.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
public class HelloController {

    @RequestMapping("hello/{name}")
    public String hello(@PathVariable String name, RedirectAttributes redirectAttributes){
        System.out.println("hello,"+name);
        redirectAttributes.addFlashAttribute("name",name);
        return "redirect:/welcome/{name}";
    }
}

另一个控制器

package com.example.redirect.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class WelcomController {

    @RequestMapping("welcome/{name}")
    @ResponseBody
    public String welcome(@PathVariable String name){
        System.out.println("welcome,"+name);
        return "welcome,"+name;
    }
}

使用addFlashAttribute方法把需要的参数添加进去
在转发的控制器依然可以拿到路径参数

addFlashAttributeaddAttribute的区别是,前者实际是把信息存在用户的session里,然后在下次请求前删除参数,路径中看不到参数;后者是把参数添加在请求中,再进行重定向——参数会被添加在url里

例如:
原请求

http://localhost:8080/hello?name=123

重定向后

http://localhost:8080/welcome?name=123

在使用路径参数时不能从路径中看出差别

不使用路径参数时addFlashAttribute添加的参数,在控制器中使用@ModelAttribute注解得到

 @RequestMapping("hello")
    public String hello(String name, RedirectAttributes redirectAttributes){
        System.out.println("hello,"+name);
        redirectAttributes.addFlashAttribute("name",name);
//        redirectAttributes.addAttribute("name",name);
        return "redirect:/welcome";
    }
 @RequestMapping("welcome")
    @ResponseBody
    public String welcome(@ModelAttribute("name") String name){
        System.out.println("welcome,"+name);
        return "welcome,"+name;
    }

使用addAttribute添加的参数,依然使用@RequestParam获取

### 实现Spring MVC POST 请求重定向Spring MVC框架中,实现POST请求后的重定向操作是一个常见的需求。当接收到`POST`请求并完成相应的业务逻辑处理之后,通常会希望将用户重定向到另一个页面或资源上。 对于带有路径变量的情况,可以在控制器方法内定义@RequestMapping注解来指定HTTP请求的方法类型以及URL模式,并利用字符串形式的视图名称作为返回值来进行重定向。例如: ```java @RequestMapping(value = "/files/{path}", method = RequestMethod.POST) public String upload(@PathVariable("path") String path, ...) { // 执行上传文件或其他业务逻辑... return "redirect:/view/files/" + path; // 或者更简洁的方式:"redirect:files/{path}"[^2] } ``` 这里需要注意的是,在执行重定向的过程中,如果想要保留某些数据供目标地址使用,则可以借助于`RedirectAttributes`对象传递临时性的属性给下一个请求。这有助于保持用户体验的一致性和连贯性而不必担心GET请求可能带来的重复提交问题。 另外一种情况是关于如何确保模型(Model)中的数据能够在重定向后仍然可用。由于默认情况下,原始请求的数据不会被自动携带至新的请求中,因此需要显式地向`RedirectAttributes`添加这些信息以便它们能在后续请求中访问得到[^3]。 ```java import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.support.RedirectAttributes; // ... @RequestMapping(method = RequestMethod.POST) public String handleFormSubmission(RedirectAttributes redirectAttrs){ // 处理表单... // 将消息添加到flash属性中 redirectAttrs.addFlashAttribute("message", "成功保存!"); return "redirect:/success"; } ``` 上述代码片段展示了如何通过`addFlashAttribute()`方法把特定的信息存储起来用于下一次请求显示出来。这种方式非常适合用来反馈操作结果或是其他短暂存在的状态提示。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值