Spring @SessionAttribute annotation – Spring MVC

Spring 4.3 provides a new @SessionAttribute annotation that helps us access to pre-existing global session attributes (outside the controller). Today we’re gonna look at how to get a HttpSession attribute which is created in an Interceptor using Spring @SessionAttribute annotation.

Related Articles:
How to use SpringMVC HandlerInterceptor with Spring Boot
Spring @RequestAttribute annotation – Spring MVC

I. OVERVIEW

Instead of using HttpSession object, we can bind attribute from a javax.servlet.http.HttpSession to a handler method parameter by using @SessionAttribute annotation on that parameter:

In the example, to have a pre-existing session attribute which are managed globally, we create an Interceptor and focus on preHandle() method:

For more details, please take a look at this article: How to use SpringMVC HandlerInterceptor with Spring Boot

So, we’re gonna look at the next part of this tutorial to know how to get Counter attribute from HttpSession with attribute created in preHandle() method.

II. PRACTICE

Technology
– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot: 1.5.1.RELEASE

Project Structure:

Now follow step by step:

1. Create Spring Boot project and add Dependency

– Open Spring Tool Suite, on Menu, choose File -> New -> Spring Starter Project, then fill each fields.
– Click Next, then Finish. Spring Boot project will be created successfully.
– Add dependency to pom.xml:

2. Create Data Model class

Under package model, create Counter class, this class will be used for session attribute:

3. Create and add Interceptor class

Under package config, create CounterInterceptor class that implements HandlerInterceptor. For simplifying the example, we only focus on writing code for preHandle() method:

We set Attribute Counter for HttpSession with value START_COUNT. This value will be kept when Counter attribute is bound to handle() method of Web Controller.

Next, create a @Configuration component to add the Interceptor above:

4. Create a WebController

Under package controller, create WebController class:

Everytime we send a request, @SessionAttribute named “counter” will be bound.

5. Add configuration to application.properties

Open application.properties under src/main/resources, add configuration:

We set session timeout every 10 seconds.

6. Run Spring Boot Application & Enjoy Result

– Config maven build:
clean install
– Run project with mode Spring Boot App
– Check results:

Send request: http://localhost:8080/
System shows:

Web Browser:

It means we got session attribute counter with value 9 successfully and increased it.

Counter will increase anytime we send request again (in 10 seconds):

When session is time out, if we send request again, the result is just like the beginning:
System shows:

Web Browser:

III. SOURCE CODE

Spring-Session-Attribute


Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*