Spring MVC customized method parameter binding

Viewed 17901

I'm looking for a way to customize the default Spring MVC parameter binding. Take this method as an example:

@RequestMapping(value="/index.html")
public ModelAndView doIndex(@RequestParam String param) {
  ...

This is easy, when I have just a Stringthat I want to extract from the request. However, I want to populate a more complete object, so that my method looks like this:

@RequestMapping(value="/index.html")
public ModelAndView doIndex(Foo bar) {
  ...

What I'm looking for is some way to declare a binding like this;

@RequestMapping(value="/index.html")
public ModelAndView doIndex(@FooPopulator Foo bar) {
  ...

And have some other kind of implementor (determined by the @FooPopulator annotation) that does this:

public void doBind(Foo target, ServletRequest originalRequest) {
  target.setX(this.computeStuffBasedOn(originalRequest));
  target.sety(y);
}

So far I've found out about the @InitBinderbinder annotaion but I'm unsure whether that's really the right choice for this scenarion.

What's the best way?

2 Answers
Related