SpringMVC如何用Post方式重定向
SpringMVC用Post方式重定向
正常會以return "redirect:/XXX"這種方式直接重定向,但是這種方式是Get方式提交。
然而有些業(yè)務為了安全性必須要Post方式重定向。
Post方式重定向
我嘗試過的方法:
? ? /** ?
? ? ?* 請求進行重定向 ?
? ? ?*/ ?
? ? @RequestMapping(value = “postPayAmount”, method = RequestMethod.GET) ??
? ? public RedirectView postPayAmount(HttpSession session,ModelMap map) { ??
? ? ? ? return new RedirectView(WsUrlConf.URI_PAY,true,false,false);//最后的參數(shù)為false代表以post方式提交請求 ??
? ? } ?以上方法并不好用,我看了作者寫的另一個解決辦法
1、先封裝一個Form,用他來發(fā)Post請求。
/**
?? ? * @Description: 后臺進行POST請求(請寫在代碼執(zhí)行結尾)
?? ? * @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-請求轉發(fā)"> ? </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"; ? ? ? ?//默認使用請求轉發(fā)
? ? }
? ? @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>
運行頁面:

結果
請求轉發(fā)的方式:

重定向的方式:

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Spring?Boot數(shù)據(jù)響應問題實例詳解
這篇文章主要給大家介紹了關于Spring?Boot數(shù)據(jù)響應問題的相關資料,文中通過實例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2022-03-03
SpringBoot配置Ollama實現(xiàn)本地部署DeepSeek
本文主要介紹了在本地環(huán)境中使用?Ollama?配置?DeepSeek?模型,并在?IntelliJ?IDEA?中創(chuàng)建一個?Spring?Boot?項目來調用該模型,文中通過圖文示例介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2025-03-03
Springboot接口返回參數(shù)及入?yún)SA加密解密的過程詳解
這篇文章主要介紹了Springboot接口返回參數(shù)及入?yún)SA加密解密,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07
Spring實戰(zhàn)之獲取其他Bean的屬性值操作示例
這篇文章主要介紹了Spring實戰(zhàn)之獲取其他Bean的屬性值操作,結合實例形式分析了Spring操作Bean屬性值的相關配置與實現(xiàn)技巧,需要的朋友可以參考下2019-12-12
Centos6.5下Jdk+Tomcat+Mysql環(huán)境安裝圖文教程
這篇文章主要為大家詳細介紹了Centos6.5系統(tǒng)下Jdk+Tomcat+Mysql環(huán)境安裝過程,感興趣的小伙伴們可以參考一下2016-05-05
SpringBoot和VUE源碼直接整合打包成jar的踩坑記錄
這篇文章主要介紹了SpringBoot和VUE源碼直接整合打包成jar的踩坑記錄,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03
Java中驗證 Mybatis 數(shù)據(jù)分片可以減輕GC壓力的操作方法
這篇文章主要介紹了Java中驗證 Mybatis 數(shù)據(jù)分片可以減輕GC壓力的操作方法,本文使用 Spock(可集成Spring Boot項目) 編寫測試用例,基于 Groovy (JVM語言),感興趣的朋友跟隨小編一起看看吧2024-12-12
java高并發(fā)ScheduledThreadPoolExecutor類深度解析
這篇文章主要為大家介紹了java高并發(fā)ScheduledThreadPoolExecutor類源碼深度解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11

