Using @RequestParam to pass in HTTP GET parameters in Spring 3 Web MVC

Receiving HTTP GET parameters in Spring is made very easy using the @RequestParam annotation.

Three different versions of the same method here.

The first takes no parameters and is accessible form the follwoing url:

Parameters can still be passed in and retrieved using:

where id may be Null.

We use the @RequestParam annotation in the other two methods to simplify things.

The ‘value’ attribute is the name of the parameter e.g. /paramdemo?id=2. id is the name.

The ‘required’ attribute determines weather the parameter is optional or not. If set to ‘true’ then the parameter is required. I.e. in the absence of the parameter in the URL, this method will not be invoked. The result may be a page not-found-error (well actually it results in a HTTP 400 error, “syntactically incorrect URL”.

Testing for Null reveals if the parameter was passed in or not as demonstrated in the third method ‘paramDemo2’

 

This can be tested using the following jsp code:

 

Hope this was helpful, comments always welcome.

See also @RequestMapping friendly URL parameters where

/paramdemo?id=2&other=otherParam

becomes

/paramdemo/2/otherParam