基于重定向RedirectAttributes的用法解析
重定向RedirectAttributes的用法
剛才做項目,遇到了redirectAttributes使用的問題,上網(wǎng)找了找,看到一篇寫的很不錯的博客,解決我對于RedirectAttributes的困惑,也給大家推薦下。
RedirectAttributes 是Spring mvc 3.1版本之后出來的一個功能,專門用于重定向之后還能帶參數(shù)跳轉(zhuǎn)的的工具類
它有兩種帶參的方式
第一種:
redirectAttributes.addAttributie("prama",value); 這種方法相當(dāng)于在重定向鏈接地址追加傳遞的參數(shù),例如:
redirectAttributes.addAttributie("prama1",value1);
redirectAttributes.addAttributie("prama2",value2);
return:"redirect:/path/list"
以上重定向的方法等同于 return:"redirect:/path/list?prama1=value1&prama2=value2 " ,注意這種方法直接將傳遞的參數(shù)暴露在鏈接地址上,非常的不安全,慎用。
第二種:
redirectAttributes.addFlashAttributie("prama",value);
這種方法是隱藏了參數(shù),鏈接地址上不直接暴露,但是能且只能在重定向的 “頁面” 獲取prama參數(shù)值。
其原理就是放到session中,session在跳到頁面后馬上移除對象。如果是重定向一個controller中是獲取不到該prama屬性值的。除非在controller中用(@RequestPrama(value = "prama")String prama)注解,采用傳參的方式。
頁面獲值
例如:
redirectAttributes.addFlashAttributie("prama1",value1);
redirectAttributes.addFlashAttributie("prama2",value2);
return:"redirect:/path/list.jsp"
在以上參數(shù)均可在list.jsp頁面使用EL表達(dá)式獲取到參數(shù)值${prama*}
controller獲得redirectAttributes重定向的值
例如:
redirectAttributes.addFlashAttributie("prama1",value1);
redirectAttributes.addFlashAttributie("prama2",value2);
return:"redirect:/path/list/"
@RequestMapping("list")
public List<Student> list(@RequestPrama(value = "prama1")String prama1,
@RequestPrama(value = "prama2")String prama2,...
){
//TODO
//your code
}
通過在controller中的list方法體中可以獲取到參數(shù)值。
RedirectAttributes詳解
RedirectAttributes 的兩個方式的獲取總結(jié):
1、addFlashAttribute
@RequestMapping (value= "hello" )
public String test(RedirectAttributes ra){
ra.addFlashAttribute( "test" , "test" );
return "redirect:/test" ;
}
@RequestMapping (value= "test" )
public String test( @ModelAttribute ( "test" )String test){
System.out.println(test);
return "redirect:/hello" ;
}
2、addAttribute
@RequestMapping (value= "hello" )
public String test(RedirectAttributes ra){
ra.addAttribute( "test" , "test" );
return "redirect:/test" ;
}
@RequestMapping (value= "test" )
public String test(HttpServletRequest request ){
String test = request.getParameter( "test" );
System.out.println(test);
return "redirect:/hello" ;
}
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Mybatis反射核心類Reflector的實(shí)現(xiàn)
本文主要介紹了Mybatis反射核心類Reflector的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11
Java語言實(shí)現(xiàn)簡單FTP軟件 FTP軟件遠(yuǎn)程窗口實(shí)現(xiàn)(6)
這篇文章主要為大家詳細(xì)介紹了Java語言實(shí)現(xiàn)簡單FTP軟件,F(xiàn)TP軟件遠(yuǎn)程窗口的實(shí)現(xiàn)方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03
Java 數(shù)據(jù)結(jié)構(gòu)之時間復(fù)雜度與空間復(fù)雜度詳解
算法復(fù)雜度分為時間復(fù)雜度和空間復(fù)雜度。其作用: 時間復(fù)雜度是度量算法執(zhí)行的時間長短;而空間復(fù)雜度是度量算法所需存儲空間的大小2021-11-11
Springmvc加ajax實(shí)現(xiàn)上傳文件并頁面局部刷新
這篇文章主要介紹了Springmvc加ajax實(shí)現(xiàn)上傳文件并頁面局部刷新,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-06-06
Java使用CountDownLatch實(shí)現(xiàn)網(wǎng)絡(luò)同步請求的示例代碼
CountDownLatch 是一個同步工具類,用來協(xié)調(diào)多個線程之間的同步,它能夠使一個線程在等待另外一些線程完成各自工作之后,再繼續(xù)執(zhí)行。被將利用CountDownLatch實(shí)現(xiàn)網(wǎng)絡(luò)同步請求,異步同時獲取商品信息組裝,感興趣的可以了解一下2023-01-01
SpringBoot深入理解之內(nèi)置web容器及配置的總結(jié)
今天小編就為大家分享一篇關(guān)于SpringBoot深入理解之內(nèi)置web容器及配置的總結(jié),小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03
SpringBoot中引入MyBatisPlus的常規(guī)操作
這篇文章主要介紹了SpringBoot中引入MyBatisPlus的常規(guī)操作,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
SpringBoot+VUE實(shí)現(xiàn)數(shù)據(jù)表格的實(shí)戰(zhàn)
本文將使用VUE+SpringBoot+MybatisPlus,以前后端分離的形式來實(shí)現(xiàn)數(shù)據(jù)表格在前端的渲染,具有一定的參考價值,感興趣的可以了解一下2021-08-08

