一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 詳解SpringMVC重定向傳參數的實現

詳解SpringMVC重定向傳參數的實現

2020-07-30 15:33Joepis Java教程

本篇文章主要介紹了詳解SpringMVC重定向傳參數的實現,我們可以使用重定向的方式,改變瀏覽器的地址欄,防止表單因為刷新重復提交。有興趣的可以了解一下。

在spring的一個controller中要把參數傳到頁面,只要配置視圖解析器,把參數添加到Model中,在頁面用el表達式就可以取到。但是,這樣使用的是forward方式,瀏覽器的地址欄是不變的,如果這時候瀏覽器F5刷新,就會造成表單重復提交的情況。所以,我們可以使用重定向的方式,改變瀏覽器的地址欄,防止表單因為刷新重復提交。

jsp文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<%@ 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:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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后了。

詳解SpringMVC重定向傳參數的實現

但是,這樣頁面不能用el獲取到,還要另外處理,所以,我們還有一種方式,不拼url,用el獲取參數,就像普通轉發一樣。

還是使用RedirectAttributes,但是這次不用addAttribute方法,spring為我們準備了新方法,addFlashAttribute()。

這個方法原理是放到session中,session在跳到頁面后馬上移除對象。所以你刷新一下后這個值就會丟失。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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添加的數據,之后就可以正常使用啦。

需要例子代碼的可以點此下載:springmvcdemo.rar

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://blog.csdn.net/u011851478/article/details/51872977

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 楚乔传第二部免费播放电视连续剧 | 亚洲AV精品无码喷水直播间 | 97久久久亚洲综合久久88 | 黄片毛片 | 欧美日韩国产精品va | 午夜办公室 | 四虎精品在线观看 | gayrb免费漫画入口 | 妹妹你插的我好爽 | 高清不卡免费一区二区三区 | 色男人的天堂久久综合 | japanese在线观看| 欧美精品一二三区 | 亚洲精品免费在线观看 | 国模李丽莎大尺度啪啪 | 亚洲国产综合自在线另类 | 亚洲精品综合一二三区在线 | 91在线老师啪国自产 | 99久久国产综合精品女小说 | 国产精品原创永久在线观看 | 日韩欧美国产一区 | a在线观看欧美在线观看 | 九九九九在线精品免费视频 | 农夫69小说恋老妇小说 | mm在线| 精品久久久久久亚洲 | gay男男白袜chinese | www.com在线观看 | 免费在线观看中文字幕 | 国产高清视频 | 国产日韩欧美在线一二三四 | 边摸边吃奶边做爽gif动态图 | 四虎成人免费观看在线网址 | 日本性生活免费看 | 国产欧美一区二区三区免费看 | 精品国产人成亚洲区 | 色综合亚洲天天综合网站 | 99热久久这里只有精品6国产网 | 国产精彩对白综合视频 | 日韩欧美一区二区在线观看 | 都市风流贵妇激情 |