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

Spring Boot session method.

Form class

LoginForm.java


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,setter omitted

Controller class

IndexController.java


@Controller
@RequestMapping("/index")
//@Session Attributes is between multiple requests handled in one Controller
//Effective when sharing data.
//Specify the object class to be stored in the HTTP session in the types attribute.
@SessionAttributes(types=LoginForm.class)
public class IndexController {

	/*
	 *Add object to HTTP session
	 */
	@ModelAttribute("loginForm")
	public LoginForm setUpLoginForm(){
		return new LoginForm();
	}

	//The attribute name of the object obtained from Model is@Specify in the value attribute of ModelAttribute.
	//In this case, select of LoginForm class is specified.
	@PostMapping("check")
	public String loginCheck(@ModelAttribute("loginForm") @Validated LoginForm loginForm, BindingResult res,
			@ModelAttribute("select") String select, Model model) {
		//Check the input
		if (res.hasErrors()) {
			return "login";
		}
	}

	//In this case, the id of the LoginForm class is specified.
	@GetMapping("form")
	public String create(Model model, @ModelAttribute("id") String id) {
		return "create";
	}

}

Implementation example to access from View

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

Recommended Posts

How to use Spring Boot session attributes (@SessionAttributes)
How to use ModelMapper (Spring boot)
How to use MyBatis2 (iBatis) with Spring Boot 1.4 (Spring 4)
How to use built-in h2db with spring boot
Spring Boot --How to set session timeout time
How to use CommandLineRunner in Spring Batch of Spring Boot
How to use Lombok in Spring
How to use Spring Data JDBC
How to call and use API in Java (Spring Boot)
How to split Spring Boot message file
How to use Map
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
How to use map
How to make Spring Boot Docker Image smaller
How to use collection_select
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to add a classpath in Spring Boot
How to use hidden_field_tag
How to use TreeSet
[How to use label]
How to use identity
How to use hashes
How to use JUnit 5
How to bind to property file in Spring Boot
How to use Dozer.mapper
How to use Gradle
[Ruby on Rails] How to use session method
How to use org.immutables
[Spring Boot] How to refer to the property file
How to use java.util.stream.Collector
How to use VisualVM
How to use Map
How to write a unit test for Spring Boot 2
How to use Struts2 * Spring Framework (Spring plugin) June 2017 Version
How to create a Spring Boot project in IntelliJ
[Spring Boot] How to create a project (for beginners)
How to boot by environment with Spring Boot of Maven
How to use In-Memory Job repository in Spring Batch
[Java] How to use Map
How to use Chain API
How to use java Optional
How to use JUnit (beginner)
How to use Ruby return
[Rails] How to use enum
How to use @Builder (Lombok)
[Swift] How to use UserDefaults
How to use java class
How to use Swift UIScrollView
How to use Big Decimal
[Java] How to use Optional ②
[Java] How to use removeAll ()
Introduction to Spring Boot ① ~ DI ~
How to use String [] args
[Java] How to use string.format
Introduction to Spring Boot ② ~ AOP ~