정말로 쉬운 폼 처리
- 비어있는 값을 허용한다. (기존에 있던 값을 삭제하고 싶을 수도 있기 때문에...)
- 중복된 값을 고민하지 않아도 된다.
- 확인할 내용은 입력 값의 길이 정도.
폼 처리
- 에러가 있는 경우 폼 다시 보여주기.
- 에러가 없는 경우
- 저장하고,
- 프로필 수정 페이지 다시 보여주기.(리다이렉트)
- 수정 완료 메시지.
리다이렉트시에 간단한 데이터를 전달하고 싶다면?
- RedirectAttributes.addFlashAttribute()
- https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/mvc/support/RedirectAttributes.html
SettingsController에 프로필 수정 POST요청을 위한 핸들러 작성
package me.weekbelt.studyolle.settings;
port javax.validation.Valid;
@RequiredArgsConstructor
@Controller
public class SettingsController {
private final AccountService accountService;
// 기존 코드 .......
@PostMapping("/settings/profile")
public String updateProfile(@CurrentUser Account account,
@Valid @ModelAttribute Profile profile,
Errors errors, Model model,
RedirectAttributes attributes) {
if (errors.hasErrors()) {
model.addAttribute(account);
return "settings/profile";
}
accountService.updateProfile(account, profile);
attributes.addFlashAttribute("message", "프로필을 수정했습니다.");
return "redirect:" + "/settings/profile";
}
}
AccountService에서 프로필 수정 처리를 위한 updateProfile메소드 작성
package me.weekbelt.studyolle.account;
@Transactional
@RequiredArgsConstructor
@Service
public class AccountService implements UserDetailsService {
// 기존 코드 .....
public void updateProfile(Account account, Profile profile) {
account.setUrl(profile.getUrl());
account.setOccupation(profile.getOccupation());
account.setLocation(profile.getLocation());
account.setBio(profile.getBio());
// TODO: 프로필 이미지 수정
accountRepository.save(account);
// TODO: 문제가 하나 더 남아 있습니다.
}
}
참고: https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81-JPA-%EC%9B%B9%EC%95%B1#
'스프링과 JPA 기반 웹 어플리케이션 개발 > 1부 (개발환경, 회원가입, 로그인, 계정설정)' 카테고리의 다른 글
25. 프로필 이미지 변경 (0) | 2020.04.21 |
---|---|
24. 프로필 수정 테스트 (0) | 2020.04.21 |
22. 프로필 수정 (0) | 2020.04.21 |
21. Open EntityManager (또는 Session) In View 필터 (0) | 2020.04.21 |
20. 프로필 뷰 (0) | 2020.04.21 |