About Alexey Zvolinskiy
Session management is one of essential parts for each web application. Since Spring MVC is a powerfull framework for a web development, it has own tools and API for the interaction with sessions. Today I intend to show you basic ways of session processing within Spring MVC application. That’s mean a processing of forms, adding objects into a session, displaying of objects from the session on JSP. I will try my best, so let’s start.
This Spring MVC Session tutorial will be based on one of the previous posts on my blog, related to the form handling. I’m going to extend the application by adding a session logic to the existing student-form, create a new one page with a form and a single text field on it. The text from the field will be processed by a some controller
and added to the session. In order to check the session functionality I will display the session objects on the pages using JSTL. The src from the tutorial you can download in the end of tutorial.
Form with the single text field
Firstly I need to create a view and the controller. I will start from the view creation and after that I’ll demonstrate the corresponding controller with the session logic.
...<h2>Adding of a String into the session</h2><form action="remember.html" method="post"><table><tbody><tr><td>To remember:</td><td><input name="thoughtParam" type="text"></td></tr><tr><td><input type="submit"></td><td></td></tr></tbody></table></form><a href="${pageContext.request.contextPath}/">Main page</a> ... Now I need to develop the controller to handle the form. There will be two methods for the requests handling: the first on is responsible for navigation to the page, the second one is related to session activity.
@Controller@SessionAttributes("thought")public class SingleFieldController { @RequestMapping(value="/single-field") public ModelAndView singleFieldPage() { return new ModelAndView("single-field-page"); } @RequestMapping(value="/remember") public ModelAndView rememberThought(@RequestParam String thoughtParam) { ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("thought", thoughtParam); modelAndView.setViewName("single-field-page"); return modelAndView; } } This is a simple Spring MVC controller with the one extra @SessionAttributes annotation. It indicates that in the controller’s methods can be assigned some values to arguments of the annotation. In this example I declared just one session attribute with the name “thought“. That’s mean I can put some object into modelAndView using addObject() method, and it will be added to the session if the name of the object will be the same as the name of argument in @SessionAttributes. The last thinng what I should to do is to add a link to the new page on the index.jsp:
... <h1>Home page</h1> <p>This is Home page.</p> <p>Don't forget: ${thought}</p> <p> <a href="person-form.html">Person page</a> <br> <a href="single-field.html">Single field page</a> </p>... In order, to check that session works properly, you need to add following code in the existing views (single-field-page.jsp, …):
<p>Don't forget: ${thought}</p> On the screenshots below you can see the result of the code work:
And the results:
And
Adding of a custom object into the session
In this section I’m going to show you how to add a custom object into the session, and how to display object’s properties on JSP. The role of custom object will play Person object. Firstly I’ll modify the existing person controller:
@Controller@SessionAttributes("personObj")public class PersonController { @RequestMapping(value="/person-form") public ModelAndView personPage() { return new ModelAndView("person-page", "person-entity", new Person()); } @RequestMapping(value="/process-person") public ModelAndView processPerson(@ModelAttribute Person person) { ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("person-result-page"); modelAndView.addObject("pers", person); modelAndView.addObject("personObj", person); return modelAndView; } } Comparing with latest version I added two new strings:
...@SessionAttributes("personObj")...modelAndView.addObject("personObj", person);... The result of the code execution is following:
And
This is the end of Spring MVC session tutorial. And as I promised earlier, I give a link to the sources of project. Everything I mentioned in the post is just a part of things, which you should know about the sessions, later I’ll write a post about different important features.
Source : http://www.javacodegeeks.com/2013/04/spring-mvc-session-tutorial.html