![article thumbnail image](https://blog.kakaocdn.net/dn/dlLDRj/btrP6cXTd3D/XiezPeROd5k0IbRHG2ZQjK/img.png)
728x90
게시글 만들기 기초
폼데이터 주고 받기
-게시글 쓰기
1. 글작성 페이지 만들기
new.mustache
{{>layouts/header}}
<form class="container" action="/articles/create" method="post">
<div class="mb-3">
<label class="form-label">제목</label>
<input type="text" class="form-control" name="title">
</div>
<div class="mb-3">
<label class="form-label">내용</label>
<textarea class="form-control" rows="3" name="content"></textarea>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
{{>layouts/footer}}
2. 글작성 컨트롤러 만들기
ArticleController 생성
package com.example.firstproject.controller;
import com.example.firstproject.dto.ArticleForm;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
@Controller
public class ArticleController {
@GetMapping("/articles/new")
public String newArticleForm(){
return "articles/new";
}
@PostMapping("/articles/create")
public String createArticle(ArticleForm form){
System.out.println(form.toString());
return "";
}
}
3. 폼데이터 전송
<form class="container" action="/articles/create" method="post">
action에 경로 지정하고 method는 post방식으로 보낸다!
4. 폼데이터 받기
<input type="text" class="form-control" name="title">
<textarea class="form-control" rows="3" name="content"></textarea>
name값을 지정해서 데이터를 넘겨준다
넘긴 데이터는 컨트롤러의
public String createArticle(ArticleForm form)
의 ArticleForm form에 저장이된다
저장된값을 form.toString()으로 보내 값을 콘솔창에 출력해본다
잘 출력이 되었다!
728x90
'Study > SpringBoot WebProject' 카테고리의 다른 글
Spring Boot DB테이블과 SQL (0) | 2022.11.03 |
---|---|
Spring Boot 데이터 생성 with JPA (0) | 2022.11.01 |
Spring Boot 뷰 템플릿/ 레이아웃 (0) | 2022.11.01 |
Spring Boot MVC역할과 흐름 (0) | 2022.11.01 |
Spring Boot 기초 설치 및 기본화면 출력 (0) | 2022.11.01 |