JSP

211025_페이징

요옫 2021. 10. 25. 11:07

dao 전체출력메서드 getalldatas는 필요없음.. 전체출력은 한페이지에 모든 데이터를 다 보여줌

 

페이징: mysql에서는 파라메타값을 시작페이지와 

 

 

simpleDao.java

//페이징

//1.전체 개수 구하기

public int getTotalCount() {

Connection conn=db.getConnection();

PreparedStatement pstmt=null;

ResultSet rs=null; //몇개인지 조회하는 것이기에 필요

 

int n=0;

 

String sql="select count(*) from simpleboard";

 

try {

pstmt=conn.prepareStatement(sql);

rs=pstmt.executeQuery();

 

if(rs.next()) {

n=rs.getInt(1);

}

} catch (SQLException e) {

} finally {

db.dbClose(rs, pstmt, conn);

}

return n;

}

 

//2.페이징 처리에 필요한 리스트만 보내기

public List<SimpleDto>getList(int start,int perpage){

List<SimpleDto>list=new Vector<SimpleDto>();

 

Connection conn=db.getConnection();

PreparedStatement pstmt=null;

ResultSet rs=null;

 

String sql="select * from simpleboard order by num desc limit ?,?";

 

try {

pstmt=conn.prepareStatement(sql);

 

//?바인딩..파라메타값으로 넘긴 거

pstmt.setInt(1, start);

pstmt.setInt(2, perpage);

 

//실행

rs=pstmt.executeQuery();

 

while(rs.next()) {

SimpleDto dto=new SimpleDto();

dto.setNum(rs.getString("num"));

dto.setWriter(rs.getString("writer"));

dto.setSubject(rs.getString("subject"));

dto.setContent(rs.getString("content"));

dto.setImgname(rs.getString("imgname"));

dto.setReadcount(rs.getInt("readcount"));

dto.setWriteday(rs.getTimestamp("writeday"));

 

//리스트에 추가

list.add(dto);

}

} catch (SQLException e) {

} finally {

db.dbClose(rs, pstmt, conn);

}

return list;

}

 

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

 

boardlist.jsp

<%@page import="java.text.SimpleDateFormat"%>

<%@page import="simpleboard.model.SimpleDto"%>

<%@page import="java.util.List"%>

<%@page import="simpleboard.model.simpleDao"%>

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<link href="https://fonts.googleapis.com/css2?family=Do+Hyeon&display=swap" rel="stylesheet">

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">

<script src="https://code.jquery.com/jquery-3.5.0.js"></script>

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>

<title>Insert title here</title>

</head>

<body>

<%

//dao선언

simpleDao db=new simpleDao();

 

//list가져오기

//List<SimpleDto>list=db.getAllDatas();

 

 

/////////////////////////

//페이징

int totalCount;  //총글수

int totalPage;  //총페이지수

int startPage;  //각 블럭의 시작 페이지

int endPage;  //각 블럭의 마지막 페이지

int start;  //각 페이지의 시작 번호

int perPage=5;  //한페이지에 보여질 글의 개수

int perBlock=5;  //한 페이지에 보여지는 페이지 개수

int currentPage; //현재페이지

int no;  //넘버값

 

//총 개수

totalCount=db.getTotalCount();

 

//현재 페이지 번호 읽기..단 null일 경우(페이지 없을 경우) 1페이지 설정

if(request.getParameter("currentPage")==null){

currentPage=1;

} else{

currentPage=Integer.parseInt(request.getParameter("currentPage"));

}

 

//총페이지 개수..오라클에서는 totalBlock도 구해야 함

totalPage=totalCount/perPage+(totalCount%perPage==0?0:1);

 

//각 블럭의 시작페이지

//예: 현재 페이지:3, startpage:1, endpage:5

//예: 현재 페이지:6, startpage:6, endpage:10

startPage=(currentPage-1)/perBlock*perBlock+1;

 

endPage=startPage+perBlock-1;

 

//만약 총페이지 수가 8일 경우

//2번째 블럭은 startpage:6, endpage:10이 되야한다

//이때는 endpage를 8로 수정해 주어야 한다

if(endPage>totalPage)

endPage=totalPage;

 

//각 페이지에서 불러올 시작번호

//현재페이지가 1일경우 start는 1, 2일경우 6

start=(currentPage-1)*perPage;

 

//각페이지에서 필요한 게시글 가져오기..dao에서 만들었음

List<SimpleDto>list=db.getList(start, perPage);

 

//각 글앞에 붙일 시작번호 구하기

//총글이 20개일 경우 1페이지 20, 2페이지 15부터

//출력해서 1씩 감소하면서 출력할것

no=totalCount-(currentPage-1)*perPage;

%>

 

<!-- 출력 -->

<div class="alert alert-danger" style="width: 800px;">

  <b><%=totalCount%>개의 글이 있습니다</b>

</div>

 

<br>

 

<table class="table table-hover" style="width: 800px;">

  <caption><b>게시판형 목록보기</b>

    <button type="button" class="btn btn-info btn-sm" 

    onclick="location.href='boardform.jsp'" style="margin-left: 600px;">글작성</button>

  </caption>

  

  <tr style="background: #ffe4e1;">

    <th style="width: 70px;">번호</th>

    <th style="width: 400px;">제목</th>

    <th style="width: 100px;">작성자</th>

    <th style="width: 120px;">작성일</th>

    <th style="width: 70px;">조회수</th>

  </tr>

  

  <%

  //날짜형식

  SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

  

  for(SimpleDto dto:list)

  {

%>

<tr>

<!-- 번호 -->

  <td><%=no-- %></td>

<!-- 제목 클릭시 내용보기로 가도록 -->  

  <td>

    <a href="content.jsp?num=<%=dto.getNum()%>">  <!-- 해당num만 보게 -->

    <img alt="" src="../photo(1021)/<%=dto.getImgname() %>" style="width: 30px; height: 30px;">

    <%=dto.getSubject() %>

    </a>

  </td>

<!-- 작성자 -->    

  <td><%=dto.getWriter() %></td>

<!-- 작성일 -->  

  <td><%=sdf.format(dto.getWriteday()) %></td>

<!-- 조회수 -->

  <td align="center"><%=dto.getReadcount() %></td>

</tr>

  <%}

  %>  

</table>

<!-- 페이징 처리 -->

  <div style="width: 600px; text-align: center;" class="container">

    <ul class="pagenation">

      <%

      //이전

      if(startPage>1){%>

      <li>

        <a href="boardlist.jsp?currentPage=<%=startPage-1%>">이전</a>

      </li>

      <%}

      

      for(int p=startPage;p<=endPage;p++){

      if(p==currentPage){%>

    <li class="active">

    <a href="boardlist.jsp?currentPage=<%=p%>"><%=p %></a>

    </li>  

      <%} else{  //active 안 줄때 %>

      <a href="boardlist.jsp?currentPage=<%=p%>"><%=p %></a>

      <%}

      }

      

      //다음

      if(endPage<totalPage){%>

      <li>

        <a href="boardlist.jsp?currentPage=<%=endPage+1%>">다음</a>

      </li>

      <%}

      %>

    </ul>

  </div>

</body>

</html>