Spring MVC定制方法参数绑定


问题内容

我正在寻找一种自定义默认Spring MVC参数绑定的方法。以这种方法为例:

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

当我只有一个String要从请求中提取的内容时,这很容易。但是,我想填充一个更完整的对象,以便我的方法如下所示:

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

我正在寻找某种方式来声明这样的绑定;

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

并具有执行此操作的其他某种实现器(由@FooPopulator批注确定):

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

到目前为止,我已经找到了有关@InitBinder活页夹注释的信息,但是我不确定这是否真的是此方案的正确选择。

最好的方法是什么?


问题答案:

谢谢您和我的信息,我已经找到问题的“正确”解决方案。Spring已经WebArgumentResolver为此场景提供了。

http://sergialmar.wordpress.com/2011/03/29/extending-handler-method-argument-
resolution-in-spring-
mvc/

http://scottfrederick.blogspot.com/2011/03/customizing-
spring-3-mvcannotation.html