귀여운사기리
115
2021-04-18 15:44:28
1
118

간단한 ajax post중 무한루프 문제가 발생했습니다. 도와주시면 감사하겠습니다.


Controller

    @PostMapping("/comments/{seq}")
    public JsonObject insertComment(@PathVariable Long seq,@RequestBody final CommentDTO params){
        JsonObject jsonObj = new JsonObject();

        try {
            System.out.println(params.toString());
            insertComment(seq,params);

            jsonObj.addProperty("result",true);

        } catch (DataAccessException e) {
            jsonObj.addProperty("message", "데이터베이스 처리 과정에 문제가 발생하였습니다.");

        } catch (Exception e) {
            jsonObj.addProperty("message", "시스템에 문제가 발생하였습니다.");
        }

        return jsonObj;
    }

DTO

package com.sample.cos.board.domain;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.util.Date;

@Getter
@Setter
@ToString

public class CommentDTO {

    private Long seq;

    private String content;

    private String writer;

    private boolean deleteYn;

    private Date createDate;
}

View

<html xmlns:th="http://www.thymeleaf.org"xmlns:sec="http://thymeleaf.org/thymeleaf-extras-springsecurity5" xmlns="http://www.w3.org/1999/html">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>게시글 상세</title>
</head>
<body th:align="center">
<h1>게시글</h1>
<form th:action="@{/board/updateBoard}" method="post">
<table th:align="center" border="1" th:cellpadding="0" th:cellspacing="0">

    <tr>
        <td bgcolor="orange" th:text="제목" width="80"></td>
        <td><input name="seq" type="hidden" th:value="${board.seq}">
            <input name="title" type="text" th:value="${board.title}"></td>
    </tr>
    <tr>
        <td bgcolor="orange" th:text="작성자"></td>
        <td th:text="${board.member.name}"></td>
    </tr>
    <tr>
        <td bgcolor="orange" th:text="내용">
        <td><textarea name="content" th:text="${board.content}" cols="40" rows="10"></textarea></td>
    </tr>
    <tr>
        <td bgcolor="orange" th:text="등록일"></td>
        <td th:text="${#dates.format(board.createDate, 'yyyy-MM-dd')}">
    </tr>
    <tr>
        <td bgcolor="orange" th:text="조회수"></td>
        <td th:text="${board.cnt}"></td>
    <tr>
        <td colspan="2" align="center">
            <input type="submit" value="게시글 수정">
        </td>
    </tr>

</table>
</form>
<div th:object="${params}">

<a th:href="@{/board/insertBoard}">글등록</a>&nbsp;&nbsp;&nbsp;
<a sec:authorize="hasRole('ROLE_ADMIN')"   th:href="@{/board/deleteBoard(seq=${board.seq})}">글삭제</a>&nbsp;&nbsp;&nbsp;
<a th:href="|@{/board/getBoardList}*{makeQueryString(currentPageNo)}|">글목록</a>
</div>
<br>
<th:block >
    <div class="box-content">
        <div class="card-content">
            <div class="clearfix">
                <h4 class="box-title pull-left">Comment</h4>
                <!-- /.box-title -->
            </div>

            <form class="form-horizontal form-view">
                <div class="input-group margin-bottom-20">
                    <input type="text" id="content" class="form-control" />
                    <div class="input-group-btn">
                        <button type="button" class="btn waves-effect waves-light" th:onclick="insertComment()">
                            등록
                        </button>
                    </div>
                </div>
                <ul class="notice-list"></ul>
            </form>
        </div>
        <!-- /.card-content -->
    </div>
    <!-- /.box-content -->
</th:block>






<script th:src="@{/scripts/jquery.min.js}"></script>
<script th:src="@{/plugin/bootstrap/js/bootstrap.min.js}"></script>
<script th:src="@{/plugin/mCustomScrollbar/jquery.mCustomScrollbar.concat.min.js}"></script>
<script th:src="@{/plugin/common/moment.js}"></script>
<script th:src="@{/scripts/main.js}"></script>
<script th:src="@{/scripts/common.js}"></script>

<script th:inline="javascript">
			/*<![CDATA[*/
function insertComment() {

	var content = document.getElementById("content");
	if (isEmpty(content.value) == true) {
		content.setAttribute("placeholder", "댓글을 입력해 주세요.");
		content.focus();
		return false;
	}

	var uri = /*[[ @{/comments/} + ${board.seq} ]]*/;
	var headers = {"Content-Type": "application/json", "X-HTTP-Method-Override": "POST"};
	var params = {"content": content.value, "writer": "관리자"};

	$.ajax({
		url: uri,
		type: "POST",
		headers: headers,
		dataType: "json",
		data: JSON.stringify(params),
		success: function(response) {
			if (response.result == false) {
				alert("댓글 등록에 실패하였습니다.");
				return false;
			}

			printCommentList();
			content.value = "";
		},
		error: function(xhr, status, error) {
			alert("에러가 발생하였습니다.");
			return false;
		}
	});
}

			$(function() {
				printCommentList();
			});



			function printCommentList() {

				var uri = /*[[ @{/comments/} + ${board.seq}]]*/;

				$.get(uri, function(response) {
					if (isEmpty(response) == false) {
						var commentsHtml = "";

						$(response.commentList).each(function(seq, comment) {
							commentsHtml += `
								<li>
									<span class="name">${comment.writer}</span>
									<span class="desc">${comment.content}</span>
<span class="time">${moment(comment.insertTime).format('YYYY-MM-DD HH:mm:ss')}</span>
									<button type="button" class="btn btn-xs btn-circle">수정</button>
								</li>
							`;
						});

						$(".notice-list").html(commentsHtml);
					}
				}, "json");
			}
			/*[- end of function -]*/


/*]]>*/
</script>
</body>
</html>

Console

CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)
CommentDTO(seq=null, content=멈춰!, writer=관리자, deleteYn=false, createDate=null)

View 부분의 insertComment 함수는 분명 한 번 실행되었는데, 어째서 View에는 같은 값이 Json으로 여러번 전달되는건지 전혀 모르겠습니다. 도와주신다면 감사하겠습니다.



0
  • 답변 1

  • yykk123
    206
    2021-04-18 17:24:34

    재귀함수이신거같습니다.

    수정하셔서

    comentService.insert(param) 이런식으로 처리하시는게 정상적으로보입니다

  • 로그인을 하시면 답변을 등록할 수 있습니다.