개발 여정/Error
400 에러 :: Required request parameter for method parameter type String is not present
calm-lee
2021. 9. 16. 19:37
[org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver] Resolved [org.springframework.web.bind.MissingServletRequestParameterException: Required request parameter 'content' for method parameter type String is not present]
원래 코드
let commentContent = $('input[name=commentTxt]').val();
alert(commentContent);
*문제원인
- 현재 댓글입력창은 contentList의 반복문 안에 있음
- 따라서 댓글 다는 곳의 postId를 지정해줘야 어디 위치의 input인지 인식할 수 있음
*문제해결
- input name 버리고 id로 변경
- id 이름 뒤에 postId 붙여줌 (input 위치 지정)
HTML
<input type="text" placeholder="댓글을 입력해주세요." id="commentTxt${content.post.id}"; class="form-control">
<button type="button" class="btn btn-light commentBtn" data-post-id="${content.post.id}">게시</button>
Script
//postId 저장
let postId = $(this).data('post-id');
//content 저장
let commentContent = $('#commentTxt' + postId).val();
if(commentContent == ''){
return;
}