• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

详解SpringMVC重定向传参数的实现

java 搞代码 4年前 (2022-01-05) 38次浏览 已收录 0个评论

本篇文章主要介绍了详解SpringMVC重定向传参数的实现,我们可以使用重定向的方式,改变浏览器的地址栏,防止表单因为刷新重复提交。有兴趣的可以了解一下。

在spring的一个controller中要把参数传到页面,只要配置视图解析器,把参数添加到Model中,在页面用el表达式就可以取到。但是,这样使用的是forward方式,浏览器的地址栏是不变的,如果这时候浏览器F5刷新,就会造成表单重复提交的情况。所以,我们可以使用重定向的方式,改变浏览器的地址栏,防止表单因为刷新重复提交。

jsp文件:

   <title>login</title>  账号:</br> 密码:</br>

controller:

 package com.demo.controller; import java.util.Map; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; /** * @author lpj * @date 2016年7月10日 */ @Controller @RequestMapping("/user") public class DemoController { @RequestMapping("/login") public String login(@RequestParam Map user, Model model) { System.out.println("用户提交了一次表单"); String username; if (user.get("name").isEmpty()) { username = "Tom"; } else { username = user.get("name"); } model.addAttribute("msg", username); //    return "home";//此方式跳转,页面刷新会重复提交表单 return "redirect:/home.jsp"; } } 

由于重定向相当于2次请求,所以无法把参数加在model中传过去。在上面例子中,页面获取不到msg参数。要想获取参数,可以手动拼url,把参数带在后面。

Spring 3.1 提供了一个很好用的类:RedirectAttributes。 使用这个类,我们可以把参数随着重定向传到页面,不需自己拼url了。

把上面方法参数中的Model换成RedirectAttributes,参数就自动跟在url后了。

但是,这样页面不能用el获取到,还要另外处理,所以,我们还有一种方式,不拼url,用el获取参数,就像普通转发一样。

还是使用RedirectAttributes,但是这次不用addAttribute方法,s来源gaodai$ma#com搞$$代**码网pring为我们准备了新方法,addFlashAttribute()。

这个方法原理是放到session中,session在跳到页面后马上移除对象。所以你刷新一下后这个值就会丢失。

 package com.demo.controller; import java.util.Map; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.mvc.support.RedirectAttributes; /** * @author lpj * @date 2016年7月10日 */ @Controller @RequestMapping("/user") public class DemoController { @RequestMapping("/login") // public String login(@RequestParam Map user, Model model) { public String login(@RequestParam Map user, RedirectAttributes model) { System.out.println("用户提交了一次表单"); String username; if (user.get("name").isEmpty()) { username = "Tom"; } else { username = user.get("name"); } model.addFlashAttribute("msg", username); //   return "home";//此方式跳转,页面刷新会重复提交表单 return "redirect:/user/toHome"; } @RequestMapping("/toHome") public String home(@ModelAttribute("msg") String msg, Model model) { System.out.println("拿到重定向得到的参数msg:" + msg); model.addAttribute("msg", msg); return "home"; } } 

这边我们使用@ModelAttribute注解,获取之前addFlashAttribute添加的数据,之后就可以正常使用啦。

需要例子代码的可以点此下载:http://xiazai.gaodaima.com/201701/yuanma/springmvcdemo_jb51.rar

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持gaodaima搞代码网

以上就是详解SpringMVC重定向传参数的实现的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:详解SpringMVC重定向传参数的实现

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址