Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
186 views
in Technique[技术] by (71.8m points)

java - Spring MVC customized method parameter binding

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It is very easy. You can use Converters (that work like one way PropertyEditors but are stateless).

See chapter 5.5 Spring 3 Type Conversion in Spring reference.

If such an converter is registered once, you do not need any additional information, you can simply use

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

For example a simple converter that load an object by its id:

@Component
@CustomConverter //custom qualifyer
public class BUdToUserConverter implements Converter<String, User> {

    @Resource
    private UserDao userDao;

    @Override
    public User convert(String source) {
        Integer id = Integer.parse(source);
        return this.userDao.getByBusinessId(id);
    }
}

A "helper" that registers all Beans with @CustomConverter anntoation

public class ApplicationConversionServiceFactoryBean extends FormattingConversionServiceFactoryBean {

    @Resource
    @CustomConverter
    private List<Converter<?, ?>> customConverter;

     @Override
    protected void installFormatters(final FormatterRegistry registry) {
        super.installFormatters(registry);

        for (Converter<?, ?> converter : customConverter) {
            registry.addConverter(converter);
        }
    }
}

How to use it

UserController {
...
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public ModelAndView show(@PathVariable("id") User user) {        
        return new ModelAndView("users/show", "user", user);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...