Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use @ControllerAdvice for ViewController's (registered using ViewControllerRegistry)

Tags:

spring-mvc

For having a global @ModelAttribute, I am using a @ControllerAdvice, like this:

@ControllerAdvice
public class MyControllerAdvice {

    @ModelAttribute("globalModelAttribute")
    public String getFacebookScope() {
        return "FACEBOOK_SCOPE";
    }

} 

Now I am able to access the globalModelAttribute in all my JSPs except those which are registered as ViewControllers, like this:

@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("home");
        registry.addViewController("/login").setViewName("login");
    }

}

Liked to know how to go about this.

like image 287
Sanjay Avatar asked Mar 14 '15 00:03

Sanjay


1 Answers

Seems that you cannot use addViewControllers("/login") because it bypass the @ControllerAdvice.

Fixed by creating a @RequestMapping for "/login".

Reference: http://nixmash.com/java/why-your-controlleradvice-may-not-be-firing/

like image 149
Bruno De Freitas Barros Avatar answered Nov 15 '22 09:11

Bruno De Freitas Barros



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!