最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringMVC 重定向參數(shù)RedirectAttributes實例

 更新時間:2021年12月17日 10:17:51   作者:藍(lán)星花  
這篇文章主要介紹了SpringMVC 重定向參數(shù)RedirectAttributes實例,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

重定向參數(shù)RedirectAttributes

SpringMVC 中常用到 redirect 來實現(xiàn)重定向。但使用場景各有需求,如果只是簡單的頁面跳轉(zhuǎn)顯然無法滿足所有要求,比如重定向時需要在 url 中拼接參數(shù),或者返回的頁面需要傳遞 Model。

SpringMVC 用 RedirectAttributes 解決了這兩個需要。

1. addAttribute

@RequestMapping("/save")
public String save(User user, RedirectAttributes redirectAttributes) {
    redirectAttributes.addAttribute("param", "value1");
    return "redirect:/index";
}

請求 /save 后,跳轉(zhuǎn)至/index,并且會在url拼接 ?param=value1。

2. addFlashAttribute

@RequestMapping("/save")
public String save(User user, RedirectAttributes redirectAttributes) {
    redirectAttributes.addFlashAttribute("param", "value1");
    return "redirect:/index";
}

請求 /save 后,跳轉(zhuǎn)至 /index,并且可以在 index 對應(yīng)的模版中通過表達(dá)式,比如 jsp 中 jstl 用 ${param},獲取返回值。該值其實是保存在 session 中的,并且會在下次重定向請求時刪除。

RedirectAttributes 中兩個方法的簡單介紹就是這樣。

重定向攜帶參數(shù)問題

問題描述

A.jsp發(fā)送請求進(jìn)入Controller,并想重定向到B.jsp并攜帶參數(shù),發(fā)現(xiàn)攜帶的參數(shù)前臺獲取不到,然后采用以下方法即可

   @RequestMapping("/index")
    public String delete(String id, RedirectAttributes redirectAttributes) {
           redirectAttributes.addFlashAttribute("msg","刪除成功!");
           return "redirect:hello";
    }
    @RequestMapping("hello")
    public String index( @ModelAttribute("msg") String msg) {
         
          return "sentinel";
    }

首先進(jìn)入delete方法,將msg放在redirectAttributes里,然后重定向到hello,通過@ModelAttribute(“msg”) String msg獲取到msg的值,那么自然sentinel頁面就能獲取到msg的值。

問題來源

B.jsp發(fā)送請求,跳轉(zhuǎn)到A.jsp,并將請求所產(chǎn)生的數(shù)據(jù)攜帶到A頁面。

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

海口市| 沾益县| 鄂州市| 三明市| 敖汉旗| 新平| 新沂市| 金川县| 天峻县| 尼玛县| 邓州市| 望奎县| 岚皋县| 和平县| 肇源县| 库尔勒市| 邓州市| 卢龙县| 屏山县| 抚顺市| 贡嘎县| 沈阳市| 乐陵市| 永胜县| 平定县| 东莞市| 时尚| 夏津县| 监利县| 广安市| 绥德县| 奎屯市| 玉门市| 施秉县| 黔南| 临漳县| 疏附县| 阳城县| 皋兰县| 新兴县| 衡水市|