form 데이터 DTO 로 받기
입력 폼 만들기
templates 폴더아래에 -> articles 폴더 생성 -> new.mustache 뷰페이지 생성
코드 작성
{{>layouts/header}}
<form action="">
<input type="text">
<textarea ></textarea>
<button type="submit">Submit</button>
</form>
{{>layouts/footer}}
Controller 만들기
controller 폴더에 -> ArticleController 자바클래스 생성
package com.example.firstproject.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class ArticleController {
@GetMapping("/articles/new")
public String newArticleForm(){
return "articles/new";
}
}
import 는 controller , getmapping 선언하면 자동으로 들어가는거임
/articles/new 에 접속해보면
안예쁘니까 new.mustache 수정
{{>layouts/header}}
<form class="container">
<div class="mb-3">
<label class="form-label">제목</label>
<input type="text" class="form-control">
</div>
<div class="mb-3">
<label class="form-label">내용</label>
<textarea class="form-control" row="3"></textarea>
</div>
<button type="submit">Submit</button>
</form>
{{>layouts/footer}}
다만 submit 은 form action 을 지정해놓지 않아서 아무 기능이 없음
form 데이터 전송하기
new.mustache 에 form action과 method 추가
<form action="/articles/create" method="post" class="container">
create 로 지정된 주소가 아직 없기때문에 컨트롤러에서 메서드추가와 맵핑을 해줘야한다
form 데이터를 Post 방식으로 보내기 위해 @PostMapping 사용
package com.example.firstproject.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class ArticleController {
@GetMapping("/articles/new")
public String newArticleForm(){
return "articles/new";
}
@PostMapping("/articles/create") //추가한 부분
public String createArticle(){
return "";
}
}
'Java' 카테고리의 다른 글
SpringBoot 공부 - DTO -> DB (0) | 2023.04.25 |
---|---|
SpringBoot 공부 - DTO (0) | 2023.04.25 |
SpringBoot 공부 - 뷰 템플릿 (부트스트랩 적용) (0) | 2023.04.24 |
SpringBoot 공부 - 뷰 , 컨트롤러 , 모델 추가 (0) | 2023.04.23 |
Window11 모든프로그램 항상 관리자권한으로 실행 (0) | 2023.04.23 |