[Spring Boot] ๋“ฑ๋ก, ๋ชฉ๋ก ๋ณด๊ธฐ ๊ตฌํ˜„ํ•˜๊ธฐ

2021. 8. 14. 08:00ใ†Back-End ์ž‘์—…์‹ค/Spring Framework

728x90
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š”? ์ฃผ๋‹ˆํ•˜๋ž‘ ์ž…๋‹ˆ๋‹ค.

์˜ค๋Š˜์€ ํšŒ์›์ด๋ผ๋Š” Key word๋กœ ๋“ฑ๋ก๊ณผ ๋ชฉ๋ก ๋ณด๊ธฐ๋ฅผํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ๊ณต๋ถ€ ํ•ด ๋ณด๋„๋ก ํ•  ๊ฒƒ์ด์—์š”.

๋ฐ”๋กœ ์‹œ์ž‘ ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค!

 

์ฝ”๋“œ์— ๊ด€๋ จํ•œ ๋‚ด์šฉ์€ ์ฃผ๋‹ˆํ•˜๋ž‘์˜ Github์—์„œ ํ™•์ธ ํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!

 

 


 

๐Ÿ“‹ ๋ชฉ์ฐจ


01.[Spring Boot] thymeleaf์™€ Spring Boot
02.[Spring Boot] Spring Web ๊ฐœ๋ฐœ ๊ธฐ์ดˆ
03.[Spring Boot] ํšŒ์› ๊ด€๋ฆฌ ์˜ˆ์ œ - Backend
04.[Spring Boot] Service ๊ฐœ๋ฐœ ๋ฐ Test Case ์ž‘์„ฑ
05.[Spring Boot] Spring Bean๊ณผ ์˜์กด๊ด€๊ณ„
06.[Spring Boot] Java Code๋กœ ์ง์ ‘ Spring Bean ๋“ฑ๋ก
07.[Spring Boot] ๋“ฑ๋ก, ๋ชฉ๋ก ๋ณด๊ธฐ ๊ตฌํ˜„ํ•˜๊ธฐ
08.[Spring Boot] ๊ธฐ์กด ์ฝ”๋“œ ์† ์•ˆ๋Œ€๊ณ , ์„ค์ •์œผ๋กœ ๊ตฌํ˜„ Class ๋ณ€๊ฒฝ
09.[Spring Boot] ํ†ตํ•ฉ Test
10.[Spring Boot] JPA
11.[Spring Boot] Spring Data JPA
12.[Spring Boot] AOP

 

 

 


 

 

 

๐Ÿ“Œ ํšŒ์› ๋“ฑ๋ก


   ๐Ÿ“ ํšŒ์› ๋“ฑ๋ก Form Controller

 

 

MemberCopntroller.java

package hello.hellospring.controller;

import hello.hellospring.domain.Member;
import hello.hellospring.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;

// ์ปดํฌ๋„ŒํŠธ ์Šค์บ” ๋ฐฉ์‹์˜ Controller
@Controller     // ์ด ์–ด๋…ธํ…Œ์ด์…˜์„ ์Šคํ”„๋ง์ด ๋ณด๊ณ , ํ•ด๋‹น Controller Class๋ฅผ ๊ฐ์ฒด๋กœ ๋งŒ๋“  ๋’ค ์Šคํ”„๋ง์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š”๋‹ค.
// ๊ทธ๊ฒƒ์„ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์—์„œ ๋นˆ์œผ๋กœ ๊ด€๋ฆฌ ๋œ๋‹ค๊ณ  ํ‘œํ˜„ํ•œ๋‹ค.
public class MemberController {
        // ์•„๋ž˜์™€ ๊ฐ™์ด MemberService๋ฅผ new๋ฅผ ํ†ตํ•ด ์ƒ์„ฑํ•˜๊ฒŒ ๋˜๋ฉด ๋‹ค๋ฅธ Controller์—์„œ๋„ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•œ ๋ฌธ์ œ
//    private final MemberService memberService = new MemberService();

    private final MemberService memberService;

    // Dependency Injection (DI)
    // @controller๊ฐ€ ์žˆ๋Š” Controller ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ, @Autowired๊ฐ€ ๋ถ™์€ ์ƒ์„ฑ์ž๋Š” ์Šคํ”„๋ง์ด ์ปจํ…Œ์ด๋„ˆ์—์„œ ๋นˆ์œผ๋กœ ๊ด€๋ฆฌํ•  ๋•Œ, ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•˜๋Š”๋ฐ, ์ด ๋•Œ MemberService๋ฅผ ๋„ฃ์–ด์ค€๋‹ค.
    @Autowired
    public MemberController(MemberService memberService) {
        this.memberService = memberService;
    } // ์ƒ์„ฑ์ž () ๋

    @GetMapping("/members/new")
    public String createForm() {
        return "members/createMemberForm";
    } // createForm() ๋
} // Class ๋

 

@GetMapping์€ Client๊ฐ€ '/members/new'๋ผ๋Š” URI๋ฅผ ํ†ตํ•ด ์ ‘๊ทผ์„ ํ•˜๋ฉด Mapping์„ ์‹œ์ผœ์ฃผ๋Š” ๊ฒƒ์ด๊ณ ,
๊ทธ ํ•˜์œ„์— Method๋ฅผ ํ˜ธ์ถœํ•ด ์ฃผ๋Š” ๊ฒƒ์ด์—์š”.

createForm Method๋Š” ๋ฐ˜ํ™˜์œผ๋กœ members ๋ฐ‘์— createMemberForm.html๋กœ ๋ณด๋‚ด์ฃผ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

 

   ๐Ÿ“ ํšŒ์› ๋“ฑ๋ก Form View

 

createMemberForm.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="UTF-8">
    <title>์ฃผ๋‹ˆํ•˜๋ž‘์˜ Spring Boot Study</title>
</head>

<body>

    <div class="container">

        <form action="/members/new" method="post">
            <div class="form-group">
                <label for="name">์ด๋ฆ„</label>
                <input type="text" id="name" name="name" placeholder="์ด๋ฆ„์„ ์ž…๋ ฅ ํ•ด ์ฃผ์„ธ์š”!">
            </div>

            <button type ="submit">๋“ฑ๋ก</button>
        </form>
    </div>  <!--- /container --->

</body>
</html>

 

์ด ๊ณณ์—์„œ๋Š” thymeleaf๋ผ๋Š” ๊ฒƒ์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๊ณ , xmlns๋ฅผ ํ†ตํ•ด thymeleaf๋ฅผ th๋กœ Mapping ๋ฐ ๋“ฑ๋ก์„ ํ•˜์—ฌ ์ค€ ๊ฒƒ์ด์—์š”.

input Tag์—์„œ name์„ ์ž…๋ ฅํ•˜๊ฒŒ ๋˜๋ฉด form์˜ Action์— ์˜ํ•ด ํ•ด๋‹น ์ž…๋ ฅ๊ฐ’์ด /member/new๋กœ ์ „๋‹ฌ๋˜๊ฒŒ ๋˜๊ณ , http Method ์ค‘ Post๋กœ ์ „๋‹ฌ ๋˜๋Š” ๊ฒƒ์ด์—์š”.

 

   ๐Ÿ“ ํšŒ์› ๋“ฑ๋ก Controller

        ๐Ÿ‘‰ ์›น ๋“ฑ๋ก ํ™”๋ฉด์—์„œ Data๋ฅผ ์ „๋‹ฌ ๋ฐ›์„ Form ๊ฐ์ฒด

 

MemberForm.java

package hello.hellospring.controller;

public class MemberForm {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
} // Class ๋

์œ„์˜ createMemberForm.html์—์„œ input Tag๋กœ ์ „๋‹ฌ๋œ Name๊ฐ’์€ ํ•ด๋‹น Class์— ํ•„๋“œ ๋ณ€์ˆ˜ name์— setter๋ฅผ ํ†ตํ•ด ์ฃผ์ž… ๋˜๊ฒŒ ๋œ๋‹ต๋‹ˆ๋‹ค!

 

 

        ๐Ÿ‘‰ ํšŒ์› Controller์—์„œ ํšŒ์›์„ ์‹ค์ œ ๋“ฑ๋กํ•˜๋Š” ๊ธฐ๋Šฅ

 

MemberCopntroller.java

    @PostMapping("/members/new")
    public String create(MemberForm form) {
        Member member = new Member();
        member.setName(form.getName());

        memberService.join(member);

        // ํšŒ์› ๊ฐ€์ž…์ด ๋๋‚˜๋ฉด homeํ™”๋ฉด์œผ๋กœ ๋ณด๋‚ด๊ธฐ
        return "redirect:/";
    } // create() ๋

 

์œ„์—์„œ ์ฃผ๋‹ˆํ•˜๋ž‘์€ createMemberForm.html์—์„œ input Tag๋ฅผ ํ†ตํ•ด ์ „๋‹ฌ ๋œ name์ด๋ผ๋Š” ๊ฐ’์ด /members/new๋กœ post ๋ฐฉ์‹์œผ๋กœ ์ „๋‹ฌ ๋œ๋‹ค๊ณ  ํ•œ ๊ฒƒ์ด์—์š”.

๊ทธ ๊ฐ’์€ @PostMapping์„ ๋ณด๊ณ  ๊ทธ URI์™€ ๋งค์นญ์ด ๋˜๋Š”์ง€ ํ™•์ธ์„ ํ•œ๋‹ต๋‹ˆ๋‹ค.

๊ทธ๋Ÿฐ ๋’ค ํ•ด๋‹น Method๊ฐ€ ํ˜ธ์ถœ๋˜๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด์—์š”.

 

๋จผ์ € ์ด Methodsms Member ๊ฐ์ฒด๋ฅผ ํ•˜๋‚˜ ์ƒ์„ฑํ•ด์„œ setter๋ฅผ ํ†ตํ•ด ์ด๋ฆ„์„ ์ฃผ์ž…ํ•˜๋Š”๋ฐ, MemberForm์ด๋ผ๊ณ  ๋งŒ๋“  Class์— getter์— ์ž…๋ ฅ๋œ Name์„ ๊ฐ€์ ธ์™€์„œ ์ฃผ์ž…์„ ํ•˜๋Š” ๊ฒƒ์ด์—์š”.

 

๊ทธ๋Ÿฐ ๋‹ค์Œ MemberService์— Join Method๋ฅผ ํ˜ธ์ถœํ•˜๋Š”๋ฐ, ์ธ์ž๊ฐ’์œผ๋กœ Member ๊ฐ์ฒด (setter๋ฅผ ํ†ตํ•ด ์ „๋‹ฌ๋œ name๊ฐ’)์„ ์ „๋‹ฌํ•˜์—ฌ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•œ ๋’ค redirect๋ฅผ ํ†ตํ•ด main Page๋กœ ๊ฐ€๋ผ๊ณ  ์•Œ๋ ค์ฃผ๊ณ  ์žˆ๋Š” ๊ฒƒ์ด์—์š”.

 

์ฆ‰, ์ž‘์—…์ด ๋‹ค ๋๋‚˜๊ณ  ๋ณด๋ฉด Main Page๋กœ ์ž๋™์œผ๋กœ ์ด๋™๋˜๊ฒŒ ํ•œ ๊ฒƒ์ด๋ž๋‹ˆ๋‹ค.

 

 

๐Ÿ“Œ ํšŒ์› ์กฐํšŒ


 

   ๐Ÿ“ ํšŒ์› Controller์—์„œ ์กฐํšŒ ๊ธฐ๋Šฅ

 

MemberCopntroller.java

    @GetMapping("/members")
    public String list(Model model) {
        List<Member> members = memberService.findMembers();
        model.addAttribute("members", members);
        return "members/memberList";
    } // list () ๋

 

์œ„์˜ Method๋ฅผ ๋ณด๋ฉด Get ๋ฐฉ์‹์œผ๋กœ /members๋ผ๋Š” URI๋ฅผ ๊ฐ–๊ณ  ์˜ค๋ฉด ํ•ด๋‹น Method list๋กœ ์˜ค๊ฒŒ ๋˜๋ฉฐ, ์ตœ์ดˆ List ํ˜•์ด๋ฉฐ, ์ž๋ฃŒํ˜•์ด Member์ธ members์— memberService์˜ findMembers Method์—์„œ ์ฐพ์€ ๊ฐ’์„ ๋„ฃ์–ด ์ฃผ๋Š” ๊ฒƒ์ด์—์š”.

๊ทธ๋Ÿผ ๋ชจ๋“  ๊ฐ€์ž…๋œ ํšŒ์› ๋ชฉ๋ก์ด ๋“ค์–ด์˜ค๊ฒŒ ๋˜๊ฒ ์ง€์š”?

๊ทธ๋Ÿฐ ๋‹ค์Œ model.addAttribute๋ฅผ ํ†ตํ•ด View์— ์ „๋‹ฌํ•  ๊ฐ’์„ ์ง€์ •ํ•˜๊ฒŒ ๋˜๋Š”๋ฐ, "members"๋Š” Key๊ฐ€ ๋˜๊ณ , members๋Š” ๊ฐ’์ด ๋˜๋Š” ๊ฒƒ์ด์—์š”.

 

 

๐Ÿ’ก ์ฐธ๊ณ : Model์ด๋ž€?
Model์€ model attributes๋ฅผ ์ €์žฅํ•˜๋Š” Interface. Model์— Attributes๋ฅผ ์ถ”๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด ๊ณ ์•ˆ๋˜์—ˆ์œผ๋ฉฐ, Java.util.Map์œผ๋กœ ๋ชจ๋“  Model์— ์ ‘๊ทผ์„ ํ—ˆ์šฉ. ์ฃผ๋กœ addAttribute Method๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ Model์— ์ €์žฅ.

Business Logic ๊ฒฐ๊ณผ๋ฅผ Client View์—๊ฒŒ ์ „์†กํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š”๊ฒƒ์ด ๋ฐ”๋กœ Model

 

 

   ๐Ÿ“ ํšŒ์› List HTML

 

memberList.html

<!DOCTYPE HTML>
  <html xmlns:th="http://www.thymeleaf.org">
  <body>
  <div class="container">
      <div>
          <table>
              <thead>
  
                 <tr>
                    <th>#</th>
					<th>์ด๋ฆ„</th> </tr>
                </thead>
                
                <tbody>
                
                <tr th:each="member : ${members}">
                    <td th:text="${member.id}"></td>
                    <td th:text="${member.name}"></td>
                </tr>
                
                </tbody>
            </table>
	</div>

</div> <!-- /container -->
</body>
</html>

 

model.addAttribute๋ฅผ ํ†ตํ•ด ์ „๋‹ฌ๋œ ๊ฐ’์€ ${members}๋ฅผ ํ†ตํ•ด ๋ฐ›์€ ๊ฒƒ์ด์—์š”.

Java์˜ For Each๋ฌธ๊ณผ ๊ฐ™์ด ${members}์— ๋‹ด๊ธด ํšŒ์› ๋ชฉ๋ก ์ •๋ณด๋ฅผ ํ•˜๋‚˜์”ฉ member์— ์ „๋‹ฌํ•ด ์ฃผ๋ฉด์„œ ๋ฐ˜๋ณต์ด ๋Œ๊ฒŒ ๋˜๊ณ ,

table์— member์— ๋‹ด๊ธด id์™€ name์ด text ํ˜•์‹์œผ๋กœ ๋ณด์ผ ์ˆ˜ ์žˆ๋„๋ก ํ•ด ์ค€ ๊ฒƒ์ด์—์š”.

 

 


 

 

์ฃผ๋‹ˆํ•˜๋ž‘์˜ ๊ธ€์ด ๋งˆ์Œ์— ๋“œ์…จ๋‚˜์š”? ๊ตฌ๋…๊ณผ ๊ณต๊ฐ! ๊ทธ๋ฆฌ๊ณ , ๋Œ“๊ธ€์€ ์ฃผ๋‹ˆํ•˜๋ž‘์—๊ฒŒ ๋งŽ์€ ํž˜์ด ๋ฉ๋‹ˆ๋‹ค

 

728x90
๋ฐ˜์‘ํ˜•