I am trying to store a list of objects in my Spring MVC application’s session so that I may iterate through them within my JSPs to create options in a drop down.
After about two hours of reading through posts and blogs I am very confused. In fact, I don’t even know where to start. Can anyone point me in the direction of a Spring based solution [documentation, tutorials, examples] that meets the following criteria?
- Values for the list are pulled from a database table.
- The list is loaded during application startup.
- It does not directly access the session within a controller.
You should define a spring bean. You can see how to run code within the bean at startup in the answer to this question. Something like an ApplicationListener should work. In that startup code you can load the table into memory (it sounds like that is what you are looking for.)
Within your controller you inject the spring bean which has a method to get the values you need. You then add those values to the Model (not the session) in your request handler and the Model is used by the view (your JSP page) which can iterate over the values and display them.