Every single view in my Spring 3 app has a set of attributes they can rely on. So the first line of every controller is something like:
ControllerHelper.addDefaultModel(model, personManager, request);
In there I'll add
- user object and full name retrieved from the database if person is logged in
- set of variables which are typically set once (e.g.
imagesHost
)
- set of languages a visitor can switch to
- current language
- some statistics (e.g. total # of people in our system)
This all allows each view to display the logged in user's name, easily reference an image location, a list of languages and some overall stats about the site.
So the question is, is the controller model object the best place to store all the data or is there a more convenient place which makes it just as easy for views to access this info?
And secondly, I'd REALLY love not to have to have the ControllerHelper
line above as the first line in every controller. It's actually not always the first line, sometimes I first check if I need to redirect in that controller, because I don't want to waste resources filling the model for no reason. Maybe a filter or annotation or some Spring callback mechanism could make sure the ControllerHelper
code is called after the controller is finished but right before the view is rendered, skipping this if a redirect was returned?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…