springmvc重定向请求

SpringMVC重定向传参数的实现(来自网友)

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

原文地址:https://www.cnblogs.com/zhizhuoDEZHUZHU/p/9602486.html

时间: 2024-08-02 07:31:19

springmvc重定向请求的相关文章

Spring 梳理-跨重定向请求传递数据-Flash

Spring MVC Flash Attribute 的讲解与使用示例 1. Spring MVC 3.1版本加了一个很有用的特性,Flash属性,它能解决一个长久以来缺少解决的问题,一个POST/Redirect/GET模式问题. 正常的MVC Web应用程序在每次提交都会POST数据到服务器.一个正常的Controller (被注解 @Controller标记)从请求获取数据和处理它 (保存或更新数据库).一旦操作成功,用户就会被带到(forward)一个操作成功的页面.传统上来说,这样的P

SpringMVC重定向视图RedirectView小分析

目录 前言 RedirectView介绍 实例讲解 总结 前言 SpringMVC是目前主流的Web MVC框架之一. 如果有同学对它不熟悉,那么请参考它的入门blog:http://www.cnblogs.com/fangjian0423/p/springMVC-introduction.html 本文所讲的部分内容跟SpringMVC的视图机制有关,SpringMVC的视图机制请参考楼主的另一篇博客: http://www.cnblogs.com/fangjian0423/p/springM

Java Servlet(九):转发请求与重定向请求区别

转发: <% pageContext.setAttribute("pageContextAttr", "pageContextAttribute"); request.setAttribute("requestAttr", "requestAttribute"); session.setAttribute("sessionAttr", "sessionAttribute"); ap

IOS拦截重定向请求(302)的几种方式

前言 在多数情况下,我们做的网络请求是返回200状态码的,但也有返回302的时候,比如使用基于Oauth2认证协议的API时,在认证阶段,需要提供一个回调地址,当用户授权后,服务器会返回一个302 Response,Response Header中会一个Location字段,包含了我们的回调地址,同时会有一个Code参数.我们在程序中该如何处理这个请求,并拿到这个Code参数呢.下面由我来为大家讲解下几种方式的做法,各取所需. 假设您知道并使用过Oauth2认证协议 (一)UIWebView控件

springMvc REST 请求和响应

前言: 突然怎么也想不起来  springMvc REST 请求的返回  类型了!   (尴尬+究竟)  然后本着 方便的想法 百度了一下 发现了个问题,大家在写      springMvc REST 的文档 和记录或者笔记的时候 . 只写到了 符合 REST 风格的 请求 ,而 响应信息 完全是 传统的 主体数据传递 .然而这并非 REST响应的 风格啊 !!!! 下面把自己整理的笔记发上来 关于MVC REST 请求的方式 格式1 @RequestMapping(value = "{ite

SpringMVC Ajax请求时返回json中文字符串的乱码问题的解决方案

1.org.springframework.http.converter.StringHttpMessageConverter类是处理请求或相应字符串的类,并且默认字符集为ISO-8859-1,所以在当返回json中有中文时会出现乱码. 2.StringHttpMessageConverter的父类里有个List<MediaType> supportedMediaTypes属性,用来存放StringHttpMessageConverter支持需特殊处理的MediaType类型,如果需处理的Me

SpringMVC之请求参数的获取方式

转载出处:https://www.toutiao.com/i6510822190219264516/ SpringMVC之请求参数的获取方式 常见的一个web服务,如何获取请求参数? 一般最常见的请求为GET和POST,get请求的参数在url上可以获取,post请求参数除了url上还有可能在表单中,文件上传时,获取方式又和一般的参数获取不一样 本篇则主要集中在不同请求方式下,获取参数的使用姿势 首先需要搭建一个后端的请求,为了快速演示 利用spring-boot创建了一个机器简单的工程,依赖版

重定向请求

# coding:utf-8import requests# 请求头headers = {     "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0"          }s = requests.session()# 打开我的随笔r = s.get('https://i.cnblogs.com/EditPosts.aspx?opt=1',

Java学习路线分享SpringMVC之请求和响应

Java学习路线分享SpringMVC之请求和响应,前面我们学习了SpringMVC的基本配置,接下来一个非常重要的知识点是如何接受用户的请求以及如何将数据发送给用户. 获得请求参数 获得页面参数的几种方式 1)通过参数名获得 给控制器的方法设置参数名和表单name相同 2)通过@RequestParam("参数名")注解设置参数 @RequestParam("表单元素的name") 参数类型 参数名 3)自动装箱,创建属性名和表单名称一样的类 把类作为方法的参数