My Spring MVC web app has a navigation menu at the top of the page with links to a list of items. The list of items is retrieved from the database. This menu is common to all pages in the application. Currently, in every controller, I retrieve and store the list of items in session (if not already in session), so that it is available on every page. It works fine, but I feel this there should be a better way to do this. To avoid this redundancy, I am now trying to use a HandlerInterceptorAdapter. I am able to get it work, but not perfectly. The first time I load the page, I do not see the object I set in session. But, if I refresh the page, I do see the object in session.
I created my interceptor this way:
I declared the interceptor:
I am checking if the object is set in session by the time the
jsp renders:
With that, when I first load the page, I see the following print in the console:
My understanding is that the posthandle method is invoked before the page is rendered. However, according to the output on the console, I see that the jsp is being rendered before the interceptor's posthandle method.
Can anyone help me understand why it is behaving this way?