AJAX란?
기존의 Web Application을 활용하는 모델에서는 서버로 데이터를 보내고 받는 과정 동안에는 사용자가 작업을 할 수 없지만, AJAX를 활용하면 서버와의 데이터 전송을 비동기로 진행하므로 결과를 AJAX 엔진을 통해서 진행하게 된다.

AJAX 문법
$.ajax({type : 'post',
url : '/test',
contentType : "application/json; charset=UTF-8",
data : JSON.stringify({
"no" : no,
"name" : name,
"nick" : nick }),
success : function(result) {
console.log(result);},
error : function(request, status, error) {
console.log(error)}})
AJAX 통신 시 type과 주소를 명시하고 보내는 데이터의 타입과 데이터를 명시하면 된다.
이때, get 방식으로 통신하면 상관없지만 post 방식으로 통신하는 경우 csrf에 대한 설정을 해줄 필요가 있다.
post 방식으로 통신하는 경우 csrf에 대한 설정을 해야 서버와의 통신이 가능하다.
먼저 mustache와 같은 view단에 csrf에 대한 명시를 해주고, javascript의 ajax에 token을 명시해주면 된다.
<meta name="_csrf" content="{{_csrf.token}}"/>
<meta name="_csrf_header" content="{{_csrf.headerName}}"/>
var header = $("meta[name='_csrf_header']").attr("content");
var token = $("meta[name='_csrf']").attr("content");
beforeSend : function(xhr) {xhr.setRequestHeader(header, token);}
GET 방식의 AJAX - 게시글 삭제하는 경우
$("#btn-delete").click(function() {
var writer = $("#writer").val();
var nowUser = $("#nowId").val();
var boardId = $("#boardId").val();
if (writer === nowUser) {
if (confirm("삭제하시겠습니까?")) {
$.ajax({
type : "get",
url : "/board/delete?bid=" + boardId,
contentType : "json",
success : function () {
alert("게시글이 삭제되었습니다.");
window.location.href = "/board";
}
});
}
} else {
alert("현재 접속중인 아이디가 작성자와 다릅니다.");
}
})
해당 URL에 대한 controller를 작성한다.
@ResponseBody
@GetMapping("/board/delete")
public void boardDetail(@RequestParam(value="bid") Long bid) {
boardService.boardDelete(bid);
commentService.commentDeleteByBid(bid);
}
등록된 게시글

등록된 댓글

삭제버튼을 눌렀을 때 confirm함수를 통해 재확인

삭제된 이후 VIEW

DB


POST 방식의 AJAX - 게시글을 등록하는 경우
$("#btn-save").click(function() {
var header = $("meta[name='_csrf_header']").attr("content");
var token = $("meta[name='_csrf']").attr("content");
var tempData =
{
"title" : $("#title").val(),
"content" : $("#content").val()
};
$.ajax({
type : "post",
url : "/board/write",
data : JSON.stringify(tempData),
beforeSend : function(xhr) {xhr.setRequestHeader(header, token);},
contentType : "application/json; charset=UTF-8",
success : function() {
window.location.href = "/board";
}
});
})
URL에 대한 controller를 작성한다.
@ResponseBody
@PostMapping("/board/write")
public String boardWrite(@RequestBody Map<String, Object> map) {
BoardDTO boardDTO = new BoardDTO();
boardDTO.setTitle((String) map.get("title"));
boardDTO.setWriter(SecurityContextHolder.getContext().getAuthentication().getName());
boardDTO.setContent((String) map.get("content"));
boardService.boardWrite(boardDTO);
return "redirect:/board";
}
public void boardWrite(BoardDTO boardDTO) {
BoardEntity boardEntity = new BoardEntity();
boardEntity.setTitle(boardDTO.getTitle());
boardEntity.setWriter(boardDTO.getWriter());
boardEntity.setContent(boardDTO.getContent());
boardEntity.setRegDate(String.valueOf(LocalDate.now()));
boardEntity.setModDate(String.valueOf(LocalDate.now()));
boardRepository.save(boardEntity);
}
게시글 등록 VIEW - 작성자는 현재 로그인 된 유저의 아이디로 지정해둔다.
<h1>게시글 등록</h1>
<div class="col-md-12">
<div class="col-md-4">
<form>
<div class="form-group">
<label for="title">제목</label>
<input type="text" class="form-control" id="title" placeholder="제목을 입력하세요">
</div>
<div class="form-group">
<label for="writer"> 작성자</label>
<input type="text" class="form-control" value="{{writer}}" readonly>
</div>
<div class="form-group">
<label for="content"> 내용</label>
<textarea type="text" class="form-control" id="content" placeholder="내용을 입력하세요"></textarea>
</div>
<input type="hidden" name="_csrf" value="{{_csrf.token}}">
</form>
<a href="/board" role="button" class="btn btn-secondary">취소</a>
<button type="button" class="btn btn-primary" id="btn-save">등록</button>
</div>
</div>

등록 결과
