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

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

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

重定向參數(shù)RedirectAttributes

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

SpringMVC 用 RedirectAttributes 解決了這兩個(gè)需要。

1. addAttribute

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

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

2. addFlashAttribute

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

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

RedirectAttributes 中兩個(gè)方法的簡(jiǎn)單介紹就是這樣。

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

問題描述

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

   @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頁(yè)面就能獲取到msg的值。

問題來源

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

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

相關(guān)文章

最新評(píng)論

扶沟县| 厦门市| 新疆| 东安县| 百色市| 海丰县| 栖霞市| 正阳县| 宁晋县| 永仁县| 博罗县| 永泰县| 丹凤县| 潮安县| 仙游县| 彭州市| 桐乡市| 陇南市| 驻马店市| 勃利县| 积石山| 响水县| 兴仁县| 贺州市| 蓝田县| 滨州市| 秀山| 鱼台县| 化德县| 虞城县| 武安市| 灵宝市| 兴仁县| 青河县| 侯马市| 舒城县| 平远县| 深圳市| 山丹县| 科技| 涟水县|