SpringMVC如何用Post方式重定向
更新時間:2022年10月26日 10:19:15 作者:賀總的好兄弟
這篇文章主要介紹了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);//最后的參數為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ā)的方式:

重定向的方式:

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
解決調用ftpClient.retrieveFileStream(String?remoteFilePath)第二次讀
這篇文章主要給大家介紹了關于如何解決調用ftpClient.retrieveFileStream(String?remoteFilePath)第二次讀取為空問題的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2023-08-08
SpringBoot+WebSocket搭建簡單的多人聊天系統
WebSocket是一種在單個TCP連接上進行全雙工通信的協議。這是一種比較官方的說法,簡單點來說就是,在一次TCP連接中,通信的雙方可以相互通信。這篇文章主要介紹了SpringBoot+WebSocket搭建簡單的多人聊天系統,需要的朋友可以參考下2019-10-10

