I have a referrer URL like this:
http://myUrl.com?page=thisPage&gotoUrl=https://yahoo.com?gotoPage
How do I get the Values of "page" and "gotoUrl" in my Spring Controller?
I want to store these values as variables, so I can reuse them later.
How do you retrieve those parameters in the code ? The URL parameter is enclosed in braces in the relative path passed to @GetMapping annotation. The URL parameter is then retrieved using @PathVariable annotation which takes the variable indicated in enclosed braces as a parameter.
Simply put, we can use @RequestParam to extract query parameters, form parameters, and even files from the request.
Query parameters are passed after the URL string by appending a question mark followed by the parameter name , then equal to (“=”) sign and then the parameter value. Multiple parameters are separated by “&” symbol.
1) The @RequestParam is used to extract query parameters while @PathVariable is used to extract data right from the URI.
In SpringMVC you can specify values from the query string be parsed and passed in as method parameters with the @RequestParam annotation.
public ModelAndView getPage( @RequestParam(value="page", required=false) String page, @RequestParam(value="gotoUrl", required = false) String gotoUrl) { }
You can use the getParameter() method from the HttpServletRequest interface.
For example;
public void getMeThoseParams(HttpServletRequest request){ String page = request.getParameter("page"); String goToURL = request.getParameter("gotoUrl"); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With