SPRING MVC – SESSIONATTRIBUTES ANNOTATION

Gerald Nguyen
Gerald Nguyen
1 min read ·
Previous | Next
On this page

Spring MVC’s SessionAttributes has 2 parameters: values (storing attributes’ names) and types (storing attributes’ types).

It’s pretty straightforward for values. You specify the name of your attribute and it is remembered.

It’s trickier for types. For example:

@SessionAttributes (types= java.util.List.class) does not work!

– But @SessionAttributes (types= java.util.ArrayList.class) works (but not always)

The reason behind this peculiar behaviour is that Spring does exact matching of attribute’s type versus declared type, instead of assessing attribute object’s Is-A relationship.

In fact, the specific method  that decides which attributes to store in session is isHandlerSessionAttribute of SessionAttributeHandler.

public boolean isHandlerSessionAttribute(String attributeName, Class<?> attributeType) {
        Assert.notNull(attributeName, "Attribute name must not be null");
        if (this.attributeNames.contains(attributeName) || this.attributeTypes.contains(attributeType)) {
            this.knownAttributeNames.add(attributeName);
            return true;
        }
        else {
            return false;
        }
}

So, my advise is to avoid interface in SessionAttributes’ Types. Instead include common implementation class such as ArrayList for List, HashMap for Map…