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

但是,這樣頁(yè)面不能用el獲取到,還要另外處理,所以,我們還有一種方式,不拼url,用el獲取參數(shù),就像普通轉(zhuǎn)發(fā)一樣。
還是使用RedirectAttributes,但是這次不用addAttribute方法,spring為我們準(zhǔn)備了新方法,addFlashAttribute()。
這個(gè)方法原理是放到session中,session在跳到頁(yè)面后馬上移除對(duì)象。所以你刷新一下后這個(gè)值就會(huì)丟失。
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),頁(yè)面刷新會(huì)重復(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ù),之后就可以正常使用啦。
需要例子代碼的可以點(diǎn)此下載:http://xiazai.jb51.net/201701/yuanma/springmvcdemo_jb51.rar
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
mybatis?resultMap之collection聚集兩種實(shí)現(xiàn)方式
本文主要介紹了mybatis?resultMap之collection聚集兩種實(shí)現(xiàn)方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-09-09
如何解決報(bào)錯(cuò):java.net.BindException:無(wú)法指定被請(qǐng)求的地址問題
在Linux虛擬機(jī)上安裝并啟動(dòng)Tomcat時(shí)遇到啟動(dòng)失敗的問題,通過檢查端口及配置文件未發(fā)現(xiàn)異常,后發(fā)現(xiàn)/etc/hosts文件中缺少localhost的映射,添加后重啟Tomcat成功,Tomcat啟動(dòng)時(shí)會(huì)檢查localhost的IP映射,缺失或錯(cuò)誤都可能導(dǎo)致啟動(dòng)失敗2024-10-10
Vue3源碼解讀effectScope API及實(shí)現(xiàn)原理
這篇文章主要為大家介紹了Vue3源碼解讀effectScope API及實(shí)現(xiàn)原理,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
springboot CommandLineRunner接口實(shí)現(xiàn)自動(dòng)任務(wù)加載功能
這篇文章主要介紹了springboot CommandLineRunner接口實(shí)現(xiàn)自動(dòng)任務(wù)加載功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05
詳解ConcurrentHashMap如何保證線程安全及底層實(shí)現(xiàn)原理
這篇文章主要為大家介紹了ConcurrentHashMap如何保證線程安全及底層實(shí)現(xiàn)原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
Spring結(jié)合WebSocket實(shí)現(xiàn)實(shí)時(shí)通信的教程詳解
WebSocket?是基于TCP/IP協(xié)議,獨(dú)立于HTTP協(xié)議的通信協(xié)議,本文將使用Spring結(jié)合WebSocket實(shí)現(xiàn)實(shí)時(shí)通信功能,有需要的小伙伴可以參考一下2024-01-01
java代理 jdk動(dòng)態(tài)代理應(yīng)用案列
java代理有jdk動(dòng)態(tài)代理、cglib代理,這里只說下jdk動(dòng)態(tài)代理,jdk動(dòng)態(tài)代理主要使用的是java反射機(jī)制,需要了解的朋友可以參考下2012-11-11

