The @SessionAttributes annotation tells Spring MVC to keep selected model attributes in the HTTP session between requests, instead of only for one request. You place it on a controller class and list the attribute names or types; Spring then copies those model entries into the session and restores them for later handler methods of the same controller.
It is meant for short multi-step conversations such as a wizard or an edit form that spans a few requests, not for general session storage.
@SessionAttributes (in org.springframework.web.bind.annotation) applies to one controller only. For the names or types you declare:
@ModelAttribute, Spring reads the value from the session instead of building a new object.SessionStatus.setComplete(), Spring removes the attributes that this annotation manages.Only attributes handled by this controller are cleared, and attributes you put into the session yourself through HttpSession are untouched.
The @ModelAttribute method creates the SignupForm once. Because the name signup is listed in @SessionAttributes, the object lives in the session across step 1, step 2 and the final post. setComplete() in finish removes it, which prevents stale data and memory growth.
names or value matches by attribute name, types matches by the type of the model value. You can combine both. Prefer explicit names; matching by type can pick up objects you did not mean to keep.
| Annotation | Target | Purpose |
|---|---|---|
@SessionAttributes | Controller class | Declares which model attributes persist in the session for this controller |
@SessionAttribute | Handler method parameter | Reads an existing session attribute, possibly set elsewhere, for example by a filter |
@RequestAttribute | Handler method parameter | Reads a request-scoped attribute |
@SessionAttribute does not write anything; it only reads.
Serializable.SessionStatus.setComplete() leaves attributes in the session until it expires.For flows that must survive multiple tabs or restarts, keep the draft in a database row, or send the whole form state as hidden fields. For stateless applications and REST APIs, avoid HTTP sessions altogether and send state in the request or a token. Spring Web Flow offers explicit conversation scope if you need richer flows.
When you call SessionStatus.setComplete() in a handler, or when the HTTP session expires or is invalidated.
Session access is not synchronised. Concurrent requests from the same user can race, so avoid mutable shared objects or synchronise on the session.
Not through @SessionAttributes, which is scoped to its own controller. Another controller can read the raw value with @SessionAttribute using the same key.
WebFlux supports @SessionAttributes on annotated controllers in a similar way, using WebSession.
@SessionAttributes stores chosen model attributes in the HTTP session for the duration of a short multi-request interaction in one controller. Pair it with a @ModelAttribute factory method, clear the data with SessionStatus.setComplete(), keep stored objects small and serialisable, and use @SessionAttribute when you only need to read a session value.