inblog logo
|
sson17
    스프링부트(Spring Boot)

    [스프링부트(Spring Boot)]8. 스프링부트 블로그 v1

    손영민's avatar
    손영민
    Apr 14, 2025
    [스프링부트(Spring Boot)]8. 스프링부트 블로그 v1
    ❗
    스스로 처음부터 끝까지 혼자서 다해보기 !!
     
     
    board
    Board.java
    0.6KB
    BoardController.java
    2.4KB
    BoardRepository.java
    1.6KB
    BoardService.java
    1.2KB
     
     
    db
    date.sql
    0.4KB
     
    템플릿
    detail.mustache
    0.4KB
    list.mustache
    0.4KB
    save-form.mustache
    0.6KB
    update-form.mustache
    0.3KB
    application.properties
    0.5KB
    레이아웃
    header.mustache
    0.4KB
     
    모델 쓰는코드
    package com.metacoding.blogv1.board; 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 java.util.List; // 책임: 요청잘받고 응답잘하고 @Controller //컴퍼넌트 스캔 -> DS가 활용 public class BoardController { private BoardService boardService; //DI : 의존성 주입 -> IoC로 부터 들고옴 public BoardController(BoardService boardService) { this.boardService = boardService; } @PostMapping("/board/{id}/update") public String update(@PathVariable("id") int id, String title, String content) { // update board_tb set title=?, content=? where id =? //주소로 받는 데이터는 where에 걸린다 System.out.println("id: " + id + " title: " + title + " content: " + content); return "redirect:/board/" + id; } @PostMapping("/board/{id}/delete") public String delete(@PathVariable int id) { System.out.println("id: " + id); //id로 db가서 삭제하면 됨. return "redirect:/"; } @PostMapping("/board/save") public String save(String title, String content) { boardService.게시글쓰기(title, content); return "redirect:/"; //주소가 만들어져있으면 리다이렉션 } @GetMapping("/") public String list(Model model) { List<Board> boards = boardService.게시글목록(); //request 담기 model.addAttribute("models", boards); return "list";// forward } @GetMapping("/board/{id}") //패턴 매칭 /board/1, 2, 3 public String detail(@PathVariable("id") int id, Model model) { Board board = boardService.게시글상세보기(id); model.addAttribute("model", board); return "detail"; } @GetMapping("/board/save-form") //주소는 (하이픈 사용) public String saveForm() { return "save-form"; } @GetMapping("/board/{id}/update-form") public String updateForm(@PathVariable("id") int id) { return "update-form"; } }
    Share article

    sson17

    RSS·Powered by Inblog