详解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后了。
但是,这样页面不能用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
猜你喜欢
- 概述Kryo 是一个快速序列化/反序列化工具,依赖于字节码生成机制(底层使用了 ASM 库),因此在序列化速度上有一定的优势,但正因如此,其
- 引言:上一专题介绍了下编译器是如何来翻译委托的,从中间语言的角度去看委托,希望可以帮助大家进一步的理解委托,然而之前的介绍都是委托只是封装一
- 引言我已经一个多星期没碰过电脑了,今日上班,打开电脑的第一件事就是想着写点什么。反正大家都还沉浸在节后的喜悦中,还没进入工作状态,与其浪费时
- 封装类用于阻止系统休眠的C#类。以下是代码注释的解释:DllImport("kernel32.dll"):定义了一个AP
- 这节主要完成一些基本的增删改查以及Service、Dao和Action的抽取。1. Service层的抽取  
- 1 配置文件的方法我们编写spring 框架的代码时候。一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量
- springboot+调用支付宝第三方接口(沙箱环境)大神勿喷!!网址:https://developers.alipay.com/plat
- 这篇文章主要介绍了如何基于Java实现对象List排序,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋
- **请注意!请注意!!!**今天讲给大家讲解非常“有用”的设计模式,解释器模式!!!设计模式有三大种类,一种是创建型模式,一种是结构型模式,
- 默认情况下,插件 spring-boot-maven-plugin 会把整个项目打包成一个可运行的Jar包(即所谓的Flat Jar),导致
- 在谈Spring事务管理之前我们想一下在我们不用Spring的时候,在Hibernate中我们是怎么进行数据操作的。在Hibernate中我
- 这篇文章主要介绍了JAVA使用 * 对象进行敏感字过滤代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值
- 我记得最开始接触多进程,多线程这一块的时候我不是怎么理解,为什么要有多线程啊?多线程到底是个什么鬼啊?我一个程序好好的就可以运行为什么要用到
- 快速排序快速排序是一种比较高效的排序算法,采用“分而治之”的思想,通过多次比较和交换来实现排序,在一
- 背景:当我们有需求将HashMap转为Json格式的String时,切记不要使用HashMap的toString()方法,需要使用FastJ
- 本次和大家分享的是怎么来消费服务,上篇文章讲了使用Feign来消费,本篇来使用rest+ribbon消费服务,并且通过轮询方式来自定义了个简
- Android的WebView做不到ios的WebView那样可以很方便的直接预览pdf文件。要实现利用WebView预览pdf我们可以使用
- 最近在做一个需求:从其他系统的ftp目录下载存储图片url的文件,然后读取文件中的url地址,根据地址下载图片后按天压缩成一个包,平均一个地
- 本文为大家分析了Java中字符流与字节流的区别,供大家参考,具体内容如下1. 什么是流 Java中的流是
- 一、SpringBoot是什么Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以