수업내용/Spring
[2022.12.27.화] model2로 댓글(comment) 실습
주니어주니
2022. 12. 29. 23:01
3. 댓글 등록, 삭제
- 게시글 번호, 로그인한 사용자 정보 등이 필요함
* comments.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="comments">
<select id="getCommentsByPostNo" parameterClass="int" resultClass="com.sample.app.dto.CommentListDto">
select
C.comment_no as no,
C.comment_user_id as userId,
U.user_name as userName,
C.comment_content as content,
C.comment_created_date as createdDate,
C.comment_post_no as postNo
from
web_comments C, web_users U
where
C.comment_post_no = #value#
and C.comment_user_id = U.user_id
order by
C.comment_no asc
</select>
<select id="getCommentByNo" parameterClass="int" resultClass="com.sample.app.vo.Comment">
select
comment_no as no,
comment_user_id as userId,
comment_content as content,
comment_created_date as createdDate,
comment_updated_date as updatedDate,
comment_post_no as postNo
from
web_comments
where
comment_no = #value#
</select>
<insert id="insertComment" parameterClass="com.sample.app.vo.Comment">
insert into web_comments
(comment_no, comment_user_id, comment_content, comment_post_no)
values
(WEB_COMMENTS_SEQ.nextval, #userId#, #content#, #postNo#)
</insert>
<delete id="deleteComment" parameterClass="int">
delete from
web_comments
where
comment_no = #value#
</delete>
</sqlMap>
* 댓글쓰기 InsertCommentController , detail.jsp의 댓글등록폼
package com.sample.app.controller.post;
import com.sample.app.dao.CommentDao;
import com.sample.app.dao.PostDao;
import com.sample.app.vo.Comment;
import com.sample.app.vo.Post;
import com.sample.model2.Controller;
import com.sample.util.StringUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
/*
* 요청 URI
* /post/insertComment.hta
* 요청 파라미터
* postNo
* content
* 반환값
* redirect:/app/user/loginform.hta?error=deny
* redirect:detail.hta?postNo=100
* 요청 처리 내용
* 세션에서 로그인된 사용자 정보를 조회한다.
* 사용자정보가 존재하지 않으면 로그인폼을 요청하는 재요청 URL("redirect:/app/user/loginform.hta?error=deny")을 반환한다.
* 요청파라미터 값(게시글번호, 댓글내용)을 조회한다.
* Comment 객체를 생성해서 작성자 아이디, 내용, 게시글 번호를 저장한다.
* CommentDao 객체의 insertComment(Comment comment)를 호출해서 댓글정보를 저장한다.
* 게시글 번호로 PostDao의 getPostByNo(int postNo)를 실행해서 게시글 정보를 조회한다.
* 게시글 정보의 댓글 개수를 1 증가시킨다.
* PostDao의 updatePost(Post post)를 실행시켜서 변경된 정보를 반영시킨다.
*
* 게시글 상세정보를 요청하는 재요청 URL을 응답으로 보낸다.
*/
public class InsertCommentController implements Controller {
@Override
public String execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
// 세션에서 로그인된 사용자 정보를 조회한다.
HttpSession session = request.getSession();
String loginUserId = (String) session.getAttribute("loginUserId");
if(loginUserId == null) {
return "redirect:/app/user/loginform.hta?error=deny";
}
// 요청 파라미터 값을 조회한다.
int postNo = StringUtils.stringToInt(request.getParameter("postNo"));
String content = request.getParameter("content");
// Comment 객체를 생성해서 작성자아이디, 내용, 게시글 번호를 저장한다.
Comment comment = new Comment();
comment.setUserId(loginUserId);
comment.setContent(content);
comment.setPostNo(postNo);
CommentDao commentDao = CommentDao.getInstance();
PostDao postDao = PostDao.getInstance();
// CommentDao의 insertComnent(Comment comment)를 실행해서 댓글 정보를 테이블에 저장한다.
commentDao.insertComment(comment);
// 게시글 번호로 게시글 정보를 조회한다.
Post post = postDao.getPostByNo(postNo);
// 게시글 정보의 댓글 수를 증가시킨다.
post.setCommentCount(post.getCommentCount() + 1);
// PostDao 객체의 updatePost(Post post)를 실행해서 변경된 게시글 정보로 갱신시킨다.
postDao.updatePost(post);
return "redirect:detail.hta?postNo=" + postNo;
}
}
* 댓글 삭제 DeleteCommentController , detail.jsp의 댓글부분
package com.sample.app.controller.post;
import com.sample.app.dao.CommentDao;
import com.sample.app.dao.PostDao;
import com.sample.app.vo.Comment;
import com.sample.app.vo.Post;
import com.sample.model2.Controller;
import com.sample.util.StringUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
/*
* 요청 URI
* /post/deleteCommen.hta
* 요청 파라미터
* postNo
* commentNo
* 반환값
* redirect:/app/user/loginform.hta?error=deny : 로그인상태가 아닐 때
* redirect:detail.hta?postNo=100&error=comment : 댓글작성자와 로그인한 사용자가 다를 때
* redirect:detail.hta?postNo=100 : 댓글삭제가 완료되었을 때
* 요청 처리 내용
* 사용자 정보가 존재하지 않으면 로그인폼을 요청하는 재요청 URL("redirect:/app/user/loginform.hta?error=deny")을 반환한다.
* 요청파라미터값(게시글번호, 댓글번호)을 조회한다.
* CommentDao객체의 getCommentByNo(int commentNo)을 실앻애서 댓글정보 저장
* 댓글 작성자 아이디와 로그인한 사용자의 아이디가 일치하지 않으면 재요청 URL("redirect:detail.hta?postNo=100&error=comment")
* CommentDao객체의 deleteComment(int commentNo)을 실행해서 댓글 삭제한다.
* PostDao객체의 getPostByNo(int post.no)를 실행해서 게시글 정보를 조회한다.
* 게시글 정보의 댓글 개수를 1 감소
* PostDao 객체의 updatePoser(Post post)를 실행해서 변경된 게시글 정보를 테이블에 반영한다.
*
* 게시글 상세화면을 요청하는 재요청 URL를 반환한다.
*/
public class DeleteCommentController implements Controller {
@Override
public String execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
// 세션에서 로그인 정보를 조회한다.
HttpSession session = request.getSession();
String loginUserId = (String) session.getAttribute("loginUserId");
if(loginUserId == null) {
return "redirect:/app/user/loginform.hta?error=deny";
}
// 요청 파라미터 값(게시글 번호, 댓글 번호)을 조회한다.
int postNo = StringUtils.stringToInt(request.getParameter("postNo"));
int commentNo = StringUtils.stringToInt(request.getParameter("commentNo"));
CommentDao commentDao = CommentDao.getInstance();
PostDao postDao = PostDao.getInstance();
// 댓글번호로 댓글정보를 조회한다.
Comment comment = commentDao.getCommentByNo(commentNo);
// 댓글정보의 댓글 작성자 아이디와 로그인한 사용자 아이디가 일치하는지 확인한다.
if(!comment.getUserId().equals(loginUserId)) {
return "redirect:detail.hta?postNo=" + postNo + "&error=comment";
}
// 댓글정보 삭제하기
commentDao.deleteComment(commentNo);
// 게시글 번호에 해당하는 게시글 정보 조회하기
Post post = postDao.getPostByNo(postNo);
// 댓글 개수 1감소
post.setCommentCount(post.getCommentCount() - 1);
// 변경된 게시글 정보를 테이블에 반영시키기
postDao.updatePost(post);
return "redirect:detail.hta?postNo=" + postNo;
}
}
4. 게시글 조회수 증감
* ReadController 생성
* list.jsp
* FrontController
*** 댓글 작성/삭제, 게시글 수정/삭제 할 때는 조회수가 안올라가고, 목록에서 들어갈 때만 조회수 올라가게 하기
- list에서 들어갈 때는 "read.hta"의 경로로 들어감 -> ReadController 작동 -> 조회수 증가 기능 구현
- 댓글 작성/삭제, 게시글 수정/삭제 할 때는 "detail.hta"의 경로로 재요청 -> 각각의 Controller 작동 -> 조회수 영향 X