Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Query String Values in Spring MVC Controller

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.

like image 439
Jake Avatar asked Jul 29 '13 21:07

Jake


People also ask

How do I retrieve query parameters in a Spring boot controller?

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.

How do I get Spring query parameters?

Simply put, we can use @RequestParam to extract query parameters, form parameters, and even files from the request.

How do I pass a query parameter in Spring boot REST API?

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.

What is difference between @RequestParam and @PathVariable?

1) The @RequestParam is used to extract query parameters while @PathVariable is used to extract data right from the URI.


2 Answers

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) { } 
like image 78
Affe Avatar answered Sep 28 '22 07:09

Affe


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"); } 
like image 20
Raunak Agarwal Avatar answered Sep 28 '22 05:09

Raunak Agarwal