天天看点

Springmvc重定向参数以及表单提交问题的认识

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

jsp文件:

[html]  view plain  copy

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  5. <html>  
  6. <head>  
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  8. <title>login</title>  
  9. </head>  
  10. <body>  
  11.     <form id="form1" action="/demo/user/login" method="post">  
  12.         账号:<input type="text" name="name" /></br>  
  13.         密码:<input type="password" name="password" /></br>  
  14.         <input type="submit" value="submit"/>  
  15.     </form>  
  16. </body>  
  17. </html>  

controller:

[java]  view plain  copy

  1. package com.demo.controller;  
  2. import java.util.Map;  
  3. import org.springframework.stereotype.Controller;  
  4. import org.springframework.ui.Model;  
  5. import org.springframework.web.bind.annotation.RequestMapping;  
  6. import org.springframework.web.bind.annotation.RequestParam;  
  7. @Controller  
  8. @RequestMapping("/user")  
  9. public class DemoController {  
  10.     @RequestMapping("/login")  
  11.     public String login(@RequestParam Map<String, String> user, Model model) {  
  12.         System.out.println("用户提交了一次表单");  
  13.         String username;  
  14.         if (user.get("name").isEmpty()) {  
  15.             username = "Tom";  
  16.         } else {  
  17.             username = user.get("name");  
  18.         }  
  19.         model.addAttribute("msg", username);  
  20. //        return "home";//此方式跳转,页面刷新会重复提交表单  
  21.         return "redirect:/home.jsp";  
  22.     }  
  23. }  

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

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

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

Springmvc重定向参数以及表单提交问题的认识

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

还是使用RedirectAttributes,但是这次不用addAttribute方法,spring为我们准备了新方法,addFlashAttribute()。

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

[java]  view plain  copy

  1. package com.demo.controller;  
  2. import java.util.Map;  
  3. import org.springframework.stereotype.Controller;  
  4. import org.springframework.ui.Model;  
  5. import org.springframework.web.bind.annotation.ModelAttribute;  
  6. import org.springframework.web.bind.annotation.RequestMapping;  
  7. import org.springframework.web.bind.annotation.RequestParam;  
  8. import org.springframework.web.servlet.mvc.support.RedirectAttributes;  
  9. @Controller  
  10. @RequestMapping("/user")  
  11. public class DemoController {  
  12.     @RequestMapping("/login")  
  13. //  public String login(@RequestParam Map<String, String> user, Model model) {  
  14.     public String login(@RequestParam Map<String, String> user, RedirectAttributes model) {  
  15.         System.out.println("用户提交了一次表单");  
  16.         String username;  
  17.         if (user.get("name").isEmpty()) {  
  18.             username = "Tom";  
  19.         } else {  
  20.             username = user.get("name");  
  21.         }  
  22.         model.addFlashAttribute("msg", username);  
  23. //      return "home";//此方式跳转,页面刷新会重复提交表单  
  24.         return "redirect:/user/toHome";  
  25.     }  
  26.     @RequestMapping("/toHome")  
  27.     public String home(@ModelAttribute("msg") String msg, Model model) {  
  28.         System.out.println("拿到重定向得到的参数msg:" + msg);  
  29.         model.addAttribute("msg", msg);  
  30.         return "home";  
  31.     }  
  32. }  

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

补充一点:使用无论是使用RedirectAttribute,还是在后面拼接url,其本质上都是利用了二次的转发。

如果我们单单省略掉toHome后面的代码直接跳转到jsp页面,发现直接重定向是无法获取任何参数的。

这里作者作了一个中介的处理,从定向到toHome,然后再进行转发到home.jsp页面,因为model里面的数据都是放在request请求域里面的。

springMvc无论怎么封装其内部实现一定是通过request.getRequestDispatcher("/home.jsp").forward(request, response);

我之所以写下这么一段话是因为之前以为springmvc里面确实有实现重定向实现参数传递的方式。其本质还是通过转发实现的。

那为什么要多此一举呢,就是防止刷新的时候出现表单重复提交的问题。

转载自:http://blog.csdn.net/u011851478/article/details/51872977

继续阅读