๐ข ์์
- StudentDto ํด๋์ค ์์ฑ
- create.html ์์ฑ
- StudentController ์์ฑ
- StudentService ์์ฑ
DTO : Data Transfer Object
DAO : Data Access Object
public class StudentDto {
private Long id;
private String name;
private Stirng email;
public StudentDto() {
}
public StudentDto(Long id, String name, String email) {
this.id = id;
this.name = name;
this.email = email;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "StudentDto{" +
"id=" + id +
", name='" + name + '\'' +
", email='" + email + '\'' +
'}';
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Create Student</title>
</head>
<body>
<h1>Create Student</h1>
<form action="/create" method="post">
<!-- ์ฌ์ฉ์๊ฐ ์ ๊ณตํ ๋ฐ์ดํฐ์ ์๋ง์ Input๊ณผ label์ ๋ง๋ ๋ค. -->
<label for="name-input">Name: <input id="name-input" name="name"></label><br>
<label for="email-input">Email: <input id="email-input" name="email"></label><br>
<input type="submit">
</form>
</body>
</html>
import com.example.crud.model.StudentDto;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class StudentController {
private final StudentService studentService;
public StudentController(StudentService studentService) {
this.studentService = studentService;
}
@GetMapping("/create-view") // URL ์ปจ๋ฒค์
, ํ์ดํ์ ์ฐ๊ฒฐํด์ ์ด์ฉ
public String createView() {
return "create";
}
@PostMapping("/create")
public String create(
@RequestParam("name")
String name,
@RequestParam("email")
String email
) {
System.out.println(name);
System.out.println(email);
StudentDto studentDto = studentService.createStudent(name, email);
System.out.println(studentDto.toString());
return "redirect:/create-view"; // ์ฒ์ ํ์ด์ง๋ก ๋์๊ฐ๊ฒ๋
}
}
import com.example.crud.model.StudentDto;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class StudentService {
// ๋ณต์์ StudentDto๋ฅผ ๋ด๋ ๋ณ์
private final List<StudentDto> studentList = new ArrayList<>();
private Long nextId = 1L;
// ์๋ก์ด StudentDto๋ฅผ ์์ฑํ๋ ๋ฉ์๋
public StudentDto createStudent(String name, String email) {
StudentDto newStudent = new StudentDto(
nextId, name, email
);
nextId++;
studentList.add(newStudent);
return newStudent;
}
}
๐ข ์์
- StudentService์ ๋ฉ์๋ ์ถ๊ฐ
- home.html ์์ฑ
- StudentController ์์
@Service
public class StudentService {
private Long nextId = 1L;
private final List<StudentDto> studentList = new ArrayList<>();
public StudentDto createStudent(String name, Stirng email) {
StudentDto newStudent = new StudentDto(nextId, name, email);
nextId++;
studentList.add(newStudent);
return newStudent;
}
public List<StudentDto> readStudentAll() {
return studentList;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Students Home</title>
</head>
<body>
<h1>Student List</h1>
<div th:if="${studentList.isEmpty()}">
<p>No students here...</p>
</div>
<div th:unless="${studentList.isEmpty()}" th:each="student: ${studentList}">
<p>๋ฒํธ: [[${student.id}]]</p>
<p>์ด๋ฆ: [[${student.name}]]</p>
<p>์ด๋ฉ์ผ: [[${student.email}]]</p>
</div>
<a th:href="@{/create-view}">Create</a>
</body>
</html>
@Controller
public class StudentController {
private final StudentService studentService;
public StudentController(StudentService studentService) {
this.studentService = studentService;
}
@GetMapping("/create-view")
public String createView() {
return "create";
}
@PostMapping("/create")
public String create(
@RequestParam("name") Stirng name,
@RequestParam("email") String email) {
System.out.println(name);
System.out.println(email);
StudentDto newStudent = studentService.createStudent(name, email);
System.out.println(newStudent);
return "redirect:/create-view";
// ์ ์ถํ ๋ค /home์ผ๋ก ๋ณด๋ด๋ ค๋ฉด ๋ค์๊ณผ ๊ฐ์ด ์์ฑ
// return "redirect:/home";
// return "home"; ์ผ๋ก ํ ๊ฒฝ์ฐ, redirectํ๋๊ฒ ์๋๋ผ "home"์ด๋ผ๋ view๋ฅผ ๋ณด์ฌ์ค
// ๋จ, ์ด๋ ํ์ฌ ์ฝ๋๋ view์ ํ์ํ ์์ฑ๊ฐ์ ๋๊ฒจ์ฃผ์ง ์๊ธฐ์, ์๋ฌ๊ฐ ๋ฐ์ํ๊ฑฐ๋,
// view๊ฐ ์ ๋๋ก ํ์๋์ง ์์ ์ ์์
// ์ค์ ๋ก view์ studentList.isEmpty() ๋ถ๋ถ์์ null ์๋ฌ ๋ฐ์
}
@GetMapping("/home")
public String home(Model model) {
model.addAttribute("studentList", studentService.readStudentAll());
return "home";
}
}
๐ for each๋ฌธ์ ํ์ฉํ ์ฝ๋
// ์ ์ฝ๋ ์๋ต
public StudentDto readStudent(Long id) {
for(StudentDto studentDto: studentDtoList) {
if(studentDto.getId().equals(id)) {
return studentDto;
}
}
return null;
}
๐ stream์ ํ์ฉํ ์ฝ๋
// ์ ์ฝ๋ ์๋ต
public StudentDto readStudent(Long id) {
return studentDtoList
.stream()
.filter(studentDto -> studentDto.getId().equals(id))
.findFirst()
.orElse(null);
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>[[${student.name}]]</title>
</head>
<body>
<h1>[[${student.id}]]. [[${student.name}]]</h1>
<p>์ด๋ฉ์ผ : [[${student.email}]]</p>
<a href="/home">Back</a>
</body>
</html>
//์ ์ฝ๋ ์๋ต
@GetMapping("/read") // http://localhost:8080/read๋ก ์คํ๋๋ ๋ฉ์๋
publci String read(Long id, Model model) {
System.out.println(id);
model.addAttribute(
"student",
studentService.readStudent(id)
);
return "read";
}
๐ธ URL โ ์ปดํจํฐ์ ํ์ผ์์คํ (๊ฒฝ๋ก)์ ๋ชจ์ต์ ์ผ๋ถ ์ ์ฌํ๊ฒ ๋ง๋ค์ด, ํน์ ์์์ ๊ฐ๋ฆฌํค๋ ์ฉ๋
๐ ์กฐํํ๊ณ ์ถ์ ์์์ด URL์ ๋๋ฌ๋๊ฒ ํ๊ธฐ
@GetMapping("/{id}") // <----- ๋ณ๊ฒฝ
public String read(
@PathVariable("id") Long id, // <---- @PathVariable๋ก url๋ก ๋๊ฒจ ๋ฐ์ ๊ฐ์
Model model // Long id๋ก ๋ฐ์์ ์ธ์ ์๋ค.
) {
System.out.println(id);
model.addAttribute(
"student",
studentService.readStudent(id)
);
return "read";
}
๐ ๋ฐ์ดํฐ ๊ฐฑ์ ์ ์ํด์ 1. ๋์ ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ 2. ๊ฐฑ์ ํ ์๋ก์ด ๋ฐ์ดํฐ๋ก ์ ๋ฐ์ดํธ ํด์ผํจ
๐ธUpdate = Create + Read
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Update</title>
</head>
<body>
<h1>Update Student</h1>
<form method="post">
<!-- ์ฌ์ฉ์๊ฐ ์ ๊ณตํ ๋ฐ์ดํฐ์ ์๋ง์ input๊ณผ label์ ๋ง๋ ๋ค. -->
<label for="name-input">Name: <input id = "name-input" name="name"></label>
<br>
<label for="email-input">Email : <input id = "email-input" name="email"></label>
<br>
<!-- ๋ฐ์ดํฐ ์ ์ถ ๋ฒํผ -->
<input type="submit">
</form>
<!-- ์
๋ฐ์ดํธ์ฐฝ์์ ๋ค์ ๋์๊ฐ์ผ ํ ๊ณณ์ ํ์ ์์ธํ์ด์ง ์์ผ๋ก ์๋์ ๊ฐ์ด ์ค์ ! -->
<a th:href="@{/{id} (id=${student.id})}">Back</a>
</body>
</html>
๐ back
์ ์์ธ๋ณด๊ธฐ ํ์ด์ง๋ก ์ค์
// ์ ์ฝ๋ ์๋ต
@GetMapping("/{id}/update-view")
public String updateView(
@PathVarible("id") Long id, Model model
) {
model.addAttribute("student", studentService.readStudent(id));
return "update";
}
PathVariable
์ url์์ ํน์ ๋ฐ์ดํฐ๋ฅผ ์ง์ ํ๋ ๋ฑ ํ ๊ฐ์ง ๊ฐ๋ง ๋ฐ์ ์ ์์RequestParam
์ ๊ฒฝ์ฐ ํด๋น ์์น์์ ์ฌ์ฉํ ๋ฐ์ดํฐ๋ฅผ ๋ด์ ๋ณด๋ด๋ ์ฉ๋๐ ์๋ณธ ๋ฐ์ดํฐ๋ฅผ ์์ ํ ์ ์๋๋ก ์ฝ๋๋ฅผ ์์ฑ
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Update</title>
</head>
<body>
<h1>Update Student</h1>
<form th:action="@{/{id}/update (id=${student.id})}" method="post">
<!-- ์ฌ์ฉ์๊ฐ ์ ๊ณตํ ๋ฐ์ดํฐ์ ์๋ง์ input๊ณผ label์ ๋ง๋ ๋ค. -->
<label for="name-input">
<!-- value ์์ฑ ์ถ๊ฐ๋ก ์ด๊ธฐ์ ํ์ํ ๊ฐ ์ค์ -->
Name: <input id="name-input" name="name" th:value="${student.name}">
</label>
<br>
<label for="email-input">
<!-- value ์์ฑ ์ถ๊ฐ๋ก ์ด๊ธฐ์ ํ์ํ ๊ฐ ์ค์ -->
Email: <input id="email-input" name="email" th:value="${student.email}">
</label>
<br>
<!-- ๋ฐ์ดํฐ ์ ์ถ ๋ฒํผ -->
<input type="submit">
</form>
<!-- <a href="/home">Back</a>-->
<a th:href="@{/{id} (id=${student.id})}">Back</a>
</body>
</html>
public StudentDto updateStudent(Long id, String name, String email) {
int target = 1;
for(int i = 0; i < studentDtoList.size(); i++) {
// ํ์์ ์ธ๋ฑ์ค๋ฅผ ์ฐพ์ผ๋ฉด ๊ธฐ๋ก ํ ๋ฐ๋ณต ์ข
๋ฃ
if(studentDtoList.get(i).getId().equals(id)) {
target = i;
break;
}
}
// ์ฐพ์ ๋์์ name๊ณผ email ๋ณ๊ฒฝ
if (target != 1) {
studentDtoList.get(target).setName(name);
studentDtoList.get(target).setEmail(email);
return studentDtoList.get(target);
}
else return null;
}
// ์ ์ฝ๋ ์๋ต
@PostMapping("/{id}/update")
public String update(
@PathVariable("id") Long id,
@PathVariable("name") String name,
@PathVariable("email") String email,
) {
// service ํ์ฉ
studentService.updateStudent(id, name, email);
// ์์ธ๋ณด๊ธฐ ํ์ด์ง๋ก redirect
return String.format("redirect:/%s", id);
}
๐ข ๊ตฌํํ ๊ธฐ๋ฅ ์ ๋ฆฌ
- ์์ธ๋ณด๊ธฐ ํ์ด์ง โ ์ญ์ ํ์ธํ๊ธฐ
- ์ญ์ ํ์ธ + ์ญ์ ๋ฒํผ + ๋ค๋ก ๊ฐ๊ธฐ
- ์ญ์ ์ฑ๊ณต ํ
"/home"
์ผ๋ก ์ด๋
// ์ ์ฝ๋ ์๋ต
@GetMapping("/{id}/delete-view")
public String deleteView(
@PathVariable("id") Long id, Model model
) {
StudentDto studentDto = studentService.readStudent(id);
model.addAttribute("student", studentDto);
return "delete";
}
// ์ ์ฝ๋ ์๋ต
public boolean deleteStudent(Long id) {
int target = -1;
for(int i = 0; i < studentDtoList.size(); i++) {
// ๋์ ์ ์
if(studentDtoList.get(i).getId().equals(id) {
target = i;
break;
}
}
// ๊ฒ์ ์ฑ๊ณต์
if (target != -1) {
// ์ญ์
studentDtoList.remove(target);
return true;
}
else return false;
}
๐ ์ญ์ ํ์๋ ์์ธํ์ด์ง๊ฐ ์์ผ๋ฏ๋ก /home
์ผ๋ก ์ด๋
@PostMapping("/{id}/delete")
public String delete(
@PathVariable("id") Long id
) {
studentService.deleteStudent(id);
return "redirect:/home";
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Delete</title>
</head>
<body>
<h1>Delete Student Name?</h1>
<form th:action="@{/{id}/delete (id=${student.id})}" method="post">
<h3>[[${student.name}]] ๋์ ์ ๋ณด๋ฅผ ์ญ์ ํฉ๋๋ค.</h3>
<!-- create / update ์๋ ๋ค๋ฅด๊ฒ -->
<!-- delete์ ๊ฒฝ์ฐ ์ถ๊ฐ์ ์ธ ๋ฐ์ดํฐ ํ์ํ์ง ์์ -->
<!-- ๊ทธ๋์ ์ถ๊ฐ์ ์ธ input ์์ ์์ด submit ๋ง ๋ง๋ค๋ฉด ๋จ-->
<input type="submit" value="Delete" />
</form>
<a th:href="@{/{id}(id=${student.id})}">Back</a> <!--read ํ์ด์ง๋ก-->
</body>
</html>