springmvc重定向请求。
SpringMVC重定向传参数的实现(来自网友)
验证了我说的,从model层中拿来的数据,不管什么类型,都是通过隐含模型,中转,放入request中的。除非你特意把这些数据放到session域中。
在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在跳到页面后马上移除对象。所以你刷新一下后这个值就会丢失。 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.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重定向请求。的更多相关文章
- Spring 梳理-跨重定向请求传递数据-Flash
Spring MVC Flash Attribute 的讲解与使用示例 1. Spring MVC 3.1版本加了一个很有用的特性,Flash属性,它能解决一个长久以来缺少解决的问题,一个POST/R ...
- 什么时候会进行 SpringMVC重定向保存参数(FlashMap)?
SpringMVC重定向保存参数(FlashMap):两种情况会保存参数: 1. 当前视图为RedirectView,也即是说当前请求为重定向请求. org.springframe ...
- 学习SpringMVC必知必会(3)~springmvc的请求和响应
一.处理器方法响应处理 ▷ Controller方法该怎么返回.Controller数据该怎么进行共享 返回void/ModelAndView/String 1.Controller方法返回void ...
- Java Servlet(九):转发请求与重定向请求区别
转发: <% pageContext.setAttribute("pageContextAttr", "pageContextAttribute"); r ...
- SpringBoot对比SpringMVC,SpringMVC 处理请求过程
(问较多:1.SpringBoot对比SpringMVC.2.SpringMVC 处理请求过程.问:springboot的理解 Spring,Spring MVC,Spring Boot 三者比较 S ...
- springMvc REST 请求和响应
前言: 突然怎么也想不起来 springMvc REST 请求的返回 类型了! (尴尬+究竟) 然后本着 方便的想法 百度了一下 发现了个问题,大家在写 springMvc RES ...
- SpringMvc Controller请求链接忽略大小写(包含拦截器)及@ResponseBody返回String中文乱码处理
SpringMvc Controller请求链接忽略大小写(包含拦截器)及@ResponseBody返回String中文乱码处理... @RequestMapping(value = "/t ...
- 细说Redirect重定向请求(情节分享)
前些日子在开发公司项目接口的时候,由于需要与第三方平台对接,由于接口之前的层层封装,不断的需要转发,把人差点搞糊涂了.本来以为之前对Redirect的认识足够清楚,可是到实际开发之前我还是没 ...
- SpringMVC之请求参数的获取方式
转载出处:https://www.toutiao.com/i6510822190219264516/ SpringMVC之请求参数的获取方式 常见的一个web服务,如何获取请求参数? 一般最常见的请求 ...
随机推荐
- 转 【<meta name="description" content=">】作用讲解
今天在看别人写的网站代码,发现类似<meta name="Keywords" content="" >.<meta name="De ...
- 一次完整的HTTP事务过程
基本流程: a. 域名解析 b. 发起TCP的3次握手 c. 建立TCP连接后发起http请求 d. 服务器端响应http请求,浏览器得到html代码 e. 浏览器解析html代码,并请求html代码 ...
- Java实现队列结构的详细代码
一.什么是队列结构 一种线性结构,具有特殊的运算法则[只能在一端(队头)删除,在另一端(队尾)插入]. 分类: 顺序队列结构 链式队列结构 基本操作: 入队列 出队列 二.准备数据 static fi ...
- Android layout 布局 属性详解
第一类:属性值 true或者 false android:layout_centerHrizontal 水平居中 android:layout_centerVertical ...
- qt中的lineEdit文本输入框的输入类型限制(三种验证类)
qt的三种验证类: 1.输入int类型 QValidator *validator=new QIntValidator(100,999,this): QLineEdit *edit=new QLine ...
- oracle 表 视图 存储过程 序列 job
table 表 --delete table drop table Test1; -- Create table create table TEST1 ( ID NUMBER, T_N ...
- UVA - 11488 前缀
题目链接:https://vjudge.net/contest/166647#problem/A 题意: 从一些字符串集合里面挑一子集,然后公共前缀长度*字符串个数最大: 分析: 将这些字符串放到一个 ...
- NYOJ298 点的变换 【矩阵乘法经典】
任意门:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=298 点的变换 时间限制:2000 ms | 内存限制:65535 KB 难度:5 ...
- HDU 5723 Abandoned country 【最小生成树&&树上两点期望】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=5723 Abandoned country Time Limit: 8000/4000 MS (Java/ ...
- 【luogu P3808 AC自动机(简单版)】 模板
题目链接:https://www.luogu.org/problemnew/show/P3808 #include <queue> #include <cstdio> #inc ...