seong
Controller연습 - Member 본문
해볼것
- Member 테이블은 간단하게 소스코드로 작성
- 1. 엔티티, Dao , Controller 작성
- 2. 직접 서버 연동해 결과창 확인해보기
시작 전 폴더, 클래스 , jsp 파일 생성
Member.java
package site.metacoding.demo.domain;
import org.springframework.stereotype.Component;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class Member {
private Integer id;
private String name;
private String password;
private String email;
}
MemberDao.java
package site.metacoding.demo.domain;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Component;
@Component // Dao를 Ioc 컨테이너에 띄워줌, 다른 곳에서 굳이 한번더 객체 생성 해줄 필요없어짐.
public class MemberDao {
public Integer Insert(String name, String password, String email) {
System.out.println("Insert 성공");
System.out.println(name);
System.out.println(password);
System.out.println(email);
return 1;
}
public Integer Update(Integer id, String password) {
System.out.println("Update 성공");
System.out.println(password);
return 1;
}
public Integer deleteById(Integer id) {
System.out.println("Delete 성공");
System.out.println(id);
return 1;
}
// 회원 정보 보기는 아직DB 연결을 하지 않아서 임의로 데이터 생성 후 리
public Member findById(Integer id) {
Member m1 = new Member(1,"cos","1234","cos@naver.com");
Member m2 = new Member(2,"ssar","12345","ssar@naver.com");
if(id == 1) {
return m1;
}
else if(id == 2) {
return m2;
}
else {
return null;
}
}
// 모든 회원 정보는 리스트 타입
public List<Member> findByAll() {
Member m1 = new Member(1,"cos","1234","cos@naver.com");
Member m2 = new Member(2,"ssar","12345","ssar@naver.com");
List<Member> memberList = new ArrayList<>();
memberList.add(m1);
memberList.add(m2);
return memberList;
}
}
MemberController.java
package site.metacoding.demo.web;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import site.metacoding.demo.domain.Member;
import site.metacoding.demo.domain.MemberDao;
@Controller
public class MemberController {
// MemberDao를 사용
private MemberDao memberDao;
public MemberController(MemberDao memberDao) {
this.memberDao = memberDao;
}
@GetMapping("/member/joinForm")
public String joinForm() {
return "member/joinForm";
}
@PostMapping("/member/join")
public String join(String name, String password, String email) {
Integer result = memberDao.Insert(name, password, email);
if (result == 1) {
return "member/loginForm";
} else {
return "error/serverError";
}
}
@GetMapping("/member/{id}")
public String detail(@PathVariable Integer id, Model model) {
Member memberPS = memberDao.findById(1);
model.addAttribute("member", memberPS);
return "member/detail";
}
}
detail.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1> Detail Page</h1>
<hr/>
name : ${member.name }<br/>
password : ${member.password }<br/>
email : ${member.email }<br/>
</body>
</html>
joinForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>회원 가입</h1>
<form method="post" action="/member/join">
name : <input type="text" name="name"> </br>
password : <input type="password" name=password> </br>
Email : <input type="email"name=email></br>
<button type="submit">Login Button</button>
</form>
</body>
</html>
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Login Page</h1>
</body>
</html>
<결과 화면>
Detail화면 Controller에서 임의로 id를 1로 주었다.
회원가입 화면
Dao의 return값이 1일 경우
Dao에서 결과 return 값을 -1로 임의로 설정 했을 경우
'Spring > SpringBoot' 카테고리의 다른 글
Mapping 메서드(1) - Get , Post, Put, Delete(white프로젝트) (0) | 2022.08.29 |
---|---|
PostMan 설정하기 (0) | 2022.08.29 |
Controller 연습 - user 데이터 읽기 (0) | 2022.08.25 |
lombok 설정하기 (0) | 2022.08.25 |
PostMapping , GetMapping (0) | 2022.08.25 |