简体   繁体   English

Java Spring-如何接受RequestParam并从URL中删除?

[英]Java Spring - How to take in RequestParam, and remove from url?

Sometimes I will want to pass in an identifier to my website through the url, but I don't want to display this to the user. 有时,我想通过URL将标识符传递给我的网站,但是我不想将其显示给用户。 Is there a simple way to take in a request param but not display it to the user when loading the page? 是否有一种简单的方法来接受请求参数,但在加载页面时不将其显示给用户?

This is a general idea of how my code is set up currently 这是目前如何设置我的代码的一般思路

@GetMapping("/somePage")
public ModelAndView get(@RequestHeader HttpHeaders headers,
                        @RequestParam(value = "someId", required = false) String someId) {

I know I could theoretically do this on the javascript side, but that seems to require the page to reload or mess with the history. 我知道从理论上讲我可以在javascript端执行此操作,但这似乎需要页面重新加载或弄乱历史记录。

Generally this is bad practice - if it's passed in the URL, it'll be visible in the user's browser history. 通常,这是一种不好的做法-如果在URL中传递它,则它将在用户的浏览器历史记录中可见。 POST is probably the best practice here. POST可能是此处的最佳实践。

But to answer your actual question: 但是要回答您的实际问题:

Put your custom value into a header and redirect? 将自定义值放入标头并重定向?

Something along these lines (untested) 遵循这些原则(未经测试)

headers.set("X-Custom-Header1", someId);
headers.set("Location", "/newEndpoint");
return new ResponseEntity<>(headers, HttpStatus.FOUND);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM