zoukankan      html  css  js  c++  java
  • RedirectAttributes 的使用

    因为使用重定向的跳转方式的情况下,跳转到的地址无法获取 request 中的值。RedirecAtrributes 很好的解决了这个问题。

    1. redirectAttributes.addAttributie("param", value);

    这种方法相当于在重定向链接地址追加传递的参数。以上重定向的方法等同于 return "redirect:/hello?param=value" ,注意这种方法直接将传递的参数暴露在链接地址上,非常的不安全,慎用。

    2. redirectAttributes.addFlashAttributie("param", value);

    这种方法是隐藏了参数,链接地址上不直接暴露,但是能且只能在重定向的 “页面” 获取 param 参数值。其原理就是将设置的属性放到 session 中,session 中的属性在跳到页面后马上销毁

    注意:这种方式在页面中可以正常获取,但是跳转目标是控制器方法的情况下,需要使用 @ModelAttribute 注解绑定参数后才能获取。

    package com.pudding.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.ModelAttribute;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.mvc.support.RedirectAttributes;
    
    @Controller
    public class RedirectController {
    
    	@RequestMapping("/set-flash-attribute")
    	public String setFlashAttribute(RedirectAttributes redirectAttribute) {
    		redirectAttribute.addFlashAttribute("username", "jack");
    		return "redirect:/user-information";
    	}
    
    	@RequestMapping("/user-information")
    	public String get(@ModelAttribute("username") String username) {
    
    		System.out.println(username);
    
    		return "/user-information";
    	}
    
    }
    
  • 相关阅读:
    [thinkphp] 是如何输出一个页面的
    [thinkphp] 获取根目录绝对路径
    onethink 插件模板定位
    win7 安全模式开启声音
    百度贴吧楼层评论地址
    第一天问题
    [php] 解析JSON字符串
    NDK编译时两 .so之间调用问题
    CDN问题积累
    C++模板特化
  • 原文地址:https://www.cnblogs.com/lemon-coke-pudding/p/12774995.html
Copyright © 2011-2022 走看看