[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);
	}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值