Using Spring MVC, is there any way to factorize the org.springframework.ui.Model, in order to not to have to specify it in the method parameters within any controller?
In other words, I’m currently doing it like this:
public abstract class AbstractController {
@Autowired
protected MultipartHttpServletRequest request;
}
@Controller
public class SigninController extends AbstractController {
@RequestMapping(value = "/signin", method = RequestMethod.GET)
public String signin(@ModelAttribute User user, Model model) {
// do stuff with user (parameter)
// do stuff with model (parameter) <--
// do stuff with request (attribute)
return "/signin/index";
}
}
And I would like to do like that:
public abstract class AbstractController {
@Autowired
protected MultipartHttpServletRequest request;
@Autowired
protected Model model;
}
@Controller
public class SigninController extends AbstractController {
@RequestMapping(value = "/signin", method = RequestMethod.GET)
public String signin(@ModelAttribute User user) {
// do stuff with user (parameter)
// do stuff with model (attribute) <--
// do stuff with request (attribute)
return "/signin/index";
}
}
But when calling the URL, an exception is thrown:
...Could not autowire field: protected org.springframework.ui.Model...
...No matching bean of type [org.springframework.ui.Model] found for dependency...
I got the same error when using org.springframework.ui.ModelMap.
Any solution of genious?
Thanks for helping 🙂
I finally found a solution. I’m not sure the game is worth it, but it works 🙂
First, add those stuff in your
AbstractController:Then, create an interceptor implementing
org.springframework.web.servlet.HandlerInterceptorlike this one:Finally, add these lines in your
applicationContext.xml:And of course, make your controllers implementing your
AbstractController.Here it is! You need to specify neither your
requestnor yourmodelwithin your controllers methods parameters anymore 🙂 I’m not really convinced of the usefulness of that trick though, but yeah. If it can make maniacal developers happier 🙂Still open to easier solutions though.