-
폼 데이터 주고 받기Back end/SpringBoot 2023. 11. 29. 14:59
{{>layouts/header}} <form class="container" action="/articles/create" method="post"> <!--어디로 보낼지 action 어떻게 보낼지 method--> <div class="mb-3"> <label class="form-label">제목</label> <input type="text" class="form-control" name="title"> <!--name에 DTO의 설정 값들을 넣어 줘야 동작됨--> </div> <div class="mb-3"> <label class="form-label">내용</label> <textarea class="form-control" rows="3" name="content"></textarea> <!--name에 DTO의 설정 값들을 넣어 줘야 동작됨--> </div> <button type="submit" class="btn btn-primary">Submit</button> </form> {{>layouts/footer}}
=> new 라는 전송폼을 만들어 주고 controller에 전달하기 위해 action값 및 method값 설정
@PostMapping("articles/create") public String createArticle(ArticleForm form){ // DTO의 AriticleForm으로 전달 System.out.println(form.toString()); return"";
=> getmapping이 아닌 postmapping 값으로 DTO의 폼으로 전달하기, system.out의 form.toString으로 전달 값 잘 전달 되었는지 확인하기
< DTO의 폼>
package com.example.firstproject.dto; public class ArticleForm { private String title; private String content; public ArticleForm(String title, String content) { this.title = title; this.content = content; } @Override public String toString() { return "ArticleForm{" + "title='" + title + '\'' + ", content='" + content + '\'' + '}'; } }
=> 생성자랑 출력을 위해 tostring 값을 만들어줌~
Mustache를 이용한 데이터 폼 전달 ~
'Back end > SpringBoot' 카테고리의 다른 글
서비스 계층과 트랜잭션 (0) 2023.12.11 HTTP와 RestController (1) 2023.12.08 뷰 템플릿과 MVC 패턴 (2) 2023.11.29 Controller -> Service -> Dao || repository -> dbms (0) 2023.11.16 @Test (0) 2023.11.14