addAttribute方法和addFlashAttribute方法

RedirectAttributes的addAttribute方法和addFlashAttribute方法

还是补javaweb不认真学的坑,之前转发已经说过了可以在request中传递数据,而重定向因为是浏览器根据服务器返回的url再起请求,之前的request已经失效了,所以是不能在request中传递数据的。

但是如果确实需要传递参数,可以通过url拼接的方式,在重定向的url后面通过 ?param1=value1&param2=value2… 的方式来传递参数;或者通过session传递参数。SpringMVC提供了更方便的方法,可以不用自己手动拼url和写session,即RedirectAttributes,它提供了addAttribute方法和addFlashAttribute方法来传递参数。

addAttribute

这个方法比较好理解,就是在url后面拼参数,然后再页面中通过EL表达式从param中取就行了,代码(DispatcherServlet配的是*.do):

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <form action="${pageContext.request.contextPath}/test.do" method="post">
        姓名:<input type="text" name="name"><br>
        年龄:<input type="text" name="age"><br>
        <button type="submit">提交</button>
    </form>
</body>
</html>

Controller方法

@RequestMapping("/test.do")
public String doTest(String name, Integer age, RedirectAttributes attributes) {
    attributes.addAttribute("myName", name);
    attributes.addAttribute("myAge", age);
    return "redirect:/result.jsp";
}

result.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>结果</title>
</head>
<body>
    <h1>取到的结果</h1>
    <h3>param姓名:${param.myName}</h3>
    <h3>param年龄:${param.myAge}</h3>
</body>
</html>

提交表单之前:

提交表单之后:

可以看到url确实变成result.jsp,并且后面拼上了参数,这里已经在web.xml中配置了编码过滤器所以post没乱码。这个也比较好理解,用起来也比较方便。

addFlashAttribute

这个方法我纠结了好久,网上搜到的全是这个方法存到session里,页面中直接用EL表达式就能取,但是我试了一下发现根本取不到啊?代码如下:

Controller方法:

@RequestMapping("/test.do")
public String doTest(String name, Integer age, RedirectAttributes attributes) {
    attributes.addFlashAttribute("myName", name);
    attributes.addFlashAttribute("myAge", age);
    return "redirect:/result.jsp";
}

result.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>结果</title>
</head>
<body>
    <h1>取到的结果</h1>
    <h3>EL姓名:${myName}</h3>
    <h3>EL年龄:${myAge}</h3>   
</body>
</html>

提交表单后:

可以看到确实重定向到result.jsp了,而且也没有在后面拼参数,但是EL表达式是不能直接取到session里的东西的。接下来在jsp页面里打印一下session里的东西,看到底存到哪了。

在result.jsp中加一段代码,在控制台打印一下session中的内容:

<%
    final Enumeration se = session.getAttributeNames();
    while (se.hasMoreElements()) {
        final String key = (String) se.nextElement();
        System.out.println(key + "==" + session.getAttribute(key));
    }
%>

控制台输出:

org.springframework.web.servlet.support.SessionFlashMapManager.FLASH_MAPS== [

​ FlashMap [

​ attributes={myName=张三, myAge=22},

​ targetRequestPath=/forward_redirect_war_exploded/result.jsp,

​ targetRequestParams={}

​ ]

]

可以看到session里总共就存了一个东西FLASH_MAPS,里面的FlashMap存了各种东西,所以如果要从通过EL表达式从session中取值,需要格式对了才能取到,不然肯定是拿不到的,所以result.jsp中这么写就可以拿到值:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>结果</title>
</head>
<body>
    <h1>取到的结果</h1>
    <h3>EL姓名:${sessionScope['org.springframework.web.servlet.support.SessionFlashMapManager.FLASH_MAPS'][0]['myName']}</h3>
    <h3>EL年龄:${sessionScope['org.springframework.web.servlet.support.SessionFlashMapManager.FLASH_MAPS'][0]['myAge']}</h3>   
</body>
</html>

提交表单结果:

暂时没想到有什么更好的写法,反正这么写是肯定能取到,但是太麻烦了…

二次转发

还可以通过二次转发的方式取到值,这种方法稍微简单一点,可以直接用EL表达式获取值,也不会因为 在url上拼参数,导致安全性问题,但是因为第二次是转发,所以地址栏最终的url就会比较尴尬,代码如下:

@RequestMapping("/test.do")
public String doTest(String name, Integer age, RedirectAttributes attributes) {
    attributes.addFlashAttribute("myName", name);
    attributes.addFlashAttribute("myAge", age);
    return "redirect:/mid.do";
}

@RequestMapping("/mid.do")
public String Mid() {
    return "/result.jsp";
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>结果</title>
</head>
<body>
    <h1>取到的结果</h1>
    <h3>经二次转发姓名:${myName}</h3>
    <h3>经二次转发年龄:${myAge}</h3>   
</body>
</html>

提交表单后的结果:

因为第二次是转发,所以地址就尬在mid.do了,体验应该不是很好。目前没想到最好的解决办法,也欢迎补充。

  • 17
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
在Spring Boot中,重定向和添加属性是常见的操作之一。 重定向是指将请求从一个URL地址重定向到另一个URL地址。通过使用重定向,可以有效地将用户导航到不同的页面或执行其他操作。在Spring Boot中,可以使用`RedirectView`或`RedirectAttributes`类来实现重定向。 首先,我们需要创建一个处理请求的控制器方法。在该方法中,我们可以使用`RedirectView`类来执行重定向操作,并将重定向的目标URL作为参数传递。例如: ```java @GetMapping("/redirect") public RedirectView redirect() { RedirectView redirectView = new RedirectView(); redirectView.setUrl("/target"); return redirectView; } ``` 上述示例中,我们创建了一个`RedirectView`对象,并通过调用`setUrl()`方法设置了重定向的目标URL。该方法返回一个`RedirectView`对象,表明重定向已成功执行。 接下来,我们可以通过`RedirectAttributes`类将属性添加到重定向的URL中。通过将属性添加到重定向URL中,我们可以在重定向后的页面上访问这些属性。例如: ```java @GetMapping("/redirectWithAttribute") public RedirectView redirectWithAttribute(RedirectAttributes redirectAttributes) { redirectAttributes.addFlashAttribute("message", "重定向页面携带的属性值"); return new RedirectView("/target"); } ``` 上述示例中,我们在`redirectWithAttribute()`方法中将一个名为"message"的属性添加到重定向URL中,属性值为"重定向页面携带的属性值"。通过调用`addFlashAttribute()`方法,我们可以将属性暂存到会话中,在重定向后的页面上可以访问此属性。 总结而言,Spring Boot中可以通过`RedirectView`类来实现重定向操作,也可以使用`RedirectAttributes`类将属性添加到重定向URL中。这些功能使得在应用程序中实现页面重定向以及携带属性变得更加简单和便捷。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值