java

JSP ๋Œ“๊ธ€๊ธฐ๋Šฅ ์ถ”๊ฐ€

dev_summer 2021. 2. 27. 13:48

 

ํ”„๋กœ์ ํŠธ ์˜ค๋Š˜๊นŒ์ง„์ค„ ์•Œ๊ณ 

์˜ฌ๋“œ๋ณด์ด์˜ ๊ฐ‡ํžŒ ์ตœ๋ฏผ์‹ ์ฒ˜๋Ÿผ ์—ฐํœด๋‚ด๋‚ด ๋ฐฅ๋จน๊ณ  ๋‚ฎ์ž ์ž๊ณ  ๊ณผ์ œ๋งŒ ํ–ˆ๋Š”๋ฐ..

๋‚ด์ผ๊นŒ์ง€๋ž€๋‹ค..

๋‚ด ์—ฐํœด ๋Œ๋ ค์ค˜..

 

๊ทธ๋Ÿฐ ๊น€์— ๋Œ“๊ธ€๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•ด๋ด์•ผ๊ฒ ๋‹น

์ฑ…์—๋„ ๋‚˜์™€์žˆ์ง€ ์•Š์€ ๋ถ€๋ถ„์ด๋ผ ์ผ๋‹จ ์ง€๊ธˆ๊นŒ์ง€ ํ•œ ์™„์„ฑ๋ณธ์„ ์ €์žฅํ•ด๋†“๊ณ  ์‹œ์ž‘

 

1. mysql comment ํ…Œ์ด๋ธ” ๋งŒ๋“ค๊ธฐ

CREATE TABLE comment(
commentno int AUTO_INCREMENT PRIMARY KEY, 
userno int(11),
noticeno int(11),
content varchar(100),
commentCreateTime timestamp,
FOREIGN KEY (userno) REFERENCES user (userno) ,
FOREIGN KEY (noticeno) REFERENCES notice (noticeno) 

) ENGINE=InnoDB DEFAULT CHARSET = utf8;

 

 

2. modelํŒจํ‚ค์ง€์— comment.java ์ถ”๊ฐ€

 

package com.seoho.tiffany.model;

import java.sql.Timestamp;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;


@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Comment {
	int commentno;
	int userno;
	int noticeno;
	String content;
	Timestamp commentCreateTime;
}

 

 

3. viewmodel ํŒจํ‚ค์ง€์˜ NoticeVM.java ์ˆ˜์ •ํ•˜๊ธฐ

package com.seoho.tiffany.viewmodel;

import java.util.List;

import com.seoho.tiffany.model.Comment;
import com.seoho.tiffany.model.Notice;
import com.seoho.tiffany.model.User;

import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@Builder

public class NoticeVM {
	private Notice notice;
	private User user;
	private List<Comment> comment;
	
	@Builder
	public NoticeVM(Notice notice, User user, List<Comment> comment) {
		super();
		this.notice = notice;
		this.user = user;
		this.comment = comment;
	}
	
	
}

 

๊ธฐ์กด์— ์‚ฌ์šฉํ•˜๋˜ noticeVM์„ ์ˆ˜์ •ํ•ด์•ผํ•˜๋Š” ๋‹จ์ ์ด ์žˆ์ง€๋งŒ 3๊ฐœ๋ฅผ ์กฐ์ธํ•˜๋Š” ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•˜์ง€ ์•Š๊ณ 

๊ธฐ์กด์— ์‚ฌ์šฉํ•˜๋˜ ์กฐ์ธ์—์„œ comment๋งŒ ์ถ”๊ฐ€ํ•˜๋ฉด ๋˜๊ธฐ๋•Œ๋ฌธ์— ๋นŒ๋”๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ํŒŒ์ผ์„ ์ˆ˜์ •ํ•ด์ค€๋‹ค.

์‚ฌ์‹ค ์ž˜ ์ดํ•ด๊ฐ€์•ˆ๋˜๊ธด ํ•œ๋ฐ ๋‚˜์ค‘์— ๋˜๋ฉด ์ดํ•ดํ•˜๊ฒŒ๋˜๊ฒŸ์ง€..?

 

 

4. detail.jsp ์ˆ˜์ •

๋‹คํ–‰ํžˆ ๊ธฐ์กด์— ๋”ฐ์™“๋˜ ๋ถ€ํŠธ์ŠคํŠธ๋žฉ์— ๋Œ“๊ธ€์„ ๋ฟŒ๋ฆฌ๋Š” ๋ฆฌ์ŠคํŠธ๊ฐ€ ์žˆ์–ด์„œ ๊ฐ–๊ณ ์™“๋‹ค.. ์ด๋ฏธ์ง€๋Š” ์„ค์ •๋ชปํ•˜๋‹ˆ ํŽญ์ˆ˜๋กœ ๊ณ ์ •

๋Œ“๊ธ€์„ ์ˆ˜์ •, ์‚ญ์ œํ•˜๋Š” ๋ฒ„ํŠผ๊ณผ ๋Œ“๊ธ€์„ ์ž‘์„ฑํ•˜๋Š” textarea์™€ ๋ฒ„ํŠผ๋งŒ ๋งŒ๋“ค์—ˆ๋‹น

 

	<div class="input-group col-sm-8 mx-auto">
			<div class="container">
				<div class="row">
					<div class="col-md-8 blog-content">
						<ul class="comment-list">

							<c:forEach var="comment" items="${NoticeListVM.comment}">
								<li class="comment">
									<div class="vcard bio">
										<img src="images/p10.jpg"
											alt="Free Website Template by Free-Template.co">
									</div>
									<div class="comment-body">
										<h3>${NoticeListVM.user.username}</h3>
										<div class="meta">${fn:substring({comment.commentCreateTime},1,17)}</div>
										<p>${comment.content}</p>



										<c:choose>
											<c:when
												test="${sessionScope.user.userid eq NoticeListVM.user.userid || sessionScope.user.admin eq 1}">

												<button id="deleteComment"
													onclick="deleteComment(${NoticeListVM.user.userno}, ${comment.commentno},${NoticeListVM.notice.noticeno})"
													type="button" class="btn btn-danger m-1">์‚ญ์ œ</button>
											</c:when>

										</c:choose>
									</div>
								</li>
							</c:forEach>
						</ul>
					</div>
				</div>
			</div>
			<form action="/notice?cmd=commentWrite" method="POST">
				<input type="hidden" name="userno"
					value="${NoticeListVM.user.userno}" /> <input type="hidden"
					name="noticeno" value="${NoticeListVM.notice.noticeno}" />
				<textarea rows="2" cols="70" class="w3-input w3-border newLogin"
					name="content" required="required"></textarea>
				<div class="form-group">
					<input type="submit" value="๋Œ“๊ธ€ ์ž‘์„ฑ"
						class="btn btn-primary btn-md text-white">
				</div>
			</form>
			<br />
		</div>

 

 

 

 

 

๋ฆฌ์ŠคํŠธ์— ๋‹ด๊ฒจ์žˆ๋Š” ๊ฒƒ์„ JSTL๋กœ ๋ฟŒ๋ฆด๋•Œ์—๋Š” ๊ผญ <c:forEach var="" items=""> ๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฟŒ๋ ค์ฃผ์–ด์•ผ ํ•œ๋‹ค.

๋˜, ๋Œ“๊ธ€์— ๋‹ฌ๋ ค์žˆ๋Š” ์‚ญ์ œ๋ฒ„ํŠผ์„ ์‚ฌ์šฉํ• ๋•Œ์—๋Š” List์•ˆ์— ์žˆ๋Š” ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ผญ forEach๋ฌธ์—์„œ ๋ณ€์ˆ˜๋ฅผ ๊ฐ–๊ณ ์™€์„œ ๋‹ค์‹œ form์•ˆ์˜ value๊ฐ’์œผ๋กœ ๋„˜๊ฒจ์ฃผ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ž‘์„ฑํ•˜์—ฌ์•ผ ํ•œ๋‹ค.

 

<form id="deleteCommentForm" action="/notice?cmd=commentdelete"
	method="POST">
	<input id="commentno" type="hidden" name="commentno" value="" /> <input
		id="userno" type="hidden" name="userno" value="" /><input
		id="noticeno" type="hidden" name="noticeno" value="" />

</form>
<script>	
function deleteComment(userno, commentno,noticeno){
		$('#deleteCommentForm #userno').attr('value',userno);
		$('#deleteCommentForm #commentno').attr('value',commentno);
		$('#deleteCommentForm #noticeno').attr('value',noticeno);
		// submit
	 $('#deleteCommentForm').submit();
	}
</script>

 

5. NoticeCommentDeleteAction.java

์‚ญ์ œ ํผ์€ ๊ฐ„๋‹จํ•˜๋‹ˆ๊นŒ ์ฝ”๋ฉ˜ํŠธ์‚ญ์ œ์•ก์…˜๋ถ€ํ„ฐ!

 

package com.seoho.tiffany.action.notice;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.seoho.tiffany.action.Action;
import com.seoho.tiffany.dao.CommentDao;
import com.seoho.tiffany.model.User;
import com.seoho.tiffany.util.Script;

public class NoticeCommentDeleteAction implements Action {

	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int noticeno = Integer.parseInt(request.getParameter("noticeno"));
		int commentno = Integer.parseInt(request.getParameter("commentno"));
		int userno = Integer.parseInt(request.getParameter("userno"));
		User user = (User) request.getSession().getAttribute("user");

		if (userno == user.getUserno() | user.getAdmin() == 1) {
			CommentDao commentDao = CommentDao.getInstance();

			int result = commentDao.delete(commentno);
			if (result == 1) {
				RequestDispatcher dis = request.getRequestDispatcher("/notice?cmd=detail&"+noticeno);
				dis.forward(request, response);
			} else {
				Script.back(response, "ํ•ด๋‹น ๊ฒŒ์‹œ๊ธ€์„ ์‚ญ์ œํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.");

			}

		} else {
			Script.back(response, "์‚ญ์ œํ•  ๊ถŒํ•œ์ด ์—†์Šต๋‹ˆ๋‹ค.");
			return;
		}

	}
}

 

 

 

6. NoticeCommentWriteAction.java

notice์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ์ป๋‹ค. comment์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ๋”ฐ๋กœ ๋งŒ๋“ค์–ด๋„๋˜์ง€๋งŒ ์–ด์ฐจํ”ผ notice์—์„œ๋งŒ ์ž‘๋™ํ• ๊บผ๊ธฐ๋•Œ๋ฌธ์— ๋…ธํ‹ฐ์Šค์˜ ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋…ธํ‹ฐ์ŠคํŒฉํ† ๋ฆฌ์—์—์„œ ๋ถ„๊ธฐํ•˜์—ฌ๋•…

package com.seoho.tiffany.action.notice;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.seoho.tiffany.action.Action;
import com.seoho.tiffany.dao.CommentDao;
import com.seoho.tiffany.util.Script;

public class NoticeCommentWriteAction implements Action {

	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if
		(
				request.getParameter("userno") == null || 
				request.getParameter("noticeno") == null ||
				request.getParameter("content") == null ||
				request.getParameter("userno").equals("") ||
				request.getParameter("noticeno").equals("") ||
				request.getParameter("content").equals("")
		) {
			Script.back(response, "์ž˜๋ชป๋œ ์ ‘๊ทผ์ž…๋‹ˆ๋‹ค.");
			return;
		}

		System.out.println(request.getParameter("userno"));
		System.out.println(request.getParameter("noticeno"));
		System.out.println(request.getParameter("content"));
		
		
		int userno = Integer.parseInt(request.getParameter("userno"));
		int noticeno = Integer.parseInt(request.getParameter("noticeno"));
		String content = request.getParameter("content");
		
//		User user = (User) request.getSession().getAttribute("user");
		
		CommentDao commentDao = CommentDao.getInstance();
		int result = commentDao.save(userno,noticeno,content);
		
		if(result == 1) {
			RequestDispatcher dis = request.getRequestDispatcher("/notice?cmd=detail&noticeno="+noticeno);
			dis.forward(request, response);
		}else {
			Script.back(response, "๊ธ€ ์ž…๋ ฅ์‹œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜์˜€์Šต๋‹ˆ๋‹ค.");
		}

	}

}

 

 

 

๋!

๋‚ด์ผ๊นŒ์ง€ ํ•œ๋‹ค๊ณ  ๊ทธ๋Ÿฌ๋ฉด ํŽ˜์ด์ง•์„ ๋งŒ๋“ค์–ด๋ณด์•„์•ผ ๊ฒ ๋‹ค๋ˆ™