Question

I have an action method on a Spring MVC controller that has an argument annotated with @ModelAttribute. However, I don't know at compile time what the type of this parameter will be - I know the abstract base type but not the derived type.

At runtime, I will be able to decide what class I am expecting and I will be able to get a new'd up instance of this class. However, I have no idea what code I should be calling to parse the request data in the same fashion that @ModelAttribute does.

I've looked around and it seems that if i can get a hold of a WebRequestDataBinder I can use that to populate my object, but for that I need a BinderFactory and this is where I kind of get lost.

Can anyone give me some pointers here - or tell me that I am looking at it the wrong way and need to do something else?

Was it helpful?

Solution

you can inject the model itself in your controllers method and access the attribute yourself.

@RequestMapping(...)
public void doStuff(ModelMap model) {
    Object attr = model.get("nameOfAttribute");
    // ...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top