在spring的一個controller中要把參數傳到頁面,隻要配置視圖解析器,把參數添加到Model中,在頁面用el表達式就可以取到。但是,這樣使用的是forward方式,浏覽器的位址欄是不變的,如果這時候浏覽器F5重新整理,就會造成表單重複送出的情況。是以,我們可以使用重定向的方式,改變浏覽器的位址欄,防止表單因為重新整理重複送出。
jsp檔案:
[html] view plain copy
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>login</title>
- </head>
- <body>
- <form id="form1" action="/demo/user/login" method="post">
- 賬号:<input type="text" name="name" /></br>
- 密碼:<input type="password" name="password" /></br>
- <input type="submit" value="submit"/>
- </form>
- </body>
- </html>
controller:
[java] view plain copy
- 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;
- @Controller
- @RequestMapping("/user")
- public class DemoController {
- @RequestMapping("/login")
- public String login(@RequestParam Map<String, String> 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方法,spring為我們準備了新方法,addFlashAttribute()。
這個方法原理是放到session中,session在跳到頁面後馬上移除對象。是以你重新整理一下後這個值就會丢失。
[java] view plain copy
- 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;
- @Controller
- @RequestMapping("/user")
- public class DemoController {
- @RequestMapping("/login")
- // public String login(@RequestParam Map<String, String> user, Model model) {
- public String login(@RequestParam Map<String, String> 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添加的資料,之後就可以正常使用啦。
補充一點:使用無論是使用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