【问题标题】:It's possible to send hidden parameters in HttpServletResponse.sendRedirect()?可以在 HttpServletResponse.sendRedirect() 中发送隐藏参数吗?
【发布时间】:2019-04-08 04:50:32
【问题描述】:

我有一个带有控制器的 Spring Boot API(无状态),它接收 POST 请求,提取 POST 请求的参数以通过 GET 将它们发送到我的 Angular 客户端。我的问题是是否可以在 HttpServletResponse.sendRedirect() 中发送隐藏参数?

目前我有的是这个,但我不想在浏览器中显示参数...

@RequestMapping(value = "/return", method = RequestMethod.POST, headers = "Accept=text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8")
    @ResponseBody
    @Transactional
    public void returnData(UriComponentsBuilder uriComponentsBuilder, final HttpServletRequest request,
            final HttpServletResponse response) throws IOException {

        String parameter=request.getParameter("billCode");

        response.sendRedirect("http://localhost:4200/payment?parameterOne="+parameter);
    }

更新:

我不能使用HttpSession session = request.getSession(false); 然后session.setAttribute("helloWorld", "Hello world") 因为sessionNull

非常感谢!

【问题讨论】:

标签: java http spring-boot controller


【解决方案1】:

您可以使用 HTTP 响应标头而不是在 queryString 中发送参数。一个例子:

@GetMapping(value="/")
public void init(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String billCode = request.getParameter("billCode");
    response.addHeader("parameterOne", billCode);
    response.sendRedirect("http://localhost:4200/payment");
}

从请求中获取值:

String billCode = request.getHeader("parameterOne");

或者,如果您使用 jQuery 从 ajax 获取:

$.ajax({
    url:'/api/v1/payment'
}).done(function (data, textStatus, xhr) { 
    console.log(xhr.getResponseHeader('parameterOne')); 
});

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2014-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多