SpringMVC如何用Post方式重定向
SpringMVC用Post方式重定向
正常會以return "redirect:/XXX"這種方式直接重定向,但是這種方式是Get方式提交。
然而有些業(yè)務(wù)為了安全性必須要Post方式重定向。
Post方式重定向
我嘗試過的方法:
? ? /** ? ? ? ?* 請求進(jìn)行重定向 ? ? ? ?*/ ? ? ? @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: 后臺進(jìn)行POST請求(請寫在代碼執(zhí)行結(jié)尾) ?? ? * @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、在控制層直接調(diào)用
/** ?? ? * 進(jìn)行請求 ?? ? * @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("-----進(jìn)入了doPostRedirectView----"); ?? ??? ?map.put("aaa", "aaaa"); ?? ??? ?HttpUtils.doBgPostReq(response, "doPostReq.do", map); ?? ?}
SpringMVC的Post提交405錯誤,只能使用重定向方式
前端文件
? <form action="welcome1" method="post"> ? ? ? ? <input type="submit" value="post-請求轉(zhuǎn)發(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"; ? ? ? ?//默認(rèn)使用請求轉(zhuǎn)發(fā) ? ? } ? ? @RequestMapping(path = "welcome2",method = RequestMethod.POST) ? ? public String welcome2(){ ? ? ? ? return ?"redirect:success.html"; ? ? ? ?//使用重定向 ? ? } }
使用@PostMapping注解的方式也一樣。
配置類
# 應(yīng)用名稱 spring.application.name=sringmvc-blog # 應(yīng)用服務(wù) WEB 訪問端口 server.port=8081 # 后綴名 spring.mvc.view.suffix=.html
要跳轉(zhuǎn)的success.html頁面
<h1>springMVC 的 post</h1>
運(yùn)行頁面:
結(jié)果
請求轉(zhuǎn)發(fā)的方式:
重定向的方式:
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot整合Redis實(shí)現(xiàn)緩存分頁數(shù)據(jù)查詢功能
類似淘寶首頁,這些商品是從數(shù)據(jù)庫中查出來的嗎,答案肯定不是,本文我們就通過一個案例實(shí)操一下,首頁熱點(diǎn)數(shù)據(jù)怎么放到Redis中去查詢,感興趣的同學(xué)可以參考一下2023-06-06spring boot加載第三方j(luò)ar包的配置文件的方法
本篇文章主要介紹了spring boot加載第三方j(luò)ar包的配置文件的方法,詳細(xì)的介紹了spring boot jar包配置文件的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10解決調(diào)用ftpClient.retrieveFileStream(String?remoteFilePath)第二次讀
這篇文章主要給大家介紹了關(guān)于如何解決調(diào)用ftpClient.retrieveFileStream(String?remoteFilePath)第二次讀取為空問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-08-08SpringBoot+WebSocket搭建簡單的多人聊天系統(tǒng)
WebSocket是一種在單個TCP連接上進(jìn)行全雙工通信的協(xié)議。這是一種比較官方的說法,簡單點(diǎn)來說就是,在一次TCP連接中,通信的雙方可以相互通信。這篇文章主要介紹了SpringBoot+WebSocket搭建簡單的多人聊天系統(tǒng),需要的朋友可以參考下2019-10-10