ํ๋ก์ ํธ ์ค๋๊น์ง์ค ์๊ณ
์ฌ๋๋ณด์ด์ ๊ฐํ ์ต๋ฏผ์ ์ฒ๋ผ ์ฐํด๋ด๋ด ๋ฐฅ๋จน๊ณ ๋ฎ์ ์๊ณ ๊ณผ์ ๋ง ํ๋๋ฐ..
๋ด์ผ๊น์ง๋๋ค..
๋ด ์ฐํด ๋๋ ค์ค..
๊ทธ๋ฐ ๊น์ ๋๊ธ๊ธฐ๋ฅ์ ์ถ๊ฐํด๋ด์ผ๊ฒ ๋น
์ฑ ์๋ ๋์์์ง ์์ ๋ถ๋ถ์ด๋ผ ์ผ๋จ ์ง๊ธ๊น์ง ํ ์์ฑ๋ณธ์ ์ ์ฅํด๋๊ณ ์์
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¬iceno="+noticeno);
dis.forward(request, response);
}else {
Script.back(response, "๊ธ ์
๋ ฅ์ ์ค๋ฅ๊ฐ ๋ฐ์ํ์์ต๋๋ค.");
}
}
}
๋!
๋ด์ผ๊น์ง ํ๋ค๊ณ ๊ทธ๋ฌ๋ฉด ํ์ด์ง์ ๋ง๋ค์ด๋ณด์์ผ ๊ฒ ๋ค๋
'java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์คํ๋ง ์์ํ๊ธฐ -2 (0) | 2021.02.27 |
---|---|
์คํ๋ง ์์ํ๊ธฐ (0) | 2021.02.27 |
JSTL ๋ฌธ์์ด ์๋ฅด๊ธฐ (0) | 2021.02.27 |
aws mysql ์๊ฐ ์ค์ ํ๊ธฐ (0) | 2021.02.27 |
ํฐ์บฃ ์คํ ์ 404 ์ค๋ฅ (0) | 2021.02.27 |