스터디를 통해 스프링부트와 AWS로 혼자 구현하는 웹 서비스(저자 이동욱) 서적을 공부하는 중입니다.
공부/실습한 내용을 정리한 포스팅입니다.
책에 모르는 부분이 있으면 구글링하거나 챗gpt에 물어봐서 보충하였습니다.
(아직 초보라 모르는 부분이 많아 이것저것 다 적었습니다.)
참고한 사이트 출처는 포스팅 맨 하단에 적었습니다.
jquery
: JavaScript 언어를 간편하게 사용할 수 있도록 단순화시킨 오픈 소스 기반의 JavaScript 라이브러리.Bootstrap
: 웹 프로젝트 손쉬운 개발을 위한 HTML, CSS, JavaScript 프레임워크.layout
디렉토리 생성header.mustache
footer.mustache
생성header.mustache
에 아래와 같이 작성<html>
<head>
<title>스프링 부트 웹 서비스</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
</head>
<body>
footer.mustache
에 아래와 같이 작성<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/min.js"></script>
</body>
</html>
index.mustache
를 아래와 같이 작성{{>layout/header}}
<h1>스프링 부트로 시작하는 웹 서비스</h1>
{{>layout/footer}}
{{>layout/header}}
: 현재 mustache 파일 기준으로 다른 파일을 가져옴.index.mustache
에 등록 버튼 추가{{>layout/header}}
<h1>스프링 부트로 시작하는 웹 서비스 Ver.2</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>
</div>
{{>layout/footer}}
IndexController.java
에 아래의 코드 추가 @GetMapping("/posts/save")
public String postsSave() {
return "posts-save";
}
posts-save.mustache
를 templates
디렉토리 바로 아래에 생성 후 아래의 코드 작성{{>layout/header}}
<h1>게시글 등록</h1>
<div class="col-md-12">
<div class="col-md-4">
<form>
<div class="form-group">
<lable for="title"> 제목</lable>
<input type="text" class="form-control" id="title" placeholder="제목을 입력하세요">
</div>
<div class="form-group">
<label for="author">작성자</label>
<input type="text" class="form-control" id="author" placeholder="작성자를 입력하세요">
</div>
<div class="form-group">
<label for="cotent">내용</label>
<textarea class="form-control" id="content" placeholder="내용을 입력하세요"></textarea>
</div>
</form>
<a href="/" role="button" class="btn btn-secondary">취소</a>
<button type="button" class="btn btn-primary" id="btn-save">등록</button>
</div>
</div>
{{>layout/footer}}
index.js
파일 생성 후 아래의 코드 작성var main = {
init : function () {
var _this = this;
$('#btn-save').on('click', function () {
_this.save();
});
},
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));
});
}
}
main.init();
footer.mustache
에 index.js
를 추가<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/min.js"></script>
<!--index.js 추가-->
<script src="/js/app/index.js"></script>
</body>
</html>
window.location.href
: 이동 페이지 url을 대입하면 자동으로 해당 페이지로 이동main
변수를 따로 둬서 function 추가한 이유제이쿼리(jQuery) - 코딩의 시작, TCP School
부트스트랩(Bootstrap)이란?
CDN이란 무엇입니까?
Ajax 요청에서 dataType 과 contentType은 뭐가 다른걸까?
[Jquery ajax] Jquery ajax 옵션 정리 !