SpringMVC中如何通过POST请求实现页面重定向?

2026-05-26 00:171阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计705个文字,预计阅读时间需要3分钟。

SpringMVC中如何通过POST请求实现页面重定向?

目录+ SpringMVC使用Post方式重定向+ Post方式重定向+ 以上方法并不好用+ 我看了作者写的另一个解决方案+ SpringMVC的Post提交405错误,只能使用重定向方式+ 前端文件+ 后端控制器类+ 配置类+ 结果

目录
  • SpringMVC用Post方式重定向
    • Post方式重定向
    • 以上方法并不好用,我看了作者写的另一个解决办法
  • SpringMVC的Post提交405错误,只能使用重定向方式
    • 前端文件
    • 后端控制器类
    • 配置类
    • 结果

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>

运行页面:

结果

请求转发的方式:

重定向的方式:

以上为个人经验,希望能给大家一个参考,也希望大家多多支持自由互联。

SpringMVC中如何通过POST请求实现页面重定向?

本文共计705个文字,预计阅读时间需要3分钟。

SpringMVC中如何通过POST请求实现页面重定向?

目录+ SpringMVC使用Post方式重定向+ Post方式重定向+ 以上方法并不好用+ 我看了作者写的另一个解决方案+ SpringMVC的Post提交405错误,只能使用重定向方式+ 前端文件+ 后端控制器类+ 配置类+ 结果

目录
  • SpringMVC用Post方式重定向
    • Post方式重定向
    • 以上方法并不好用,我看了作者写的另一个解决办法
  • SpringMVC的Post提交405错误,只能使用重定向方式
    • 前端文件
    • 后端控制器类
    • 配置类
    • 结果

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>

运行页面:

结果

请求转发的方式:

重定向的方式:

以上为个人经验,希望能给大家一个参考,也希望大家多多支持自由互联。

SpringMVC中如何通过POST请求实现页面重定向?