Session handling has become a integral part of web applications. Spring MVC is no exception in Session Handling.
Spring MVC provides very easy ways in which we can handle the Session Attributes. We will see some examples in which we will store objects/ variables in session using the
@SessionAttributes.
Storing Simple variables in Session
In this example we will see how we can store and retrieve String from Session. We will get a Employee Full name from the User and store it in the Session. In the next page we will use the jsp tags to show the Full Name.
1. Form to get the Full Name
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <html> <head> <title>Spring MVC - Session Attribute Test</title> </head> <body> <h2>Spring MVC - Storing String in Session Example</h2> <form:form method="post" action="showWelcome"> <table> <tr> <td>Employee Full Name</td> <td><input type="text" name="fullName"/></td> </tr> <tr> <td colspan="2"><input type="submit" value="Go To Welcome Page" /></td> </tr> </table> </form:form> </body> </html> |
2. Controller to Store the session attribute
As you can see we have used the
@SessionAttributes at the Controller level and defined “empFullName” to be stored in session. So in the showWelcome we store the Model Attribute as “empFullName”. This will automatically store the “empFullName” in the Session.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package com.kscodes.sampleproject.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.SessionAttributes; import org.springframework.web.servlet.ModelAndView; @Controller @SessionAttributes("empFullName") public class SessionAttributeController { @RequestMapping(value = "/firstPage") public ModelAndView showFirstPage() { return new ModelAndView("firstPage"); } @RequestMapping(value = "/showWelcome", method = RequestMethod.POST) public ModelAndView showWelcome(@RequestParam String fullName) { ModelAndView mv = new ModelAndView("welcome"); mv.addObject("empFullName", fullName); return mv; } } |
3. Display the Session attribute on JSP
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <html> <head> <title>Spring MVC - Storing String in Session Example</title> </head> <body> <h2>Spring MVC - Storing String in Session Example</h2> <h3>Welcome ${sessionScope.empFullName} !!!!!</h3> </body> </html> |
Output
Storing Custom Objects in Session
Now lets tweak the above example a little and use a custom object to store and retrieve from Session.
1. Employee Details Object
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package com.kscodes.sampleproject.model; public class EmployeeDetails { private String fullName; private String department; public String getFullName() { return fullName; } public void setFullName(String fullName) { this.fullName = fullName; } public String getDepartment() { return department; } public void setDepartment(String department) { this.department = department; } } |
2. Form to get the Full Name and Department
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <html> <head> <title>Spring MVC - Storing Custom Object in Session Example</title> </head> <body> <h2>Spring MVC - Storing Custom Object in Session Example</h2> <form:form method="post" action="showWelcome"> <table> <tr> <td>Employee Full Name</td> <td><input type="text" name="fullName"/></td> </tr> <tr> <td>Department Name</td> <td><input type="text" name="department"/></td> </tr> <tr> <td colspan="2"><input type="submit" value="Go To Welcome Page" /></td> </tr> </table> </form:form> </body> </html> |
4. Controller to Store the session attribute
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
package com.kscodes.sampleproject.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.SessionAttributes; import org.springframework.web.servlet.ModelAndView; import com.kscodes.sampleproject.model.EmployeeDetails; @Controller @SessionAttributes("empObject") public class SessionAttributeController { @RequestMapping(value = "/firstPage") public ModelAndView showFirstPage() { return new ModelAndView("firstPage"); } @RequestMapping(value = "/showWelcome", method = RequestMethod.POST) public ModelAndView showWelcome(@RequestParam String fullName, @RequestParam String department) { EmployeeDetails empDetails = new EmployeeDetails(); empDetails.setFullName(fullName); empDetails.setDepartment(department); ModelAndView mv = new ModelAndView("welcome"); mv.addObject("empObject", empDetails); return mv; } } |
5. Display the Employee Details Object from Session on JSP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <html> <head> <title>Spring MVC - Storing Custom Object in Session Example</title> </head> <body> <h2>Spring MVC - Storing Custom Object in Session Example</h2> <h3>Welcome ${sessionScope.empObject.fullName} from Department ${sessionScope.empObject.department} !!!!!</h3> </body> </html> |
6. Output
These were the 2 examples using Spring MVC Session attributes for String and Custom objects.