SPRING/스프링 MVC
핸들러 메소드 9부 멀티 폼 서브밋
JUMP개발자
2021. 7. 5. 22:43
세션을 사용해서 여러 폼에 걸쳐 데이터를 나눠 입력 받고 저장하기
1. 이벤트 이름 입력받고 Submit
2. 이벤트 제한 인원 입력 받고 Submit
3. 이벤트 목록 페이지
- 완료된 경우에 세션에서 모델 객체 제거하기
sessionStatus.setComplete();
@Controller
@SessionAttributes("event")
public class SampleController2 {
// 이름 Form 화면
@GetMapping("/events/form/name")
public String eventsForm(Model model, HttpSession httpSession) {
model.addAttribute("event",new Event());
return "/events/form-name";
}
// 이름 form에서 입력받은 내용 전송
@PostMapping("/events/form/name")
public String eventForNameSubmit(@ModelAttribute @Validated(Event.ValidateName.class) Event event,
BindingResult bindingResult, Model model,
SessionStatus sessionStatus) {
// @Validated 어노테이션을 통하여 Validation 체크에 맞지 않으면 다시 form으로 리턴
if(bindingResult.hasErrors()) {
return "/events/form-name";
}
// POST-REDIRECT-GET 패턴
// ( Post 이후에 브라우저를 리프래시 하더라도 폼 서브밋이 발생하지 않도록 하는 패턴)
return "redirect:/events/form/limit";
}
// limit Form 화면
@GetMapping("/events/form/limit")
public String eventForLimit(@ModelAttribute Event event, Model model) {
model.addAttribute("event",event);
return "events/form-limit";
}
// limit form에서 입력받은 내용 전송
@PostMapping("/events/form/limit")
public String eventForLimitSubmit(@ModelAttribute @Validated(Event.ValidateLimit.class)
Event event,BindingResult bindingResult,
Model model, SessionStatus sessionStatus) {
if(bindingResult.hasErrors()) {
return "/events/form-limit";
}
// 세션 데이터 초기화
sessionStatus.setComplete();
return "redirect:/events/list";
}
// 이벤트 목록 페이지
@GetMapping("/events/list")
public String getEvents(Model model) {
// 리스트를 불러오는 부분
Event event = new Event();
event.setName("spring");
event.setLimit(10);
List<Event> eventList = new ArrayList<>();
eventList.add(event);
model.addAttribute(eventList);
return "/events/list";
}
}
form.html
<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Create New Event</title>
</head>
<body>
<form action="#" th:action="@{/events}" method="post" th:object="${event}">
<p th:if="${#fields.hasErrors('name')}" th:errors="*{name}">Incorrect name</p>
<p th:if="${#fields.hasErrors('limit')}" th:errors="*{limit}">Incorrect limit</p>
<input type="text" title="name" th:field="*{name}">
<input type="text" title="limit" th:field="*{limit}">
<input type="submit" value="Create"/>
</form>
</body>
</html>
form-limit.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="#" th:action="@{/events/form/limit}" method="post" th:object="${event}">
<p th:if="${#fields.hasErrors('limit')}" th:errors="*{limit}">error</p>
limit : <input type="text" title="limit" th:field="*{limit}">
<button type="submit">Create</button>
</form>
</body>
</html>
form-name.html
<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Create New Event</title>
</head>
<body>
<form action="#" th:action="@{/events/form/name}" method="post" th:object="${event}">
<p th:if="${#fields.hasErrors('name')}" th:errors="*{name}">Incorrect limit</p>
name : <input type="text" title="name" th:field="*{name}">
<input type="submit" value="Create"/>
</form>
</body>
</html>
list.html
<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Create New Event</title>
</head>
<body>
<a th:href="@{/events/form}">Create New Event</a>
<div th:unless="${#lists.isEmpty(eventList)}">
<ul th:each="event: ${eventList}">
<p th:text="${event.name}">Event Name</p>
</ul>
</div>
</body>
</html>