25. 프로필 수정 처리

25. 프로필 수정 처리

728x90

정말로 쉬운 폼 처리

비어있는 값을 허용한다. (기존에 있던 값을 삭제하고 싶을 수도 있기 때문에..)

중복된 값을 고민하지 않아도 된다.

확인할 내용은 입력 값의 길이 정도.

폼 처리

에러가 있는 경우 폼 다시 보여주기.

에러가 없는 경우

리다이렉트시에 간단한 데이터를 전달하고 싶다면?

프로필 수정 수정 후 모습

AccountController @GetMapping("/check-email-token") public String checkEmailToken(String token, String email, Model model) { Account account = accountRepository.findByEmail(email); String view = "account/checked-email"; if (account == null){ model.addAttribute("error","wrong email"); return view; } if(!account.isValidToken(token)){ model.addAttribute("error","wrong email"); return view; } File file; ZipEntry entry = null; accountService.completeSignUp(account); model.addAttribute("numberOfUser", accountRepository.count()); model.addAttribute("nickname", account.getNickname()); return view; } // 이 Account 객체는 persistence 상태 영속성 컨텍스트에서 관리하는 객체 // (accountRepository)이곳에 들렸다 옴 public void completeSignUp(Account account) { account.completeSignup(); login(account); } // SettingsController의 updateProfile메서드에서 온 Account는 persistence 상태가 아니다. //Http Session Principal 객체 정보 Transaction 끝난지 오래 /* 4가지 상태 비영속(new, transient) 상태 : 엔티티가 영속성 컨텍스트와 전혀 관련이 없는 상태입니다. 영속(managed) 상태 : 엔티티가 영속성 컨텍스트에서 관리되고 있는 상태입니다. 준영속(detached) 상태 : 영속성 컨텍스트에서 관리되던 엔티티가 영속성 컨텍스트에서 관리되지 않게 되면, 이 엔티티를 준영속 상태라고 합니다. 삭제(removed) 상태 : 삭제 상태는 엔티티를 영속성 컨텍스트에서 관리하지 않게 되고, 해당 엔티티를 DB에서 삭제하는 DELETE 쿼리문을 보관하게 됩니다. 이 Account는 detached 상태 -> JPA에 한번이라도 알고 있고(ID 값 존재), 한번이라도 DB에 저장된 객체라면 detached detached -> persistence(managed) -> Repsository 보내기 */ 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); // -> persistence // TODO 문제가 하나 더 남았습니다. }

실습

from http://devjun.tistory.com/248 by ccl(A) rewrite - 2021-12-19 01:27:25