详解SpringMVC重定向传参数的实现

作者:Joepis 时间:2022-09-20 19:01:02 

在spring的一个controller中要把参数传到页面,只要配置视图解析器,把参数添加到Model中,在页面用el表达式就可以取到。但是,这样使用的是forward方式,浏览器的地址栏是不变的,如果这时候浏览器F5刷新,就会造成表单重复提交的情况。所以,我们可以使用重定向的方式,改变浏览器的地址栏,防止表单因为刷新重复提交。

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";//此方式跳转,页面刷新会重复提交表单
   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在跳到页面后马上移除对象。所以你刷新一下后这个值就会丢失。


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添加的数据,之后就可以正常使用啦。

需要例子代码的可以点此下载:http://xiazai.jb51.net/201701/yuanma/springmvcdemo_jb51.rar

来源:http://blog.csdn.net/u011851478/article/details/51872977

标签:springmvc,参数
0
投稿

猜你喜欢

  • Springboot集成spring data elasticsearch过程详解

    2023-04-16 00:42:16
  • Android View事件机制 21问21答

    2022-10-14 04:27:10
  • Java统计输入字符的英文字母、空格、数字和其它

    2022-11-13 04:11:31
  • Java求解两个非负整数最大公约数算法【循环法与递归法】

    2021-10-15 13:53:48
  • 详解JS与APP原生控件交互

    2022-11-21 21:28:15
  • Android 调用系统照相机拍照和录像

    2023-10-30 05:40:35
  • Java并发编程面试之线程池

    2023-11-11 10:58:33
  • spring boot整合RabbitMQ实例详解(Fanout模式)

    2022-08-18 18:52:30
  • 基于SpringBoot中activeMq的JmsTemplate的实例

    2021-11-04 15:36:34
  • 解决springmvc关于前台日期作为实体类对象参数类型转换错误的问题

    2023-11-28 20:53:42
  • springBoot项目如何实现启动多个实例

    2021-09-22 00:13:47
  • java.net.ConnectException: Connection refused问题解决办法

    2023-11-28 18:49:52
  • Android控件之CheckBox、RadioButton用法实例分析

    2021-06-01 06:31:26
  • PC蓝牙通信C#代码实现

    2023-07-06 19:59:32
  • 基于WPF实现3D画廊动画效果的示例代码

    2022-02-06 08:02:22
  • DevExpress实现GridControl根据列选中一行

    2021-08-16 05:23:19
  • 在@Value注解内使用SPEL自定义函数方式

    2022-04-26 20:59:41
  • springBoot整合redis使用案例详解

    2022-07-02 10:51:33
  • Android实现调用摄像头拍照与视频功能

    2021-11-22 08:02:28
  • IDEA2022创建Maven Web项目教程(图文)

    2022-09-25 02:30:44
  • asp之家 软件编程 m.aspxhome.com