Warren Taggart
Warren Taggart

Reputation: 75

How are these params used in @RequestMapping

I have inherited code which I am trying to figure out.

I have a Controller class for registering a user with the following method

@RequestMapping(method = POST, params = {USERNAME_PARAM, "!" + EMAIL_PARAM})

public ModelAndView usernameRegister(@Valid RegisterWithUsernameParameter usernameParameter, ModelMap model) 


Where USERNAME_PARAM = "username" and EMAIL_PARAM = "email". I also have this method:

@RequestMapping(method = POST, params = {EMAIL_PARAM, "!" + USERNAME_PARAM})

public ModelAndView emailRegister(@Valid AuthenticateWithEmailParameter emailParameter, ModelMap model)

My question is, what is the purpose of the "params = " ? I have removed and tested and I can't see any difference, it still works. Also what does "!" mean in this context?

Thanks!

Upvotes: 1

Views: 49

Answers (2)

Jigish
Jigish

Reputation: 1784

params = {} attribute specifies additional conditions under which a particular method will be called.

usernameRegister() method will only be called if USERNAME_PARAM is present and EMAIL_PARAM is NOT present in the request.

emailRegister() method will only be called if EMAIL_PARAM is present and USERNAME_PARAM is NOT present in the request.

Some useful links: Javadoc for params, Example using params attribute

Upvotes: 1

αƞjiβ
αƞjiβ

Reputation: 3246

Using 'params' we can narrow the request. And '!' negate the expression. So first method (userNameRegister) is looking for reuqets with USERNAME_PARAM but not EMAIL_PARAM whereas second one (emailRegister) is lookign for EMAIL_PARAM but not an USERNAME_PARAM

Upvotes: 0

Related Questions