Bar*_*bas 1 jsp jspx spring-mvc apache-tiles
我在我的系统中使用Spring MVC + Apache Tiles + JSPX开发.
我想在每个页面中放入登录表单.在登录过程中,我想使用@ModelAttribute("loginModel")LoginDTO
在Spring docs页面中,我必须在每个控制器中定义一个方法,我希望在这里显示该模型:
@ModelAttribute("loginModel")
public LoginDTO loginModel() {
return new LoginDTO();
}
Run Code Online (Sandbox Code Playgroud)
有没有办法我只定义一个控制器,并在我写入登录表单jsp的每个页面中工作?
谢谢你的answare!
如果您使用的是Spring 3.2或更高版本,则可以使用新的@ControllerAdvice批注.从@ModelAttribute的文档:
@ModelAttribute方法也可以在@ControllerAdvice-annotated类中定义,并且此类方法适用于所有控制器.@ControllerAdvice注释是一个组件注释,允许通过类路径扫描自动检测实现类.
在你的情况下,我想它看起来像:
@ControllerAdvice
public class GlobalControllerAdvice {
@ModelAttribute("loginModel")
public LoginDTO loginModel() {
return new LoginDTO();
}
}
Run Code Online (Sandbox Code Playgroud)