Spring boot

(스프링 부트 입문) 스프링 부트 익명 블로그 만들어 보기(네이티브 쿼리만사용) 1. 게시글 작성 - 네이티브 쿼리 활용

mynote6676 2025. 6. 20. 23:12

 

| 화면 확인

 

 

 

-화면 게시글 화면 코드 확인하기

| tempiates/board/save-form.mustache 파일

{{> layout/header}}

<div class="container p-5">
    <div class="card">
        <div class="card-header"><b>글쓰기 화면입니다</b></div>
        <div class="card-body">
            <!--
            action="/board/save": 폼 제출시 /board/save 경로로 요청
            method="post": HTTP POST 방식으로 데이터 전송
            GET: URL에 데이터 노출, POST: 요청 본문에 데이터 숨김
            -->
            <form action="/board/save" method="post">
                <!--
                name 속성: 서버에서 받을 때 사용하는 변수명
                name="username" → 컨트롤러에서 String username으로 받음
                -->
                <div class="mb-3">
                    <input type="text" class="form-control" placeholder="Enter username" name="username">
                </div>
                <div class="mb-3">
                    <input type="text" class="form-control" placeholder="Enter title" name="title">
                </div>
                <div class="mb-3">
                    <!-- textarea: 여러 줄 텍스트 입력 가능 -->
                    <textarea class="form-control" rows="5" name="content"></textarea>
                </div>
                <!-- 버튼 클릭시 폼이 제출됨 -->
                <button class="btn btn-primary form-control">글쓰기완료</button>
            </form>
        </div>
    </div>
</div>

{{> layout/footer}}

 

BoardController 컨트롤러에서 데이터 받아 보기

package com.tenco.blog.controller;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller // IoC 대상 - 싱글톤 패턴으로 관리 됨
public class BoardController {


    // username, title, content <--- DTO 받는 방법, 기본 데이터 타입 설정
    // form 태그에서 넘어오는 데이터 받기
    // form 태그에 name 속성에에 key 값 동일해야 함
    // http://localhost:8080/board/save
    // 스프링 부트 기본 파싱 전략 - key=value (form)
    @PostMapping("/board/save")
    public String save(@RequestParam("username") String username,
                       @RequestParam("title")String title,
                       @RequestParam("content")String content) {
        System.out.println("title : " + title);
        System.out.println("content : " + content);
        System.out.println("username : " + username);
        return "redirect:/";
    }


    @GetMapping({"/", "/index"})
    public String index() {
        //    prefix: /templates/
        //    return : index
        //    suffix: .mustache
        //    # 기본 경로를 src/main/resources/templates/index.mustache
        return "index";
    }

    @GetMapping("/board/save-form")
    public String saveFrom() {
        // /templates//board
        // /templates/board/
        return "board/save-form";
    }

    //

    /**
     * 상세보기 화면 요청
     * board/1
     */
    @GetMapping("/board/{id}")
    public String detail(@PathVariable(name = "id") Integer id) {
        // URL 에서 받은 id 값을 사용해서 특정 게시글 상세보기 조회
        // 실제로는 이 id값으로 데이터베이스에 있는 게시글 조회 하고
        // 머스태치 파일로 데이터를 내려 주어야 함 (Model)
        return "board/detail";
    }

}

 

 

728x90