对于拥有全局@ModelAttribute
,我使用的是@ControllerAdvice
,如下所示:
@ControllerAdvice
public class MyControllerAdvice {
@ModelAttribute("globalModelAttribute")
public String getFacebookScope() {
return "FACEBOOK_SCOPE";
}
}
现在我可以访问所有JSP中的globalModelAttribute
,除了那些注册为ViewControllers的JSP,如下所示:
@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("home");
registry.addViewController("/login").setViewName("login");
}
}
喜欢知道怎么做。
答案 0 :(得分:0)
似乎您无法使用 addViewControllers(“/ login”),因为它绕过了@ControllerAdvice
。
通过为“/ login”创建@RequestMapping
来修复。
参考: http://nixmash.com/java/why-your-controlleradvice-may-not-be-firing/