SpringMVC重定向傳參數的實現

SpringMVC

在spring的一個controller中要把參數傳到頁面,只要配置視圖解析器,把參數添加到Model中,在頁面用el表達式就能夠取到。可是,這樣使用的是forward方式,瀏覽器的地址欄是不變的,若是這時候瀏覽器F5刷新,就會形成表單重複提交的狀況。因此,咱們可使用重定向的方式,改變瀏覽器的地址欄,防止表單由於刷新重複提交。html

jsp文件:java

<%@ 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:web

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<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後了。 spring


可是,這樣頁面不能用el獲取到,還要另外處理,因此,咱們還有一種方式,不拼url,用el獲取參數,就像普通轉發同樣。 仍是使用RedirectAttributes,可是此次不用addAttribute方法,spring爲咱們準備了新方法,addFlashAttribute()。 這個方法原理是放到session中,session在跳到頁面後立刻移除對象。因此你刷新一下後這個值就會丟失。 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.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<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添加的數據,以後就能夠正常使用啦。session

須要例子代碼的能夠點此下載:demomvc

相關文章
相關標籤/搜索