I have been trying to use a path variable in grails controller but I am not able to achieve it. The intention behind is to validate the parameter submitted to the url which I need to make mandatory. I could not achieve it through RequestParam so I switched to PathVariable so that the url submitted without the required param should be filtered off by grails controller itself rather than me adding if/else checks for validity.
So, I can illustrate as below: My URL is something as below:-
'<appcontext>/<controller>/<action>?<paramName>=<something>'
Now, to make 'paramName' mandatory I am not finding any way in Grails(Spring MVC provides @RequestParam annotation which can enable me for 'required' as true).
Another alternative I thought was to use path variables so that 'paramName' can be included in URL itself. So I tried like following:
'<appcontext>/<controller>/<action>/$paramName'
For validating the above URL I wrote specific mapping but some how it does not work too..
Following is the specific mapping I wrote:-
"/<controllerName>/<action>/$paramName" {
controller:<controller to take request>
action:<action to do task>
constraints {
paramName(nullable: false,empty:false, blank: false)
}
}
I tried to use spring annotation like @PathVariable and @RequestParam in controller as given below:-
def action(@PathVariable("paramName") String param){
//code goes here
}
"$controller/$action?/$id?"
mapping) then it may be that your constraints are in fact working correctly, rejecting that particular mapping, but the same controller is being called through a different mapping instead. – Joniejoninanullable: false, empty: false, blank: false
doesn't really do anything. Since the mapping does not include a?
, the parameter has to be present in order for this mapping to apply. You can eliminate those constraints. – Conant