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

SpringMVC中重定向传参数的方法的更多相关文章

  1. springMVC controller间跳转 重定向 传递参数的方法

    springMVC controller间跳转 重定向 传递参数的方法 spring MVC框架controller间跳转,需重定向.有几种情况:不带参数跳转,带参数拼接url形式跳转,带参数不拼接参 ...

  2. setInterval(callbackfunc,time)中callbackfunc传参数问题

    var si=setInterval(callbackfunc,time)中callbackfunc传参数问题(循环执行) var st=setTimeout(callbackfunc,time);定 ...

  3. ASP.NET MVC WebApi 返回数据类型序列化控制(json,xml) 用javascript在客户端删除某一个cookie键值对 input点击链接另一个页面,各种操作。 C# 往线程里传参数的方法总结 TCP/IP 协议 用C#+Selenium+ChromeDriver 生成我的咕咚跑步路线地图 (转)值得学习百度开源70+项目

    ASP.NET MVC WebApi 返回数据类型序列化控制(json,xml)   我们都知道在使用WebApi的时候Controller会自动将Action的返回值自动进行各种序列化处理(序列化为 ...

  4. Struts2中Action接收参数的方法

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt112 Struts2中Action接收参数的方法主要有以下三种: 1.使用A ...

  5. Struts2中Action接收参数的方法主要有以下三种:

    Struts2中Action接收参数的方法主要有以下三种: 1.使用Action的属性接收参数(最原始的方式):     a.定义:在Action类中定义属性,创建get和set方法:     b.接 ...

  6. SpringMVC中post请求参数注解@requestBody使用问题

    一.httpClient发送Post 原文https://www.cnblogs.com/Vdiao/p/5339487.html public static String httpPostWithJ ...

  7. Java中构造函数传参数在基本数据类型和引用类型之间的区别

    Java中构造函数传参数在基本数据类型和引用类型的区别 如果构造函数中穿的参数为基本数据类型,如果在函数中没有返回值,在调用的时候不会发生改变:而如果是引用类型,改变的是存储的位置,所有不管有没有返回 ...

  8. springMVC中接收请求参数&&数据转发

    ### 1. 接收请求参数 #### 1.1. [不推荐] 通过HttpServletRequest获取请求参数 假设存在: <form action="handle_login.do ...

  9. 【ASP.NET Core】给中间件传参数的方法

    最近博客更新频率慢了些,原因有三: 其一,最近老周每星期六都录 ASP.NET Core 的直播,有些内容在视频里讲过,就不太想在博客里面重复.有兴趣的话可以去老周的微博看,或者去一直播,直播帐号与微 ...

随机推荐

  1. BaseAnimation是基于开源的APP,致力于收集各种动画效果(最新版本1.3) (转)

    声明:部分动画来源于网络,本人只是想方便收集在一起,如果不妥请及时与我联系!谢谢 为了统一BaseAnimationApp签名,一定要方便以后大家自动更新...防止签名冲突,不能及时更新 1.3源码下 ...

  2. 不可理喻的JSTL标签库

    JSTL 全名为Java Server Pages Standard Tag Library(JSP Standard Tag Library),它的中文名称为JSP 标准标签函数库. Web 程序开 ...

  3. 第七章 二叉搜索树 (a)概述

  4. wcf 调试

    1>在开发环境中调试,我们先在WCF服务上将服务Serivce1.svc设置为启动页面 然后在WCF上Debug中启动新实例 服务就启动起来了 2>wcf发布以后调试,只需在Visual ...

  5. FIX protocol tutorial : Fix Session is not connecting how to diagnose it ?

    In this blog post of FIX protocol tutorial series I would like to share my experience with connectiv ...

  6. pl/sql登录时,数据库下拉框没有任何内容

    打开plsql时突然发现database下拉框里面没有任何配置信息,如下图: 解决方法: 找到环境变量TNS_ADMIN,修改存放tnsnames.ora的路径:

  7. [Linux] Big-endian and Little-endian (大小端模式)

    Big-endian Little-endian 大小端模式   https://en.wikipedia.org/wiki/Endianness 大端模式,是指数据的高字节保存在内存的低地址中,而数 ...

  8. 20155230 2016-2017-2 《Java程序设计》第七周学习总结

    20155230 2016-2017-2 <Java程序设计>第6周学习总结 教材学习内容总结 世界时:在1972年引入UTC之前,GMT与UT是相同的 格林威治标准时间(GMT),现已不 ...

  9. 14 Finding a Shared Motif

    Problem A common substring of a collection of strings is a substring of every member of the collecti ...

  10. NAOQI API之学习笔记

    https://www.jianshu.com/p/e84f38e45bf5 NAOQI OS是软银pepper和nao机器人的核心操作系统,NAOQI API提供了访问机器人的各种传感器设备接口以及 ...