How to make a Model attribute global?


I'm using Spring MVC Framework and I'd like all the .jsp pages of the View to have access to the User's attributes(name, sex, age...). So far, I use the addAttribute method of the Model(UI) in every Controller to pass the current User's attributes to the View. Is there a way to do this only once and avoid having the same code in every Controller?


You can use Spring's @ControllerAdvice annotation on a new Controller class like this:

@ControllerAdvice public class GlobalControllerAdvice { @ModelAttribute("user") public List<Exercice> populateUser() { User user = /* Get your user from service or security context or elsewhere */; return user; } }

The "populateUser" method will be executed on every request and since it has a @ModelAttribute annotation, the result of the method (the user) will be put into the model for every request.

The user will be available in your jsp using ${user} since that was the name given to the @ModelAttribute (example: @ModelAttribute("fooBar") -> ${fooBar} )

You can pass some arguments to the @ControllerAdvice annotation to specify which controllers are advised by this Global controller. For example:

@ControllerAdvice(assignableTypes=FooController.class,BarController.class}) or @ControllerAdvice(basePackages="foo.bar.web.admin","foo.bar.web.management"}))


If it is about User's attributes, you can bind the model bean to session as an attribute which can be accessed on every view. This needs to be done only once.

Another option could be is to implement a HandlerInterceptor, and expose the model to every request.


  • Vue and Vuex: Handling dependent computed properties
  • Does cudaFree after asynchronous call work?
  • Repeated calls to CreateCompatibleBitmap() eventually failure with GetLastError() == 6
  • php login session login variables
  • KeyedCollection with enum key type will not index by int
  • Can I use servlet api 3.0 and jetty 8?
  • Are the cmdlets in a pipeline executing in parallel?
  • DataGrid Looses Focus When Delete Key is Pressed
  • Iterating on Javascript arrays with the “in” keyword
  • How do I search for a particular file in a directory using Perl?
  • How do you set up the correct HTTP Response object for a Range request coming from BITS (Background
  • Cocoa: Accepting and responding to keystrokes
  • What to do after getting Auth Token - Android Youtube API
  • Get a class as javax.lang.model.element.Element in junit tests
  • CSS - don't wrap span contents in fixed width table cell
  • HTTP Status 500 - Error instantiating servlet class org.springframework.web.servlet.DispatcherServle
  • java: reading large file with charset
  • maximizing profit for given stock data via DP
  • search bar getting disappeared in ios UIsearchcontroller
  • limit python input to int
  • AppEngine Paypal integration giving SSLCertificateError on localhost, using Python
  • Creating and managing two independent random number sequences
  • List using with references, changes behavior when used as a member
  • Send HTML Mail with Unicode
  • Battery life if using GPS and background app ios/android
  • How to move to lines with the same indentation in Visual Studio Code
  • Unity Resources.load() won't work with external dll
  • ASP.NET MVC 2 actions for the same route?
  • Multiplying polynomials/simplifying like terms
  • Problems using phonegap / cordova file plugin part 2 - synchronicity
  • Floating parent div grows to hypothetical width of floating child div
  • How to make 100% div height between header and footer?
  • Terminal run dalvikvm with am.jar
  • Send array to next viewcontroller iOs xcode [duplicate]
  • Codeigniniter insert data through models and controller
  • Angular FormGroup won't update it's value immediately after patchValue or setValue
  • PHP Permalinks.. how to change?
  • What does the “id” field in an Android “Google Play Music” broadcast intent correspond to?
  • ARKit code issue {unknown error -1=ffffffffffffffff error: Task failed with exit 1}
  • Time Complexity of Fibonacci Algorithm [duplicate]