This issue seems trivial, but I can’t get it to work properly. I’m calling my Spring controller mapping with jquery ajax. The value for someAttr is always empty string regardless of the value in the url. Please help me determine why.
-URL called
http://localhost:8080/sitename/controllerLevelMapping/1?someAttr=6
-Controller Mapping
@RequestMapping(value={"/{someID}"}, method=RequestMethod.GET)
public @ResponseBody int getAttr(@PathVariable(value="someID") final String id,
@ModelAttribute(value="someAttr") String someAttr) {
//I hit some code here but the value for the ModelAttribute 'someAttr' is empty string. The value for id is correctly set to "1".
}
You should be using
@RequestParaminstead of@ModelAttribute, e.g.You can even omit
@RequestParamaltogether if you choose, and Spring will assume that’s what it is: