
728x90
목록에서 새 글작성 페이지로
{{>layouts/header}}
<table class="table">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">Title</th>
<th scope="col">Content</th>
</tr>
</thead>
<tbody>
{{#articleList}}
<tr>
<th>{{id}}</th>
<th><a href="/articles/{{id}}">{{title}}</a></th>
<td>{{content}}</td>
</tr>
{{/articleList}}
</tbody>
</table>
<a href="/articles/new">new article</a>
{{>layouts/footer}}
목록페이지로 돌아가기
{{>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>
<a href="/articles">back</a>
</form>
{{>layouts/footer}}
리다이렉트 - 새 글저장 후, 상세 페이지로
@PostMapping("/articles/create")
public String createArticle(ArticleForm form){
log.info(form.toString());
//System.out.println(form.toString());
//1. DTO를 Entity로 변환
Article article = form.toEntity();
log.info(article.toString());
//System.out.println(article.toString());
//2. Repository에게 Entity를 DB안에 저장
Article saved = articleRepository.save(article);
log.info(saved.toString());
//System.out.println(saved.toString());
return "redirect:/articles/"+saved.getId();
}
package com.example.firstproject.entity;
import lombok.*;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Entity // DB가 해당 객체를 인식 가능하게 한다
@Data
@Getter // 롬복으로 게터 추가
public class Article {
@Id // 대표값을 지정한다 like 주민번호
@GeneratedValue //1,2,3,... 자동생성 어노테이션!
private Long id;
@Column
private String title;
@Column
private String content;
}
상세 페이지에서 목록으로
{{>layouts/header}}
<table class="table">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">Title</th>
<th scope="col">Content</th>
</tr>
</thead>
<tbody>
{{#article}}
<tr>
<th>{{id}}</th>
<td>{{title}}</td>
<td>{{content}}</td>
</tr>
{{/article}}
</tbody>
</table>
<a href="/articles">Go to Article List</a>
{{>layouts/footer}}
728x90
'Study > SpringBoot WebProject' 카테고리의 다른 글
Spring Boot 수정 폼 만들기 (0) | 2022.12.09 |
---|---|
Spring Boot 데이터 목록 조회하기 (0) | 2022.12.09 |
Spring boot 데이터 조회하기 with JPA (0) | 2022.12.09 |
Spring Boot lombok과 refactoring (1) | 2022.11.03 |
Spring Boot DB테이블과 SQL (0) | 2022.11.03 |