SpringMVC如何用Post方式重定向
作者:贺总的好兄弟 发布时间:2021-10-05 21:34:27
标签:SpringMVC,Post,重定向
SpringMVC用Post方式重定向
正常会以return "redirect:/XXX"这种方式直接重定向,但是这种方式是Get方式提交。
然而有些业务为了安全性必须要Post方式重定向。
Post方式重定向
我尝试过的方法:
? ? /** ?
? ? ?* 请求进行重定向 ?
? ? ?*/ ?
? ? @RequestMapping(value = “postPayAmount”, method = RequestMethod.GET) ??
? ? public RedirectView postPayAmount(HttpSession session,ModelMap map) { ??
? ? ? ? return new RedirectView(WsUrlConf.URI_PAY,true,false,false);//最后的参数为false代表以post方式提交请求 ??
? ? } ?
以上方法并不好用,我看了作者写的另一个解决办法
1、先封装一个Form,用他来发Post请求。
/**
?? ? * @Description: 后台进行POST请求(请写在代码执行结尾)
?? ? * @return void ?返回类型
?? ? */
?? ?public static void doBgPostReq(HttpServletResponse response,String postUrl,Map<String, ?> paramMap) throws IOException {
?? ??? ?response.setContentType( "text/html;charset=utf-8"); ??
?? ? ? ?PrintWriter out = response.getWriter(); ?
?? ? ? ?out.println("<form name='postSubmit' method='post' action='"+postUrl+"' >"); ?
?? ? ? ?for (String key : paramMap.keySet()) {
?? ? ? ??? ?out.println("<input type='hidden' name='"+key+"' value='" + paramMap.get(key)+ "'>");
?? ??? ?}
?? ? ? ?out.println("</form>"); ??
?? ? ? ?out.println("<script>"); ??
?? ? ? ?out.println(" ?document.postSubmit.submit()"); ??
?? ? ? ?out.println("</script>"); ??
?? ?}
2、在控制层直接调用
/**
?? ? * 进行请求
?? ? * @param request
?? ? * @return
?? ? * @throws IOException?
?? ? */
?? ?@RequestMapping(value = "doPostRedirectView.do", method = RequestMethod.GET)
?? ?@ResponseBody
?? ?public void doPostRedirectView(HttpServletRequest request,HttpServletResponse response,ModelMap map) throws IOException {
?? ??? ?logger.debug("-----进入了doPostRedirectView----");
?? ??? ?map.put("aaa", "aaaa");
?? ??? ?HttpUtils.doBgPostReq(response, "doPostReq.do", map);
?? ?}
SpringMVC的Post提交405错误,只能使用重定向方式
前端文件
? <form action="welcome1" method="post">
? ? ? ? <input type="submit" value="post-请求转发">
? </form>
? <br>
? <br>
? <form action="welcome2" method="post">
? ? ? <input type="submit" value="post-重定向">
? </form>
后端控制器类
@Controller
public class SpringMvcController {
? ? @RequestMapping(path = "welcome1",method = RequestMethod.POST)
? ? public String welcome1(){
? ? ? ? return ?"success"; ? ? ? ?//默认使用请求转发
? ? }
? ? @RequestMapping(path = "welcome2",method = RequestMethod.POST)
? ? public String welcome2(){
? ? ? ? return ?"redirect:success.html"; ? ? ? ?//使用重定向
? ? }
}
使用@PostMapping注解的方式也一样。
配置类
# 应用名称
spring.application.name=sringmvc-blog
# 应用服务 WEB 访问端口
server.port=8081
# 后缀名
spring.mvc.view.suffix=.html
要跳转的success.html页面
<h1>springMVC 的 post</h1>
运行页面:
结果
请求转发的方式:
重定向的方式:
来源:https://blog.csdn.net/CosecantY/article/details/122912059
0
投稿
猜你喜欢
- 本文实例讲述了C++实现的求解多元一次方程。分享给大家供大家参考,具体如下:注:这里计算的是n*n的等距矩阵,代码如下:#include&l
- 主内存和工作内存Java 内存模型规定了所有的变量都存储在主内存中, 每条线程有自己的工作内存线程的工作内存中保存了被该线程使用的变量的主内
- 创蓝253: https://www.253.com/#region 获取手机验证码(创蓝253) /// <summar
- IEnumerable这个接口在MSDN上是这么说的,它是一个公开枚举数,该枚举数支持在非泛型集合上进行简单的迭代。换句话说,对于所有数组的
- C#定义多行字符串的方式在定义的前面加上@符号: string aa = @"asdfsdfsd &n
- 在Springboot中默认的静态资源路径有:classpath:/METAINF/resources/,classpath:/resour
- 1、题目描述找出数组中重复的数字。在一个长度为 n 的数组 nums 里的所有数字都在 0~n-1 的范围内。数组中某些数字是重复的,但不知
- 今天给大家带来一个自定义的底部导航,我不会做动图,只能搞一张图片给大家看看,大家见谅这个就是自定义的tablayout底部搞好的样式了Tab
- 1.任何一门编程语言均有相关数据类型。C#也不例外,其基本数据类型有int,short,long,float,double,string等。
- Java注解(annotation)简单上手反射reflect:https://www.jb51.net/article/221282.ht
- 本文实例讲述了java实现mp3合并的方法。分享给大家供大家参考。具体实现方法如下:package test;import java.io.
- 本文实例讲述了C#简单聊天程序实现方法。分享给大家供大家参考。具体如下:假如有服务器端程序,ChatServer和客户端程序ChatClie
- synchronized是Java里的一个关键字,起到的一个效果是“监视器锁”~~,它的功能就是保证操作的原子性,同时禁止指令重排序和保证内
- 1. 运行环境 Enviroment当 MyBatis 与不同的应用结合时,需要不同的事务管理机制。与 Spring 结合时,由 Sprin
- 一对一查询一对一查询的模型用户表和订单表的关系为,一个用户有多个订单,一个订单只从属于一个用户。一对一查询的需求:查询一个订单,与此同时查询
- 小伙伴私信我说想要研究下Spring的源码,想让我出一期教程来实现IDEA导入Spring源码,今天它来了~版本 :IDEA 2020.2.
- 1. java 执行shelljava 通过 Runtime.getRuntime().exec() 方法执行 shell 的命令或 脚本,
- 概述对List进行分组是日常开发中,经常遇到的,在JDK 8中对List按照某个属性分组的代码,超级简单。package test;impo
- 一、线程的生命周期1.五种状态:新建状态、就绪状态、运行状态、阻塞状态、消亡状态2.就绪状态的线程表示有权利去获取CPU的时间片,CPU时间
- 先对问题进行描述:(1)在MyEclipse Servers视图在MyEclipse 10.7.0在某些情况下,可能无法打开。 试图打开视图