--------------------------------------------/WEB-INF/CommandPro.properties 추가
/mvc2bbs/list.do=my.action.ListAction
----------------------------------------------------------------ListAction.java
package my.action;
import java.util.Collections;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import my.board.BoardDBBean;
public class ListAction implements CommandAction{
public String requestPro(HttpServletRequest request,
HttpServletResponse response) throws Throwable{
String pageNum=request.getParameter("pageNum");
if(pageNum==null){
pageNum="1";
}
int pageSize=10;
int currentPage=Integer.parseInt(pageNum);
int startRow=(currentPage-1)*pageSize+1;
int endRow=currentPage*pageSize;
int count=0;
int number=0;
List articleList=null;
BoardDBBean dbPro=BoardDBBean.getInstance();
count=dbPro.getArticleCount();
if(count>0){
articleList=dbPro.getArticles(startRow, endRow);
}
else{
articleList=Collections.EMPTY_LIST;
}
number=count-(currentPage-1)*pageSize;
//request영역에 저장
request.setAttribute("currentPage", new Integer(currentPage));
request.setAttribute("startRow", new Integer(startRow));
request.setAttribute("endRow", new Integer(endRow));
request.setAttribute("count", new Integer(count));
request.setAttribute("pageSize", new Integer(pageSize));
request.setAttribute("number", new Integer(number));
request.setAttribute("articleList", articleList);
return "list.jsp";
}
}
------------------------------------------------------------------------list.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ include file="/view/color.jspf" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>글목록</title>
</head>
<body bgcolor="${bodyback_c }">
<center><b>글목록(전체 글:${count })</b></center>
<table width="700">
<tr>
<td bgcolor="${value_c}"><a href="/mvc2bbs/writeForm.do">글쓰기</a></td>
</tr>
</table>
<c:if test="${count==0 }">
<table width="700">
<tr>
<td>게시판에 글 없음!!</td>
</tr>
</table>
</c:if>
<c:if test="${count>0 }">
<table border="1">
<tr>
<td>번호</td>
<td>제목</td>
<td>작성자</td>
<td>작성일</td>
<td>조회</td>
<td>IP</td>
</tr>
<c:forEach var="article" items="${articleList }">
<tr>
<td>
<c:out value="${number }"/>
<c:set var="number" value="${number-1 }"/>
</td>
<td>
<c:if test="${article.re_level>0 }">
<img src="images/level.gif" border="0" width="${5*article.re_level }" height="16"/>
<img src="images/re.gif">
</c:if>
<c:if test="${article.re_level==0 }">
<img src="images/level.gif" border="0" width="${5*article.re_level }" height="16"/>
</c:if>
<a href="/mvc2bbs/content.do?num=${article.num }&pageNum=${currentPage }">
${article.subject }</a>
<c:if test="${article.readcount>=20 }">
<img src="images/hot.gif" border="0" height="16"/>
</c:if>
</td>
<td><a href="mailto:${article.email}">${article.writer }</a></td>
<td>${article.reg_date }</td>
<td>${article.readcount }</td>
<td>${article.ip }</td>
</tr>
</c:forEach>
</table>
</c:if>
<c:if test="${count>0 }">
<c:set var="pageCount" value="${count/pageSize+(count%pageSize==0?0:1) }"/>
<c:set var="startPage" value="${currentPage/pageSize+1 }"/>
<c:set var="endPage" value="${startPage+10 }"/>
<c:if test="${endPage>pageCount }">
<c:set var="endPage" value="${pageCount }"/>
</c:if>
<c:if test="${startPage>10 }">
<a href="/mvc2bbs/list.do?pageNum=${startPage-10 }">[이전]</a>
</c:if>
<c:forEach var="i" begin="${startPage }" end="${endPage }">
<a href="/mvc2bbs/list.do?pageNum=${i }">[${i}]</a>
</c:forEach>
<c:if test="${endPage<pageCount }">
<a href="/mvc2bbs/list.do?pageNum=${startPage+10 }">[다음]</a>
</c:if>
</c:if>
</body>
</html>
-------------------------------------------------------------BoardDBBean.java추가
public class BoardDBBean{
//글갯수 구하기
public int getArticleCount() throws Exception{
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
int x=0;
try{
conn=getConnection();
pstmt=conn.prepareStatement("SELECT count(*) FROM board");
rs=pstmt.executeQuery();
if(rs.next()){
x=rs.getInt(1);
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(rs!=null) try { rs.close(); } catch(SQLException e){}
if(pstmt!=null) try { pstmt.close(); } catch(SQLException e){}
if(conn!=null) try { conn.close(); } catch(SQLException e){}
}
return x;
}//count
//글목록 구하기
public List getArticles(int start,int end) throws Exception{
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
List articleList=null;
String sql=" Select a.* ";
sql+=" From ( ";
sql+=" Select ROWNUM as RNUM, b.* ";
sql+=" FROM ( ";
sql+=" Select * From board Order By ref desc,re_step ASC ";
sql+=" ) b ";
sql+=" ) a ";
sql+=" Where a.RNUM >=? AND a.RNUM<=? ";
try{
conn=getConnection();
pstmt=conn.prepareStatement(sql);
pstmt.setInt(1, start);
pstmt.setInt(2, end);
rs=pstmt.executeQuery();
if(rs.next()){
articleList=new ArrayList(end);
do{
BoardDataBean article=new BoardDataBean();
article.setNum(rs.getInt("num"));
article.setWriter(rs.getString("writer"));
article.setEmail(rs.getString("email"));
article.setSubject(rs.getString("subject"));
article.setPasswd(rs.getString("passwd"));
article.setReg_date(rs.getTimestamp("reg_date"));
article.setReadcount(rs.getInt("readcount"));
article.setRef(rs.getInt("ref"));
article.setRe_step(rs.getInt("re_step"));
article.setRe_level(rs.getInt("re_level"));
article.setContent(rs.getString("content"));
article.setIp(rs.getString("ip"));
articleList.add(article);
}while(rs.next());
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(rs!=null) try { rs.close(); } catch(SQLException e){}
if(pstmt!=null) try { pstmt.close(); } catch(SQLException e){}
if(conn!=null) try { conn.close(); } catch(SQLException e){}
}
return articleList;
}//end
}
'..열심히 공부하세.. > MVC' 카테고리의 다른 글
[11] MVC2패턴 게시판 - ⑤ 수정 (0) | 2012.06.30 |
---|---|
[10] MVC2패턴 게시판 - ④ 상세보기 (0) | 2012.06.28 |
[08] 데이터베이스 커넥션 풀 - ② (0) | 2012.06.26 |
[07] MVC2패턴 게시판 - ① 쓰기 (0) | 2012.06.26 |
[06] 서블릿 요청 커맨드 패턴 (~~~.do) (0) | 2012.06.23 |