728x90
Set VO 구조
Set VO 구조는 MODEL 에서 1:N 구조를 구현할 때 사용할 수 있는 구조입니다. 주로 커뮤니티 속성을 가지는 경우에 자주 사용합니다.
Set VO 구조 적용 예시
1 | N |
게시글 | 댓글 |
댓글 | 대댓글 |
상품 | 후기 |
공지 | 답변 |
실습
BoardVO / BoardSetVO
package model;
import java.util.Date;
public class BoardVO {
private int bid;
private String mid;
private String content;
private int favCnt;
private int replyCnt;
private Date date;
public int getBid() {
return bid;
}
public void setBid(int bid) {
this.bid = bid;
}
public String getMid() {
return mid;
}
public void setMid(String mid) {
this.mid = mid;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public int getFavCnt() {
return favCnt;
}
public void setFavCnt(int favCnt) {
this.favCnt = favCnt;
}
public int getReplyCnt() {
return replyCnt;
}
public void setReplyCnt(int replyCnt) {
this.replyCnt = replyCnt;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
@Override
public String toString() {
return "BoardVO [bid=" + bid + ", mid=" + mid + ", content=" + content + ", favCnt=" + favCnt + ", replyCnt="
+ replyCnt + ", date=" + date + "]";
}
}
BoardVO.java
package model;
import java.util.ArrayList;
public class BoardSet {
private BoardVO board; // 1
private ArrayList<ReplyVO> rdatas; // N
public BoardVO getBoard() {
return board;
}
public void setBoard(BoardVO board) {
this.board = board;
}
public ArrayList<ReplyVO> getRdatas() {
return rdatas;
}
public void setRdatas(ArrayList<ReplyVO> rdatas) {
this.rdatas = rdatas;
}
@Override
public String toString() {
return "BoardSet [board=" + board + ", rdatas=" + rdatas + "]";
}
}
BoardSet.java
Main jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib tagdir="/WEB-INF/tags" prefix="jun" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>메인 페이지</title>
</head>
<body>
<script type="text/javascript">
function signup() {
// window.open("새창링크", "새창의 이름", "새창의 속성");
window.open("signup.jsp", "회원가입 창", "width = 450, height = 300");
}
function signin() {
window.open("signin.jsp", "로그인 창", "width = 450, height = 300");
}
</script>
<div id="header">
<h1>SNS 커뮤니티 실습 프로젝트</h1>
</div>
<div id="gnb">
<ul>
<li><a href="main.do">메인으로 가기</a></li>
<jun:login_logout/>
</ul>
</div>
<div id="content">
<section>
<jun:insertBoard/>
</section>
<br>
<section>
<c:if test="${ bdatas ne null }">
<c:forEach var="v" items="${ bdatas }">
<c:set var="b" value="${ v.board }"/> <!-- 새로운 변수를 생성하는 것 보단 -->
<c:set var="reply" value="${ v.rdatas }"/> <!-- 멤버변수 접근 연산자를 줄이기 위해 set 사용 -->
${ b.bid } ${ b.content } <br>
<c:forEach var="r" items="${ reply }">
${ r.rid } ${ r.rContent } <br>
</c:forEach>
<hr>
</c:forEach>
</c:if>
<!-- 페이지네이션 -->
<a href="main.do?count=${ count+2 }">[더보기]</a>
</section>
</div>
<div id="footer">
<h3>© NAVER corp. | 회사소개 | 이용약관 | 개인정보처리방침 | 고객센터</h3>
</div>
</body>
</html>
main.jsp
MainAction Controller
package controller;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.BoardDAO;
import model.BoardSet;
import model.BoardSetDAO;
import model.BoardVO;
public class MainAction implements Action {
@Override
public ActionForward execute(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ActionForward forward = new ActionForward();
forward.setPath("main.jsp");
forward.setRedirect(false);
String count = request.getParameter("count");
// TryCatch 처리하면 제일 좋음
// null값 체크할 때 null을 조건문 첫 번째에 넣음
if (count == null || count.isEmpty() || count.isBlank() || count.equals("")) {
count = "2";
}
int cnt = Integer.parseInt(count);
BoardSetDAO bsDAO = new BoardSetDAO();
ArrayList<BoardSet> bdatas = bsDAO.selectAll(null, cnt);
request.setAttribute("bdatas", bdatas);
request.setAttribute("count", cnt);
return forward;
}
}
MainAction.java
BoardSet DAO
package model;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
public class BoardSetDAO {
private Connection conn;
private PreparedStatement pstmt;
private ResultSet rs;
static final private String SQL_SELECTALL = "SELECT * FROM BOARD ORDER BY BID DESC LIMIT 0,?";
static final private String SQL_SELECTALL_REPLY = "SELECT * FROM REPLY WHERE BID = ?";
public ArrayList<BoardSet> selectAll(BoardVO bVO, int count) {
conn = JDBCUtil.connect();
ArrayList<BoardSet> datas = new ArrayList<BoardSet>();
try {
pstmt = conn.prepareStatement(SQL_SELECTALL);
pstmt.setInt(1, count);
rs = pstmt.executeQuery();
while (rs.next()) {
System.out.println("dao 보드 while");
BoardSet bs = new BoardSet();
BoardVO data = new BoardVO();
data.setBid(rs.getInt("BID"));
data.setMid(rs.getString("MID"));
data.setContent(rs.getString("CONTENT"));
data.setFavCnt(rs.getInt("FAVCNT"));
data.setReplyCnt(rs.getInt("REPLYCNT"));
data.setDate(rs.getDate("DATE"));
bs.setBoard(data);
//////
pstmt = conn.prepareStatement(SQL_SELECTALL_REPLY);
pstmt.setInt(1, data.getBid());
ResultSet rs2 = pstmt.executeQuery();
ArrayList<ReplyVO> rdatas = new ArrayList<ReplyVO>();
while (rs2.next()) {
ReplyVO rVO = new ReplyVO();
rVO.setRid(rs2.getInt("RID"));
rVO.setBid(rs2.getInt("BID"));
rVO.setMid(rs2.getString("MID"));
rVO.setDate(rs2.getDate("DATE"));
rVO.setrContent(rs2.getString("CONTENT"));
rdatas.add(rVO);
}
//////
bs.setRdatas(rdatas);
datas.add(bs);
}
} catch (SQLException e) {
e.printStackTrace();
}
JDBCUtil.disconnect(rs, pstmt, conn);
return datas;
}
}
BoardSetDAO.java
GitHub
https://github.com/Qkrwnsgus0522/JSP
728x90