使用 Spring MVC,有没有办法分解org.springframework.ui.Model
,以便不必在任何控制器的方法参数中指定它?
换句话说,我目前正在这样做:
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";
}
}
我想这样做:
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";
}
}
但是在调用 URL 时,会抛出异常:
...Could not autowire field: protected org.springframework.ui.Model...
...No matching bean of type [org.springframework.ui.Model] found for dependency...
我在使用时遇到了同样的错误org.springframework.ui.ModelMap
。
有什么天才的解决方法吗?
感谢您的帮助:)