Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring interface as @ModelAttribute param

I have UsersController with method:

@RequestMapping(value={"/new"}, method=RequestMethod.GET)
public String showCreationForm(@ModelAttribute User user){
    return "user_registration_form";
}

which displays registration form. I want to keep modularity (would be nice to use this controller in some other project) in my project so User is an interface and there is its implementation - UserImpl. The problem is that Spring cannot instatiate User interface. Is there a way to configure spring to use some default implementation of User?

like image 540
Bladositto Avatar asked Oct 06 '22 19:10

Bladositto


1 Answers

You can provide an object to be populated with request data using @ModelAttribute-annotated method:

@ModelAttribute
public User createUser() {
    return new UserImpl();
}
like image 61
axtavt Avatar answered Oct 13 '22 05:10

axtavt