Spring Boot session method.
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
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";
}
}
<h3 th:text=${loginForm.id}></h3>
<h3 th:text=${loginForm.select}></h3>
Recommended Posts