I\'ve been retrofitting an existing webapp with Spring. Clearly it\'s easier to start with Spring than to add it on later.
We have servlets that can take multiple reque
The RequestMapping annotation tells Spring which URL requests to map to your controller. You can put the value either at the method level or at the class level.
In your example, nothing differs between the two request mappings. You can do this, though:
@RequestMapping(value="/bar/example.htm", method={RequestMethod.GET}, params={"prod", "owner"})
public String doIt( @RequestParam("prod") int prod,
@RequestParam("owner") int owner) {
LOGGER.trace("in doIt(int,int)");
return "foo/bar";
}
@RequestMapping(value="/bar/example.htm", method={RequestMethod.GET}, params={"prod"})
public String doIt( @RequestParam("prod") int prod) {
LOGGER.trace("in doIt(int)");
return "foo/bar";
}
You can even share a model between them if you want:
@Model
public static Map<String,Object> model() {
LOGGER.trace("in model()");
Map<String,Object> model = new HashMap<>();
model.put("hello", "hello world");
return model;
}
Your request mapping needs to map an actual URL rather then just the HTTP method. You can also put required params on a per method basis like so...
@RequestMapping(value="/doSomething", method=RequestMethod.GET, params=["prod", "owner"])
public ModelAndView doIt(@RequestParam("prod") int prod,
@RequestParam("owner") int owner,
Model model)
{
ModelAndView mav = new ModelAndView();
mav.setViewName("jsonView");
return mav;
}