詳解SpringMVC重定向傳參數(shù)的實現(xiàn)
在spring的一個controller中要把參數(shù)傳到頁面,只要配置視圖解析器,把參數(shù)添加到Model中,在頁面用el表達式就可以取到。但是,這樣使用的是forward方式,瀏覽器的地址欄是不變的,如果這時候瀏覽器F5刷新,就會造成表單重復(fù)提交的情況。所以,我們可以使用重定向的方式,改變?yōu)g覽器的地址欄,防止表單因為刷新重復(fù)提交。
jsp文件:
<%@ 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:
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";//此方式跳轉(zhuǎn),頁面刷新會重復(fù)提交表單
return "redirect:/home.jsp";
}
}
由于重定向相當于2次請求,所以無法把參數(shù)加在model中傳過去。在上面例子中,頁面獲取不到msg參數(shù)。要想獲取參數(shù),可以手動拼url,把參數(shù)帶在后面。
Spring 3.1 提供了一個很好用的類:RedirectAttributes。 使用這個類,我們可以把參數(shù)隨著重定向傳到頁面,不需自己拼url了。
把上面方法參數(shù)中的Model換成RedirectAttributes,參數(shù)就自動跟在url后了。

但是,這樣頁面不能用el獲取到,還要另外處理,所以,我們還有一種方式,不拼url,用el獲取參數(shù),就像普通轉(zhuǎn)發(fā)一樣。
還是使用RedirectAttributes,但是這次不用addAttribute方法,spring為我們準備了新方法,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<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";//此方式跳轉(zhuǎn),頁面刷新會重復(fù)提交表單
return "redirect:/user/toHome";
}
@RequestMapping("/toHome")
public String home(@ModelAttribute("msg") String msg, Model model) {
System.out.println("拿到重定向得到的參數(shù)msg:" + msg);
model.addAttribute("msg", msg);
return "home";
}
}
這邊我們使用@ModelAttribute注解,獲取之前addFlashAttribute添加的數(shù)據(jù),之后就可以正常使用啦。
需要例子代碼的可以點此下載:http://xiazai.jb51.net/201701/yuanma/springmvcdemo_jb51.rar
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Spring Boot應(yīng)用的啟動和停止(start啟動)
這篇文章主要介紹了詳解Spring Boot應(yīng)用的啟動和停止(start啟動),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12
如何使用spring?boot的程序主線程中異步訪問外部接口
CompletableFuture.supplyAsync提供了一種強大的工具,使您能夠以異步方式執(zhí)行操作,充分利用多核處理器和提高程序性能,同時保持代碼的清晰性和可維護性,本文給大家介紹使用spring?boot的程序主線程中異步訪問外部接口,感興趣的朋友一起看看吧2023-10-10
SpringBoot如何優(yōu)雅的實現(xiàn)重試功能
Java AbstractMethodError案例分析詳解
類似Object監(jiān)視器方法的Condition接口(詳解)
SpringBoot3.0+SpringSecurity6.0+JWT的實現(xiàn)

