프로젝트 설정
application.properties
#port
server.port=9090
#thymeleaf cache
spring.thymeleaf.cache=false
#encoding
server.servlet.encoding.charset=UTF-8
server.servlet.encoding.force=true
server.servlet.encoding.enabled=true
시작
HomeController.java
생성@Controller
public class HomeController {
@GetMapping
public String home() {
return "home";
}
}
home.html
생성<div class="container" style="max-width: 600px">
<div class="py-5 text-center">
<h2>홈 화면</h2>
</div>
<div class="row">
<div class="col">
<button class="w-100 btn btn-secondary btn-lg" type="button"
th:onclick="|location.href='@{/members/add}'|">
회원 가입
</button>
</div>
<div class="col">
<button class="w-100 btn btn-dark btn-lg"
onclick="location.href='items.html'"
th:onclick="|location.href='@{/login}'|" type="button">
로그인
</button>
</div>
</div>
<hr class="my-4">
</div>
Member.java
생성@Data
public class Member {
private Long id;
private String name; // 로그인 ID
private String loginId; // 사용자 이름
private String password; // 사용자 pw
}
MemberRepository.java
생성@Repository
public class MemberRepository {
private static Map<Long, Member> store = new HashMap<Long, Member>();
private static Long sequence = 0L;
public Member save(Member member) { // 저장
member.setId(++sequence);
store.put(member.getId(), member);
return member;
}
public Member findById(Long id) { // 아이디 찾기
return store.get(id); // 스토어에 저장된 id값 찾아서 바로 리턴
}
public List<Member> findAll(){ // 전체 회원 조회
return new ArrayList<Member>(store.values());
}
public Member findByLoginId(String loginId) { // 아이디 중복 확인
List<Member> all = findAll();
for(Member m : all) {
if(m.getLoginId().equals(loginId)) {
return m;
}
}
return null;
}
}
MemberController.java
생성@Controller
@RequestMapping("/members")
public class MemberController {
@GetMapping("/add")
public String addForm(@ModelAttribute("member") Member member) {
return "members/addMemberForm";
}
}
templates > members 폴더 생성
addMemberForm.html
생성 <form action="" id="myForm" th:action th:object="${member}" method="post">
<div>
<label for="loginId">로그인 ID</label>
<input type="text" id="loginId" th:field="*{loginId}" class="form-control" >
</div>
<div>
<label for="password">비밀번호</label>
<input type="password" id="password" th:field="*{password}" class="form-control" >
</div>
<div>
<label for="name">이름</label>
<input type="text" id="name" th:field="*{name}" class="form-control" >
</div>
<hr class="my-4">
<div class="row">
<div class="col">
<button class="w-100 btn btn-primary btn-lg" type="submit" >회원가입</button>
</div>
<div class="col">
<button class="w-100 btn btn-secondary btn-lg" onclick="location.href='items.html'" th:onclick="|location.href='@{/}'|" type="button">
취소
</button>
</div>
</div>
</form>
// 회원가입 클릭시, 홈화면으로 이동
MemberController.java
수정@Controller
@RequestMapping("/members")
@RequiredArgsConstructor
public class MemberController {
private final MemberRepository memberRepository;
...
@PostMapping("/add")
public String save(@ModelAttribute Member member) {
memberRepository.save(member);
return "redirect:/";
}
}
TestData를 세팅해주는 클래스 생성
TestDataInit.java
생성@Component // spring bean 에 등록
@RequiredArgsConstructor
public class TestDataInit {
private final MemberRepository memberRepository;
// 테스트 데이터 추가
@PostConstruct
public void init() {
Member member = new Member();
member.setLoginId("test");
member.setPassword("test!");
member.setName("테스터");
memberRepository.save(member);
}
}
LoginForm.java
생성@Data
public class LoginForm {
private String loginId;
private String password;
}
LoginController.java
생성@Controller
public class LoginController {
@GetMapping("/login")
public String loginForm(@ModelAttribute("loginForm")LoginForm loginForm) {
return "login/loginForm";
}
}
loginForm.html
생성<form action="item.html" th:action th:object="${loginForm}" method="post">
<div>
<label for="loginId">로그인 ID</label>
<input type="text" id="loginId" th:action th:field="*{loginId}" class="form-control">
</div>
<div>
<label for="password">비밀번호</label>
<input type="password" id="password" th:field="*{password}" class="form-control">
</div>
<hr class="my-4">
<div class="row">
<div class="col">
<button class="w-100 btn btn-primary btn-lg" type="submit">로그인</button>
</div>
<div class="col">
<button class="w-100 btn btn-secondary btn-lg" onclick="location.href='items.html'" th:onclick="|location.href='@{/}'|" type="button">취소</button>
</div>
</div>
</form>
로그인 아이디와 비밀번호 입력 후 로그인 시
- 존재하는 회원일 경우 -> "로그인 성공" alert창 띄운 뒤 home.html이동
- 존재하지 않는 회원일 경우 - > "로그인 실패" alert창 띄운 뒤 페이지 유지
LoginService.java
생성@Service
@RequiredArgsConstructor
public class LoginService {
private final MemberRepository memberRepository;
public Member login(String loginId, String password) {
Member member = memberRepository.findByLoginId(loginId);
if( member != null && member.getLoginId().equals(loginId) && member.getPassword().equals(password)) {
// 로그인 성공
return member;
} else {
return null;
}
}
}
LoginController.java
수정@Controller
@RequiredArgsConstructor
public class LoginController {
private final LoginService loginService;
....
@PostMapping("/login")
public String login(@ModelAttribute LoginForm form, Model model, RedirectAttributes redirectAttributes) {
Member loginMember = loginService.login(form.getLoginId(), form.getPassword());
if( loginMember == null) {
// 로그인실패
model.addAttribute("msg", "로그인실패");
return "login/loginForm";
}
// 로그인 성공
redirectAttributes.addFlashAttribute("msg","로그인 성공");
return "redirect:/";
}
}
loginForm.html
수정<script>
let message = "[[${msg}]]";
if(message != ""){
alert(message);
}
</script>
home.html
수정<script>
let message = "[[${msg}]]";
if(message != ""){
alert(message);
}
</script>
application.propertes
추가server.servlet.session.tracking-modes=cookie