1 ๋ถ„ ์†Œ์š”

Mapping์ด๋ž€?

์›น์—์„œ ๋งคํ•‘์ด๋ž€ ํ•ด๋‹น ๊ฐ’์ด ๋‹ค๋ฅธ ๊ฐ’์„ ๊ฐ€๋ฆฌํ‚ค๋„๋ก ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.
์˜ˆ๋ฅผ๋“ค์–ด, http://localhost:8080/web.html ์ด๋ผ๋Š” ํŽ˜์ด์ง€๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค๊ณ  ํ•˜๋ฉด ์ด url์ด ๊ทธ๋Œ€๋กœ ๋…ธ์ถœ๋˜๊ฒŒ ๋œ๋‹ค.
๊ทธ๋ ‡๊ฒŒ๋˜๋ฉด ๋ณด์•ˆ์ƒ ๋งค์šฐ ์ทจ์•ฝํ•  ์ˆ˜ ์žˆ๋‹ค.
์ด๋Ÿฐ ์ทจ์•ฝ์ ์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด web.html์ด ์•„๋‹Œ w์ด web.html๊ณผ ๊ฐ™๋‹ค๊ณ  ์„ค์ •ํ•˜์—ฌ ๋ณด์•ˆ์„ฑ์„ ๋†’์ผ ์ˆ˜ ์žˆ๋‹ค.

Spring์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋งคํ•‘ ์• ๋…ธํ…Œ์ด์…˜

๋ณธ๊ฒฉ์ ์œผ๋กœ ๋งคํ•‘ ์• ๋…ธํ…Œ์ด์…˜๋“ค์— ๋Œ€ํ•ด ์ž‘์„ฑํ•œ๋‹ค.

@RequestMapping

์š”์ฒญ URL์„ ์–ด๋–ค ๋ฉ”์†Œ๋“œ๊ฐ€ ์ฒ˜๋ฆฌํ• ์ง€ ๋งคํ•‘ํ•ด์ฃผ๋Š” ์• ๋…ธํ…Œ์ด์…˜์ด๋‹ค.
url๋ณ„๋กœ ๋‹ค๋ฅด๊ฒŒ ์š”์ฒญ์ด ๋“ค์–ด์˜ค๋ฉด ๋‹ค๋ฅธ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์•ผํ•˜๋Š” ๊ฒƒ์ด ๋‹น์—ฐํ•˜๋‹ค.
~/hello/junho ์™€ ๊ฐ™์€ ์š”์ฒญ๊ณผ /hi/kong์˜ ์š”์ฒญ์€ ์—„์—ฐํžˆ ๋‹ค๋ฅด๋‹ค.
/hello์™€ /hi๋กœ ์‹œ์ž‘ํ•˜๋Š” ์š”์ฒญ์„ ๊ฐ๊ฐ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด @RequestMapping์„ ์‚ฌ์šฉํ•œ๋‹ค.

@GetMapping

Http Method์—๋Š” CRUD๋ฅผ ์œ„ํ•œ GET, POST, PUT, DELETE๊ฐ€ ์กด์žฌํ•œ๋‹ค.
@GetMapping์€ ์ด ์ค‘์— Get Method ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.
@GetMapping์ด ๋ถ™์€ ๋ฉ”์†Œ๋“œ๋Š” ์š”์ฒญ์— ๋Œ€ํ•œ ๊ฐ’์„ ์กฐํšŒํ•˜๋Š” ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.

[์˜ˆ์ œ์ฝ”๋“œ]

    @GetMapping("/")
    public String list(Model model) {
        List<BoardDto> boardDtoList = boardService.getBoardlist();
        model.addAttribute("boardList", boardDtoList);

        return "board/list.html";
    }

@PostMapping

@PostMapping์€ ๋‹น์—ฐํžˆ Post Method ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.
@PostMapping์ด ๋ถ™์€ ๋ฉ”์†Œ๋“œ๋Š” ์š”์ฒญ์— ๋Œ€ํ•ด ๊ฐ’์„ ์ˆ˜์ •, ์‚ญ์ œ ๋“ฑ์˜ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

[์˜ˆ์ œ์ฝ”๋“œ]

    @PostMapping("/post")
    public String write(BoardDto boardDto) {
        boardService.savePost(boardDto);
        return "redirect:/";
    }

@PutMapping

@PutMapping์€ Put Method ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•˜๊ณ , Put์€ ๋ฐ์ดํ„ฐ ์ˆ˜์ •์„ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค.
ํ•˜์ง€๋งŒ ๋ณด์•ˆ์ƒ์˜ ์ด์œ ๋กœ Put๋ฉ”์†Œ๋“œ๋ณด๋‹จ Post๋ฉ”์†Œ๋“œ๋ฅผ ์„ ํ˜ธํ•œ๋‹ค๊ณ  ํ•œ๋‹ค.

[์˜ˆ์ œ์ฝ”๋“œ]

    @PutMapping("/post/edil/{no}")
    public String update(BoardDto boardDto) {
        boardService.savePost(boardDto);
        return "redirect:/";
    }

@DeleteMapping

@DeleteMapping๋˜ํ•œ Delete Method ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•˜๋ฉฐ, ๋ฐ์ดํ„ฐ์˜ ์‚ญ์ œ๋ฅผ ๋‹ด๋‹นํ•œ๋‹ค.
Delete๋„ Post๋กœ ๋Œ€์ฒด ๊ฐ€๋Šฅํ•˜๋ฉฐ, ๋ณด์•ˆ์ƒ์˜ ์ด์œ ๋กœ Post๋ฅผ ์„ ํ˜ธํ•œ๋‹ค๊ณ  ํ•œ๋‹ค.

[์˜ˆ์ œ์ฝ”๋“œ]

    @DeleteMapping("/post/{no}")
    public String delete(@PathVariable("no") Long id) {
        boardService.deletePost(id);

        return "redirect:/";
    }

์ฐธ๊ณ ์ž๋ฃŒ

Web์—์„œ ๋งคํ•‘์ด๋ž€? JSP Mapping

[Spring Boot + JPA + MySQL] ๊ฒŒ์‹œ๊ธ€ ์ˆ˜์ • & ์‚ญ์ œ

ํƒœ๊ทธ: ,

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ:

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ