[E-3-4] 게시글 수정 화면 생성
2020. 2. 1. 15:51ㆍProject E/Project E 파트3
반응형
게시글 수정
src/main/resources
templates
posts-update.mustache
더보기
{{>layout/header}}
<h1>게시글 수정</h1>
<div class="col-md-12">
<div class="col-md-4">
<form>
<div class="form-group">
<label for="id">글 번호</label>
<input type="text" class="form-control" id="id" value="{{post.id}}" readonly>
</div>
<div class="form-group">
<label for="title">제목</label>
<input type="text" class="form-control" id="title" value="{{post.title}}">
</div>
<div class="form-group">
<label for="author">작성자</label>
<input type="text" class="form-control" id="author" value="{{post.author}}" readonly>
</div>
<div class="form-group">
<label for="content">내용</label>
<textarea class="form-control" id="content">{{post.content}}</textarea>
</div>
</form>
<a href="/" role="button" class="btn btn-secondary">취소</a>
<button type="button" class="btn btn-primary" id="btn-update">수정 완료</button>
</div>
</div>
{{>layout/footer}}
src/main/resources
static.js.app
index.js
더보기
var main = {
init : function () {
var _this = this;
$('#btn-save').on('click', function() {
_this.save();
});
$('#btn-update').on('click', function() {
_this.update();
});
},
save : function (){
var data = {
title: $('#title').val(),
author: $('#author').val(),
content: $('#content').val()
};
$.ajax({
type: 'POST',
url: '/api/v1/posts',
dataType: 'json',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(data)
}).done(function(){
alert('글이 등록되었습니다.');
window.location.href = '/';
}).fail(function (error){
alert(JSON.stringify(error));
});
},
update : function () {
var data = {
title: $('#title').val(),
content: $('#content').val()
};
var id = $('#id').val();
$.ajax({
type: 'PUT',
url: '/api/v1/posts/' + id,
dataType: 'json',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(data)
}).done(function(){
alert('글이 수정되었습니다.');
window.location.href = '/';
}).fail(function (error){
alert(JSON.stringify(error))
});
}
};
main.init();
src/main/resources
template/layout
index.mustache
더보기
{{>layout/header}}
<h1>스프링 부트로 시작하는 웹 서비스 Ver 2.0</h1>
<div class="col-md-12">
<div class="row">
<div class="col-md-6">
<a href="/posts/save" role="button" class="btn btn-primary">글 등록</a>
</div>
</div>
<br/>
<!-- 목록 출력 영역 -->
<table class="table table-horizental table-bordered">
<thead class="thead-strong">
<tr>
<th>게시글 번호</th>
<th>제목</th>
<th>작성자</th>
<th>최종수정일</th>
</tr>
</thead>
<tbody id="tbody">
{{#posts}}
<tr>
<td>{{id}}</td>
<td><a href="/posts/update/{{id}}">{{title}}</a></td>
<td>{{author}}</td>
<td>{{modifiedDate}}</td>
</tr>
{{/posts}}
</tbody>
</table>
</div>
{{>layout/footer}}
src/main/resources
com.minokuma.book.springboot
web/dto
IndexController.java
더보기
package com.minokuma.book.springboot.web;
import com.minokuma.book.springboot.service.posts.PostsService;
import com.minokuma.book.springboot.web.dto.PostsResponseDto;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@RequiredArgsConstructor
@Controller
public class IndexController {
private final PostsService postsService;
@GetMapping("/")
public String index(Model model){
model.addAttribute("posts",postsService.findAllDesc());
return "index";
}
@GetMapping("/posts/save")
public String postsSave(){
return "posts-save";
}
@GetMapping("/posts/update/{id}")
public String postsUpdate(@PathVariable Long id, Model model){
PostsResponseDto dto = postsService.findById(id);
model.addAttribute("post", dto);
return "posts-update";
}
}
게시글 수정 테스트
반응형
'Project E > Project E 파트3' 카테고리의 다른 글
[E-3-5] 게시글 삭제 (0) | 2020.02.01 |
---|---|
[E-3-3] 전체 조회 화면 (0) | 2020.02.01 |
[E-3-2] 게시글 등록 화면 (0) | 2020.01.31 |
[E-3-1] 머스태쉬 설치 및 기본 페이지 작성 (0) | 2020.01.31 |