html
Spring Boot μ ν리μΌμ΄μ μμ Get Post κΈ°λ₯ ꡬν
λͺ©μ°¨
- μκ°
- νλ‘μ νΈ κ΅¬μ‘° μ€μ
- Post Controller μμ±
- μλΉμ€ κ³μΈ΅ ꡬν
- λ·° ν νλ¦Ώ κ°λ°
- ꡬν ν μ€νΈ
- κ²°λ‘
μκ°
μΉ κ°λ° μμμμ, μ½ν μΈ λ₯Ό λμ μΌλ‘ κ΄λ¦¬νκ³ νμνλ κ²μ κΈ°λ³Έμ μΈ μꡬμ¬νμ λλ€. Java κΈ°λ° μ ν리μΌμ΄μ μ ꡬμΆνκΈ° μν κ°λ ₯ν νλ μμν¬μΈ Spring Bootλ μ΄λ₯Ό μννκ² λ¬μ±ν μ μλ κ°λ ₯ν λꡬλ₯Ό μ 곡ν©λλ€. μ΄ eBookμ Spring Boot μ ν리μΌμ΄μ μμ "Get Post" κΈ°λ₯μ ꡬννλ λ°©λ²μ λ€λ£¨λ©°, κΈ°μ΄ μ§μμ κ°μ§ μ΄λ³΄ κ°λ°μλ€μ μ΄ κ³Όμ μ ν΅ν΄ μλ΄ν©λλ€.
Get Post κΈ°λ₯μ μ€μμ±
κ³ μ μλ³μλ₯Ό κΈ°λ°μΌλ‘ νΉμ κ²μλ¬Όμ κ°μ Έμ€λ κ²μ λμ μ΄κ³ μ¬μ©μ λ°μμ±μ΄ λμ μ ν리μΌμ΄μ μ λ§λλ λ° μ€μν μν μ ν©λλ€. λΈλ‘κ·Έ, ν¬λΌ, λλ μ΄λ€ μ½ν μΈ μ€μ¬μ νλ«νΌμ΄λ μ§ κ°μ, κ²μλ¬Όμ ν¨μ¨μ μΌλ‘ κ²μνκ³ νμνλ λ₯λ ₯μ μ¬μ©μ κ²½νκ³Ό μ°Έμ¬λ₯Ό ν₯μμν΅λλ€.
μ₯μ κ³Ό λ¨μ
μ₯μ :
- λμ μ½ν μΈ μ 곡: μ¬μ©μ μμ²μ λ°λΌ μ½ν μΈ λ₯Ό κ°μ Έμ κ΄λ ¨μ±μ 보μ₯ν©λλ€.
- νμ₯μ±: μ±λ₯ μ ν μμ΄ λ§μ κ²μλ¬Όμ ν¨μ¨μ μΌλ‘ κ΄λ¦¬ν©λλ€.
- ν₯μλ μ¬μ©μ κ²½ν: νΉμ μ½ν μΈ λ₯Ό μ¬μ©μμκ² μ 곡νμ¬ λΆλ¬μ€λ μκ°μ λ¨μΆν©λλ€.
λ¨μ :
- 볡μ‘μ±: Controller, Service, Repositoryμ λν μ΄ν΄κ° νμν©λλ€.
- μ€λ₯ μ²λ¦¬: κ²μλ¬Όμ΄ μ‘΄μ¬νμ§ μμ κ²½μ° μΆκ° λ Όλ¦¬κ° νμν©λλ€.
μΈμ μ μ΄λμ μ¬μ©ν κ²μΈκ°
λμ μ½ν μΈ κ²μμ΄ νμν μ ν리μΌμ΄μ μ Get Post κΈ°λ₯μ ꡬννμμμ€. λΈλ‘κ·Έ, λ΄μ€ ν¬νΈ, μ¬μ©μ νΉμ μ½ν μΈ νμκ° νμν νλ«νΌμ μ΄μμ μ λλ€.
νλ‘μ νΈ κ΅¬μ‘° μ€μ
μ μ‘°μ§λ νλ‘μ νΈ κ΅¬μ‘°λ μ μ§λ³΄μμ±κ³Ό νμ₯μ±μ μν΄ νμμ μ λλ€. μ μ ν μ‘°μ§μ Controller, Service, Viewμ κ°μ λ€μν κ΅¬μ± μμκ° μ½κ² μ κ·Ό κ°λ₯νκ³ κ΄λ¦¬λ μ μλλ‘ λ³΄μ₯ν©λλ€.
λ·° μ‘°μ§
μ μ μ¦κ°νλ λ·° μλ₯Ό κ΄λ¦¬νκΈ° μν΄ κΈ°λ₯μ λ°λΌ μΉ΄ν κ³ λ¦¬ννλ κ²μ΄ μ’μ΅λλ€. μλ₯Ό λ€μ΄, ν, κ΄λ¦¬, κ³μ κ³Ό κ΄λ ¨λ λ·°λ λ³λμ ν΄λμ λ°°μΉν μ μμ΅λλ€:
1 2 3 4 5 6 7 8 |
templates/ β βββ fragments/ βββ home_views/ βββ admin_views/ βββ account_views/ βββ post_views/ |
μ΄ κ³μΈ΅μ ꡬ쑰λ λͺ νμ±μ λμ΄κ³ νλ‘μ νΈ λ΄μμμ λ€λΉκ²μ΄μ μ μ©μ΄νκ² ν©λλ€.
Post Controller μμ±
Controllerλ μ¬μ©μ μΈν°νμ΄μ€μ λ°±μλ λ‘μ§ κ°μ λ€λ¦¬ μν μ ν©λλ€. λ€μ΄μ€λ μμ²μ μ²λ¦¬νκ³ , Service κ³μΈ΅κ³Ό μνΈ μμ©νλ©°, μ μ ν μλ΅μ λ°νν©λλ€.
Get Post λ©μλ μ μ
PostControllerμ GET μμ²μ μ²λ¦¬νλ κ³΅κ° λ©μλλ₯Ό μμ±νμ¬ νΉμ κ²μλ¬Όμ μ²λ¦¬νμμμ€:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
// PostController.java @Controller public class PostController { @Autowired private PostService postService; @GetMapping("/posts/{id}") public String getPost(@PathVariable Long id, Model model) { Optional<Post> optionalPost = postService.getById(id); if (optionalPost.isPresent()) { Post post = optionalPost.get(); model.addAttribute("post", post); return "post_views/post"; } else { return "404"; } } } |
μ€λͺ :
- @Controller: μ΄ ν΄λμ€κ° μΉ μ»¨νΈλ‘€λ¬ μν μ ν¨μ λνλ λλ€.
- @Autowired: PostServiceλ₯Ό μ£Όμ νμ¬ κ²μλ¬Ό λ°μ΄ν°μ μνΈ μμ©ν©λλ€.
- @GetMapping("/posts/{id}"): νΉμ IDλ₯Ό κ°μ§ GET μμ²μ μ΄ λ©μλμ 맀νν©λλ€.
- @PathVariable: URL μΈκ·Έλ¨ΌνΈ {id}λ₯Ό λ©μλ 맀κ°λ³μ idμ λ°μΈλ©ν©λλ€.
- Model: λ°μ΄ν°λ₯Ό λ·°μ μ λ¬νλ μν μ ν©λλ€.
- Optional<Post>: κ²μλ¬Όμ΄ μ‘΄μ¬νκ±°λ μ‘΄μ¬νμ§ μμ μ μλ μλ리μ€λ₯Ό μ²λ¦¬ν©λλ€.
μ νμ μΈ Post μ²λ¦¬
Optional<Post>μ μ¬μ©νμ¬ λ©μλλ μ§μ λ IDλ₯Ό κ°μ§ κ²μλ¬Όμ΄ μ‘΄μ¬νμ§ μλ κ²½μ°λ₯Ό μ°μνκ² μ²λ¦¬νλ©°, 404 μ€λ₯ νμ΄μ§λ₯Ό λ°νν©λλ€.
μλΉμ€ κ³μΈ΅ ꡬν
Service κ³μΈ΅μ λΉμ¦λμ€ λ‘μ§μ μΊ‘μννλ©°, Repositoryμ μνΈ μμ©νμ¬ λ°μ΄ν°λ₯Ό κ°μ Έμ€κ³ μ‘°μν©λλ€.
PostService ν΅ν©
PostServiceκ° IDλ‘ κ²μλ¬Όμ κ²μνλ λ©μλλ₯Ό μ 곡νλμ§ νμΈνμμμ€:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
// PostService.java @Service public class PostService { @Autowired private PostRepository postRepository; public Optional<Post> getById(Long id) { return postRepository.findById(id); } } |
μ€λͺ :
- @Service: μ΄ ν΄λμ€κ° λΉμ¦λμ€ κΈ°λ₯μ μ 곡ν¨μ λνλ λλ€.
- PostRepository: λ°μ΄ν°λ² μ΄μ€μ μνΈ μμ©νμ¬ CRUD μμ μ μνν©λλ€.
- getById(Long id): κ³ μ IDμ κΈ°λ°νμ¬ κ²μλ¬Όμ κ°μ Έμ΅λλ€.
λ·° ν νλ¦Ώ κ°λ°
λ·°λ μ¬μ©μμκ² λ°μ΄ν°λ₯Ό νμνλ μν μ ν©λλ€. μ§κ΄μ μ΄κ³ λ°μν ν νλ¦Ώμ μμ±νμ¬ μνν μ¬μ©μ κ²½νμ 보μ₯ν©λλ€.
Post View
post_views ν΄λ λ΄μ post.html νμΌμ μμ±νμ¬ κ²μλ¬Ό μΈλΆ μ 보λ₯Ό νμνμμμ€:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<!-- post_views/post.html --> <!DOCTYPE html> <html lang="en"> <head> <th:block th:replace="fragments/head :: head"></th:block> </head> <body> <th:block th:replace="fragments/header :: header"></th:block> <div class="container"> <h1 th:text="${post.title}">Post Title</h1> <p th:text="${post.content}">Post Content</p> </div> <th:block th:replace="fragments/footer :: footer"></th:block> <script src="/static/js/bootstrap.js"></script> <script src="/static/js/custom.js"></script> </body> </html> |
μ€λͺ :
- Thymeleaf: Thymeleaf ν νλ¦Ών μ μ¬μ©νμ¬ λμ μ½ν μΈ λ₯Ό λ λλ§ν©λλ€.
- Fragments: head, header, footerμ κ°μ κ³΅ν΅ νλκ·Έλ¨ΌνΈλ₯Ό κ°μ Έμ μΌκ΄μ±μ μ μ§ν©λλ€.
- ${post.title} & ${post.content}: κ²μλ¬Όμ μ λͺ©κ³Ό λ΄μ©μ λμ μΌλ‘ νμν©λλ€.
404 μ€λ₯ νμ΄μ§
κ²μλ¬Όμ΄ μ°Ύμ μ μλ κ²½μ°λ₯Ό μ²λ¦¬νκΈ° μν΄ 404.html νμΌμ μμ±νμμμ€:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<!-- templates/404.html --> <!DOCTYPE html> <html lang="en"> <head> <th:block th:replace="fragments/head :: head"></th:block> </head> <body> <th:block th:replace="fragments/header :: header"></th:block> <div class="container"> <h3>νμ΄μ§λ₯Ό μ°Ύμ μ μμ΅λλ€</h3> <p>μμ²νμ κ²μλ¬Όμ΄ μ‘΄μ¬νμ§ μμ΅λλ€.</p> </div> <th:block th:replace="fragments/footer :: footer"></th:block> <script src="/static/js/bootstrap.js"></script> <script src="/static/js/custom.js"></script> </body> </html> |
μ€λͺ :
- μ¬μ©μ μΉνμ μΈ λ©μμ§: μμ²ν κ²μλ¬Όμ΄ μμμ λͺ ννκ² μ¬μ©μμκ² μ립λλ€.
- μΌκ΄λ λ μ΄μμ: κ³΅ν΅ νλκ·Έλ¨ΌνΈλ₯Ό ν¬ν¨νμ¬ μ ν리μΌμ΄μ μ μ 체μ μΈ λͺ¨μμ μ μ§ν©λλ€.
ꡬν ν μ€νΈ
Controllerμ λ·°λ₯Ό μ€μ ν νμλ λͺ¨λ κΈ°λ₯μ΄ μμλλ‘ μλνλμ§ νμΈνκΈ° μν΄ ν μ€νΈνλ κ²μ΄ μ€μν©λλ€.
CSS λ‘λ© λ¬Έμ μ²λ¦¬
ν μ€νΈ μ€μ CSS νμΌμ΄ μ¬λ°λ₯΄κ² λ‘λλμ§ μλ λ¬Έμ κ° λ°μν μ μμ΅λλ€. μ΄λ MIME μ ν νμΈκ³Ό κ΄λ ¨λ λΈλΌμ°μ μ½μμ μμμΉ λͺ»ν μ€λ₯λ‘ λνλ μ μμ΅λλ€.
ν΄κ²°μ± :
- κ²½λ‘ νμΈ: HTML ν νλ¦Ώμμ CSS νμΌμ κ²½λ‘κ° μ¬λ°λ₯Έμ§ νμΈνμμμ€.
- MIME μ ν: μλ²κ° μ¬λ°λ₯Έ MIME μ νμΌλ‘ μ μ 리μμ€λ₯Ό μ 곡νλλ‘ κ΅¬μ±νμμμ€.
- λΈλΌμ°μ μΊμ: λΈλΌμ°μ μΊμλ₯Ό μ§μ μ€λλ 리μμ€λ₯Ό λ‘λνμ§ μλλ‘ ν©λλ€.
μ€λ₯ μμ:
1 2 |
Refused to apply style from '/static/css/bootstrap.css' because its MIME type ('text/plain') is not a supported stylesheet MIME type, and strict MIME checking is enabled. |
λ¬Έμ ν΄κ²° λ¨κ³:
- νμΌ μμΉ νμΈ: CSS νμΌμ΄ src/main/resources/static/css/ λλ ν 리μ λ°°μΉλμ΄ μλμ§ νμΈνμμμ€.
- Spring Boot ꡬμ±: Spring Bootλ κΈ°λ³Έμ μΌλ‘ /static λλ ν 리μμ μ μ μ½ν μΈ λ₯Ό μ 곡ν©λλ€. μ΄ λμμ μ¬μ μνλ μ¬μ©μ μ μ ꡬμ±μ΄ μλμ§ νμΈνμμμ€.
- νμΌ κΆν: μλ²κ° CSS νμΌμ μ κ·Όνκ³ μ΄λ₯Ό μ 곡ν μ μλ νμν κΆνμ κ°μ§κ³ μλμ§ νμΈνμμμ€.
κ²°λ‘
Spring Boot μ ν리μΌμ΄μ μμ Get Post κΈ°λ₯μ ꡬννλ κ²μ Controller, Service, Repository, Viewμ κ°μ λ€μν κ΅¬μ± μμλ₯Ό μ‘°μ¨νμ¬ λμ μ΄κ³ μ¬μ©μ νΉμ μ½ν μΈ λ₯Ό μ 곡νλ κ²μ ν¬ν¨ν©λλ€. μ΄ eBookμμ μ μν ꡬ쑰νλ μ κ·Ό λ°©μμ λ°λ₯΄λ©΄, κ°λ°μλ μ¬μ©μ μ°Έμ¬μ κ²½νμ ν₯μμν€λ ν¨μ¨μ μ΄κ³ νμ₯ κ°λ₯ν μ ν리μΌμ΄μ μ λ§λ€ μ μμ΅λλ€.
ν΅μ¬ μμ
- ꡬ쑰νλ νλ‘μ νΈ μ‘°μ§: μ μ§λ³΄μμ±κ³Ό νμ₯μ±μ μ©μ΄νκ² ν©λλ€.
- κ°λ ₯ν μ€λ₯ μ²λ¦¬: μ‘΄μ¬νμ§ μλ 리μμ€λ₯Ό μ°μνκ² κ΄λ¦¬ν©λλ€.
- λμ μ½ν μΈ λ λλ§: κ΄λ ¨ μ½ν μΈ λ₯Ό μ μνκ² μ 곡νμ¬ μ¬μ©μ κ²½νμ ν₯μμν΅λλ€.
- μ² μ ν ν μ€νΈ: μνν κΈ°λ₯μ 보μ₯νκΈ° μν΄ λ¬Έμ λ₯Ό μλ³νκ³ ν΄κ²°ν©λλ€.
SEO ν€μλ: Spring Boot tutorial, Get Post feature, Spring Boot controller, Spring Boot service, dynamic content in Spring, handling 404 in Spring Boot, Spring Boot views, Thymeleaf templates, Spring Boot application structure, Java web development.
μ°Έκ³ : μ΄ κΈ°μ¬λ AIμ μν΄ μμ±λμμ΅λλ€.