Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 자바
- springdatajpa
- JPQL
- http
- 스프링 핵심 원리
- spring
- JDBC
- pointcut
- 스프링
- Exception
- jpa
- Android
- Servlet
- 알고리즘
- db
- transaction
- java
- SpringBoot
- 스프링 핵심 기능
- QueryDSL
- 백준
- Spring Boot
- 그리디
- AOP
- Thymeleaf
- Greedy
- 인프런
- Proxy
- kotlin
- 김영한
Archives
- Today
- Total
개발자되기 프로젝트
[로그인] 회원가입 본문
1. Member
package hello.login.domain.member;
import lombok.Data;
import javax.validation.constraints.NotEmpty;
@Data
public class Member {
private Long id;
@NotEmpty
private String loginId; //로그인id
@NotEmpty
private String name;
@NotEmpty
private String password;
}
2. MemberRepository
- loginId로 찾는 메서드 추가
package hello.login.domain.member;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import java.util.*;
@Slf4j
@Repository
public class MemberRepository {
private static Map<Long, Member> store = new HashMap<>();
private static long sequence = 0L;
public Member save(Member member){
member.setId(++sequence);
log.info("save : member = {}", member);
store.put(member.getId(), member);
return member;
}
public Member findById(Long ig){
return store.get(ig);
}
public List<Member> findAll(){
return new ArrayList<>(store.values());
}
public Optional<Member> findByLoginId(String loginId){
return findAll().stream().
filter(m -> m.getLoginId().equals(loginId)).findFirst();
}
public void clearStore(){
store.clear();
}
}
3. MemberController
- Bean Validation을 위해 @Validated 추가,
- Validation 결과 보관을 위해 BindingResult 추가.
package hello.login.web.member;
import hello.login.domain.member.Member;
import hello.login.domain.member.MemberRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequiredArgsConstructor
@RequestMapping("/members")
public class MemberController {
private final MemberRepository memberRepository;
@GetMapping("/add")
public String addForm(@ModelAttribute("member") Member member){
return "members/addMemberForm";
}
@PostMapping("/add")
public String save(@Validated @ModelAttribute("member") Member member, BindingResult bindingResult){
if(bindingResult.hasErrors()){
return "members/addMemberForm";
}
memberRepository.save(member);
return "redirect:/";
}
}
4. 테스트용 데이터
package hello.login;
import hello.login.domain.item.Item;
import hello.login.domain.item.ItemRepository;
import hello.login.domain.member.Member;
import hello.login.domain.member.MemberRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
@Component
@RequiredArgsConstructor
public class TestDataInit {
private final ItemRepository itemRepository;
private final MemberRepository memberRepository;
/**
* 테스트용 데이터 추가
*/
@PostConstruct
public void init() {
itemRepository.save(new Item("itemA", 10000, 10));
itemRepository.save(new Item("itemB", 20000, 20));
Member member = new Member();
member.setLoginId("test");
member.setPassword("test!");
member.setName("테스터");
memberRepository.save(member);
}
}
5. GitHub : 210925 join
'인프런 > [인프런] 스프링 MVC 2' 카테고리의 다른 글
[로그인] 로그인 처리 - 쿠키 (0) | 2021.09.26 |
---|---|
[로그인] 로그인 기능. (0) | 2021.09.25 |
[로그인] 프로젝트 생성, home 화면 (0) | 2021.09.25 |
[로그인] 로그인 요구사항 (0) | 2021.09.25 |
Bean Validation - HTTP 메시지 컨버터 (0) | 2021.09.25 |
Comments