[Java] How to use Spring Boot session attributes (@SessionAttributes)

less than 1 minute read

This is the Spring Boot session method.

Form class


public class LoginForm implements Serializable {

@NotEmpty(message = "Enter Id")
private String id;

@NotEmpty(message = "Enter Password")
private String password;

private String check;
private String radio;
private String select;
//getter and setter omitted

Controller class


//@SessionAttributes is between multiple requests handled in one Controller
// Effective when sharing data.
Specify the object class to be stored in HTTP session in //types attribute.
public class IndexController {

* Add object to HTTP session
public LoginForm setUpLoginForm(){
return new LoginForm();

//Specify the attribute name of the object fetched from Model in value attribute of @ModelAttribute.
//This time, select of LoginForm class is specified.
public String loginCheck(@ModelAttribute("loginForm") @Validated LoginForm loginForm, BindingResult res,
@ModelAttribute("select") String select, Model model) {
  //Check the input
if (res.hasErrors()) {
return "login";

//This time, id of LoginForm class is specified.
public String create(Model model, @ModelAttribute("id") String id) {
return "create";


Implementation example accessed from #View

<h3 th:text=${loginForm.id}></h3>
<h3 th:text=${loginForm.select}></h3>