SpringMVC如何用Post方式重定向
SpringMVC用Post方式重定向
正常會(huì)以return "redirect:/XXX"這種方式直接重定向,但是這種方式是Get方式提交。
然而有些業(yè)務(wù)為了安全性必須要Post方式重定向。
Post方式重定向
我嘗試過(guò)的方法:
? ? /** ?
? ? ?* 請(qǐng)求進(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方式提交請(qǐng)求 ??
? ? } ?以上方法并不好用,我看了作者寫的另一個(gè)解決辦法
1、先封裝一個(gè)Form,用他來(lái)發(fā)Post請(qǐng)求。
/**
?? ? * @Description: 后臺(tái)進(jìn)行POST請(qǐng)求(請(qǐng)寫在代碼執(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)行請(qǐng)求
?? ? * @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錯(cuò)誤,只能使用重定向方式
前端文件
? <form action="welcome1" method="post"> ? ? ? ? <input type="submit" value="post-請(qǐng)求轉(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)使用請(qǐng)求轉(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 訪問(wèn)端口 server.port=8081 # 后綴名 spring.mvc.view.suffix=.html
要跳轉(zhuǎn)的success.html頁(yè)面
<h1>springMVC 的 post</h1>
運(yùn)行頁(yè)面:

結(jié)果
請(qǐng)求轉(zhuǎn)發(fā)的方式:

重定向的方式:

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
定時(shí)任務(wù)注解@Scheduled不生效問(wèn)題及解決
這篇文章主要介紹了定時(shí)任務(wù)注解@Scheduled不生效問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
SpringBoot整合Redis實(shí)現(xiàn)緩存分頁(yè)數(shù)據(jù)查詢功能
類似淘寶首頁(yè),這些商品是從數(shù)據(jù)庫(kù)中查出來(lái)的嗎,答案肯定不是,本文我們就通過(guò)一個(gè)案例實(shí)操一下,首頁(yè)熱點(diǎn)數(shù)據(jù)怎么放到Redis中去查詢,感興趣的同學(xué)可以參考一下2023-06-06
spring boot加載第三方j(luò)ar包的配置文件的方法
本篇文章主要介紹了spring boot加載第三方j(luò)ar包的配置文件的方法,詳細(xì)的介紹了spring boot jar包配置文件的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
解決調(diào)用ftpClient.retrieveFileStream(String?remoteFilePath)第二次讀
這篇文章主要給大家介紹了關(guān)于如何解決調(diào)用ftpClient.retrieveFileStream(String?remoteFilePath)第二次讀取為空問(wèn)題的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-08-08
springcloud-feign調(diào)用報(bào)錯(cuò)問(wèn)題
這篇文章主要介紹了springcloud-feign調(diào)用報(bào)錯(cuò)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
SpringBoot+WebSocket搭建簡(jiǎn)單的多人聊天系統(tǒng)
WebSocket是一種在單個(gè)TCP連接上進(jìn)行全雙工通信的協(xié)議。這是一種比較官方的說(shuō)法,簡(jiǎn)單點(diǎn)來(lái)說(shuō)就是,在一次TCP連接中,通信的雙方可以相互通信。這篇文章主要介紹了SpringBoot+WebSocket搭建簡(jiǎn)單的多人聊天系統(tǒng),需要的朋友可以參考下2019-10-10

