JSP - eclipse/mysql

최성현·2023년 8월 24일
0

JSP

목록 보기
13/13

simpleboarddao

조회수 1증가 메소드

update해주는 것이라 update와 비슷

readcount에 +1씩 더해진 것을 넣어줌

String sql="update simpleboard set readcount=readcount+1 where num=?";
public void updateReadCount(String num)
	{
		Connection conn=db.getConnection();
		PreparedStatement pstmt=null;
		
		//readcount에 +1씩 더해진 것을 넣어줌
		String sql="update simpleboard set readcount=readcount+1 where num=?";
		
		try {
			pstmt=conn.prepareStatement(sql);
			pstmt.setString(1, num);
			pstmt.execute();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			db.dbClose(pstmt, conn);
		}
	}

페이징처리...1.전체갯수반환

1.전체갯수반환
1번 열만 구하면 된다...1개의 열만 구하면 행 갯수(전체갯수)가 파악이 된다

if(rs.next())
{
	total=rs.getInt(1); //1번 열만 구하면 된다...1개의 열만 구하면 행 갯수(전체갯수)가 파악이 된다
}
public int getTotalCount()
	{
		int total=0;
		
		Connection conn=db.getConnection();
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		
		String sql="select count(*) from simpleboard";
		
		try {
			pstmt=conn.prepareStatement(sql);
			rs=pstmt.executeQuery();
			
			if(rs.next())
			{
				total=rs.getInt(1); //1번 열만 구하면 된다...1개의 열만 구하면 행 갯수(전체갯수)가 파악이 된다
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return total;
	}

2.부분조회

(파라메터1,파라메터2) 파라메터1 : 시작하는 번호 / 파라메터2 : 시작번호부터 갯수

public List<SimpleBoardDto> getPagingList(int startNum,int perPage)
	{
		List<SimpleBoardDto> list=new ArrayList<SimpleBoardDto>();
		
		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, startNum);
			pstmt.setInt(2, perPage);
			rs=pstmt.executeQuery();
			
			while(rs.next()) //여러 개의 행을 구하기 위해서 while
			{
				SimpleBoardDto dto=new SimpleBoardDto();
				
				dto.setNum(rs.getString("num"));
				dto.setWriter(rs.getString("writer"));
				dto.setPass(rs.getString("pass"));
				dto.setSubject(rs.getString("subject"));
				dto.setStory(rs.getString("story"));
				dto.setReadcount(rs.getInt("readcount"));
				dto.setWriteday(rs.getTimestamp("writeday"));
				
				list.add(dto);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			db.dbClose(rs, pstmt, conn);
		}
		return list;
	}

SimpleBoardDao

package db.simpleboard;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import mysql.db.DBConnect;

public class SimpleBoardDao {
	
	DBConnect db=new DBConnect();
	
	//전체조회...paging이 없을 때 사용/파라메터로 받는 값이 따로 없음
	public List<SimpleBoardDto> getAllBoards() 
	{
		//list ... arraylist로 많이 쓰임/vector는 안되는 기능이 한가지 있음
		List<SimpleBoardDto> list=new ArrayList<SimpleBoardDto>();
		
		Connection conn=db.getConnection();
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		
		String sql="select * from simpleboard order by num desc";
		
		try {
			pstmt=conn.prepareStatement(sql);
			rs=pstmt.executeQuery();
			
			while(rs.next())
			{
				SimpleBoardDto dto=new SimpleBoardDto();
				
				dto.setNum(rs.getString("num"));
				dto.setWriter(rs.getString("writer"));
				dto.setPass(rs.getString("pass"));
				dto.setSubject(rs.getString("subject"));
				dto.setStory(rs.getString("story"));
				dto.setReadcount(rs.getInt("readcount"));
				dto.setWriteday(rs.getTimestamp("writeday"));
				
				list.add(dto);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			db.dbClose(rs, pstmt, conn);
		}
		return list;
	}
	
	//num에 해당하는 dto반환
	public SimpleBoardDto getBoard(String num)
	{
		SimpleBoardDto dto=new SimpleBoardDto();
		
		Connection conn=db.getConnection();
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		
		String sql="select * from simpleboard where num=?";
		
		try {
			pstmt=conn.prepareStatement(sql);
			pstmt.setString(1, num);
			rs=pstmt.executeQuery();
			
			if(rs.next())
			{
				dto.setNum(rs.getString("num"));
				dto.setWriter(rs.getString("writer"));
				dto.setPass(rs.getString("pass"));
				dto.setSubject(rs.getString("subject"));
				dto.setStory(rs.getString("story"));
				dto.setReadcount(rs.getInt("readcount"));
				dto.setWriteday(rs.getTimestamp("writeday"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			db.dbClose(rs, pstmt, conn);
		}
		return dto;
	}
	
	//가장 최신에 추가한 글의 num값 얻기 ... 가장 최신 글은 항상 sequence값이 제일 높기 때문
	public int getMaxnum()
	{
		int max=0;
		
		Connection conn=db.getConnection();
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		
		//alias 줘서 if문에 alias만 가져와도 됨
		String sql="select max(num) max from simpleboard";
		
		try {
			pstmt=conn.prepareStatement(sql);
			rs=pstmt.executeQuery();
			
			if(rs.next())
			{
				max=rs.getInt("max");//==rs.getInt(1);...열 번호(index)
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			db.dbClose(rs, pstmt, conn);
		}
		return max;
	}
	
	//insert
	public void insertBoard(SimpleBoardDto dto)
	{
		Connection conn=db.getConnection();
		PreparedStatement pstmt=null;
		
		//int값은 0으로 잡아줘야함
		String sql="insert into simpleboard values(null,?,?,?,?,0,now())";
		
		try {
			pstmt=conn.prepareStatement(sql);
			pstmt.setString(1, dto.getWriter());
			pstmt.setString(2, dto.getPass());
			pstmt.setString(3, dto.getSubject());
			pstmt.setString(4, dto.getStory());
			//i give 0 to readcount, so don't need to bind readcount
			pstmt.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			db.dbClose(pstmt, conn);
		}
	}
	
	//조회수 1증가 메소드...update해주는 것이라 update와 비슷
	public void updateReadCount(String num)
	{
		Connection conn=db.getConnection();
		PreparedStatement pstmt=null;
		
		//readcount에 +1씩 더해진 것을 넣어줌
		String sql="update simpleboard set readcount=readcount+1 where num=?";
		
		try {
			pstmt=conn.prepareStatement(sql);
			pstmt.setString(1, num);
			pstmt.execute();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			db.dbClose(pstmt, conn);
		}
	}
	
	//비밀번호 맞는지 체크
	public boolean passCheck(String num,String pass)
	{
		boolean flag=false;
		
		Connection conn=db.getConnection();
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		
		String sql="select count(*) from simpleboard where num=? and pass=?";
		
		try {
			pstmt=conn.prepareStatement(sql);
			pstmt.setString(1, num);
			pstmt.setString(2, pass);
			rs=pstmt.executeQuery();
			
			if(rs.next())
			{
				//초기값이 false이므로 else는 굳이 안줘도됨
				if(rs.getInt(1)==1) //비번이 틀릴경우 0...when using count() uses this if문
					flag=true;
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			db.dbClose(rs, pstmt, conn);
		}
		return flag;
	}
	
	//수정...writer.subject,story다 되게 할 것!
	public void updateBoard(SimpleBoardDto dto)
	{
		Connection conn=db.getConnection();
		PreparedStatement pstmt=null;
		
		String sql="update simpleboard set writer=?,subject=?,story=? where num=?";
		
		try {
			pstmt=conn.prepareStatement(sql);
			pstmt.setString(1, dto.getWriter());
			pstmt.setString(2, dto.getSubject());
			pstmt.setString(3, dto.getStory());
			pstmt.setString(4, dto.getNum());
			pstmt.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			db.dbClose(pstmt, conn);
		}
	}
	
	//삭제
	public void deleteBoard(String num)
	{
		Connection conn=db.getConnection();
		PreparedStatement pstmt=null;
		
		String sql="delete from simpleboard where num=?";
		
		try {
			pstmt=conn.prepareStatement(sql);
			pstmt.setString(1, num);
			pstmt.execute();
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			db.dbClose(pstmt, conn);
		}
	}
	
	//페이징처리_1.전체갯수반환
	public int getTotalCount()
	{
		int total=0;
		
		Connection conn=db.getConnection();
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		
		String sql="select count(*) from simpleboard";
		
		try {
			pstmt=conn.prepareStatement(sql);
			rs=pstmt.executeQuery();
			
			if(rs.next())
			{
				total=rs.getInt(1); //1번 열만 구하면 된다...1개의 열만 구하면 행 갯수(전체갯수)가 파악이 된다
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return total;
	}
	
	//2.부분조회 (파라메터1,파라메터2) 파라메터1 : 시작하는 번호 / 파라메터2 : 시작번호부터 갯수
	public List<SimpleBoardDto> getPagingList(int startNum,int perPage)
	{
		List<SimpleBoardDto> list=new ArrayList<SimpleBoardDto>();
		
		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, startNum);
			pstmt.setInt(2, perPage);
			rs=pstmt.executeQuery();
			
			while(rs.next()) //여러 개의 행을 구하기 위해서 while
			{
				SimpleBoardDto dto=new SimpleBoardDto();
				
				dto.setNum(rs.getString("num"));
				dto.setWriter(rs.getString("writer"));
				dto.setPass(rs.getString("pass"));
				dto.setSubject(rs.getString("subject"));
				dto.setStory(rs.getString("story"));
				dto.setReadcount(rs.getInt("readcount"));
				dto.setWriteday(rs.getTimestamp("writeday"));
				
				list.add(dto);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			db.dbClose(rs, pstmt, conn);
		}
		return list;
	}
}

addform

story(textarea)

story 에는 th 태그 필요 없음(글만 쓰면 되기 때문)

<tr>
	<%-- story don't have to use th tag --%>
	<td colspan="2">
		<textarea style="width: 390px; height: 150px;"
		class="form-control;" required="required" name="story"></textarea>
	</td>
</tr>

image button

type이 submit과 image일 경우 기본 타입이 submit

<tr>
	<td colspan="2" align="center">
		<%-- when submit and image basic type is submit --%>
		<%-- type이 image이거나 submit이면 기본이 submit --%>
		<input type="image" src="../image/submit button.jpeg" style="width: 70px;">
		<input type="image" src="../image/list image2.jpeg" style="width: 70px;"
		<%-- return false을 줌으로써 onclick 우선 적용해주는 역할 --%>
		onclick="location.href='boardlist.jsp'; return false;">
	</td>
</tr>

onclick return false

return false을 줌으로써 onclick 우선 적용해주는 역할

<input type="image" src="../image/list image2.jpeg" style="width: 70px;"
<%-- return false을 줌으로써 onclick 우선 적용해주는 역할 --%>
onclick="location.href='boardlist.jsp'; return false;">

addform.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Cute+Font&family=Diphylleia&family=Dokdo&family=Nanum+Brush+Script&family=Nanum+Gothic+Coding&family=Noto+Sans+KR&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css">
<script src="https://code.jquery.com/jquery-3.7.0.js"></script>
<title>Insert title here</title>
</head>
<body>
	<div style="margin: 30px 30px; width: 400px;">
		<form action="addaction.jsp" method="post">
			<table class="table table-bordered">
				<caption align="top"><b><i class="bi bi-pencil-fill"></i>Writing</b></caption>
				<tr>
					<th style="background-color: #40e0d0">Writer</th>
					<td>
						<input type="text" name="writer" class="form-control"
						required="required" autofocus="autofocus" style="width: 150px;">
					</td>
				</tr>
				
				<tr>
					<th style="background-color: #40e0d0">Password</th>
					<td>
						<input type="password" name="pass" class="form-control"
						required="required" style="width: 150px;">
					</td>
				</tr>
				
				<tr>
					<th style="background-color: #40e0d0">Title</th>
					<td>
						<input type="text" name="subject" class="form-control"
						required="required" style="width: 270px;">
					</td>
				</tr>
				
				<tr>
					<%-- story don't have to use th tag --%>
					<td colspan="2">
						<textarea style="width: 390px; height: 150px;"
						class="form-control;" required="required" name="story"></textarea>
					</td>
				</tr>
				
				<tr>
					<td colspan="2" align="center">
						<%-- when submit and image basic type is submit --%>
						<%-- type이 image이거나 submit이면 기본이 submit --%>
						<input type="image" src="../image/submit button.jpeg" style="width: 70px;">
						<input type="image" src="../image/list image2.jpeg" style="width: 70px;"
						<%-- return false을 줌으로써 onclick 우선 적용해주는 역할 --%>
						onclick="location.href='boardlist.jsp'; return false;">
					</td>
				</tr>
			</table>
		</form>
	</div>
</body>
</html>

addaction

useBean은 new로 객체생성하는 것과 같아 id는 변수명이 된다

<jsp:useBean id="dao" class="db.simpleboard.SimpleBoardDao"/>
<jsp:useBean id="dto" class="db.simpleboard.SimpleBoardDto"/>

dto의 멤버랑 같은이름의 폼태그는 자동으로 읽어서 dto에 넣어준다

<jsp:setProperty property="*" name="dto"/>

내용보기로 이동하려면?
max num필요(최신 글은 맨 마지막에 입력한 값이기 때문) -> detail.jsp로 보내줘야함

int num=dao.getMaxnum(); //방금 insert된 num값을 알아야 하기 때문에
response.sendRedirect("detailview.jsp?num="+num);

addaction.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Cute+Font&family=Diphylleia&family=Dokdo&family=Nanum+Brush+Script&family=Nanum+Gothic+Coding&family=Noto+Sans+KR&display=swap" rel="stylesheet">
<script src="https://code.jquery.com/jquery-3.7.0.js"></script>
<title>Insert title here</title>
</head>
<body>
<%
	request.setCharacterEncoding("utf-8");
%>

	<%-- useBean은 new로 객체생성하는 것과 같아 id는 변수명이 된다 --%>
	<jsp:useBean id="dao" class="db.simpleboard.SimpleBoardDao"/>
	<jsp:useBean id="dto" class="db.simpleboard.SimpleBoardDto"/>
	
	<%-- dto의 멤버랑 같은이름의 폼태그는 자동으로 읽어서 dto에 넣어준다 --%>
	<jsp:setProperty property="*" name="dto"/>
<%
	//db에 insert
	dao.insertBoard(dto);
	
	//목록으로 이동
	//response.sendRedirect("boardlist.jsp");
	
	//내용보기로 이동하려면?
	//need a max num...and send it to detailview.jsp
	int num=dao.getMaxnum(); //방금 insert된 num값을 알아야 하기 때문에
	response.sendRedirect("detailview.jsp?num="+num);
%>
</body>
</html>

페이징 처리

자바 구문

int totalCount=dao.getTotalCount(); // 전체개수
int totalPage; // 총 페이지 수
int startPage; // 각 블럭에서 보여질 시작페이지
int endPage; // 각 블럭에서 보여질 끝페이지
int startNum; // db에서 가져올 글의 시작번호(mysql은 첫 글 번호가 0, oracle은 1)
int perPage=3; // 1페이지당 보여질 글의 갯수
int perBlock=5; // 1블럭당 보여질 페이지 갯수
int currentPage; // 현재페이지
int no; //각 페이지당 출력할 시작번호...해도안해도 상관없지만 미리 처리해줌

//현재페이지 읽기(단 null일 경우는 1페이지로 준다)		계산을 해야하므로 int로 형변환해줌
if(request.getParameter("currentPage")==null) //현재 페이지가 없다
    currentPage=1; //현재 페이지를 1로 하겠다
else //현재 페이지가 있다
    currentPage=Integer.parseInt(request.getParameter("currentPage")); //받은 페이지로 현재 페이지를 하겠다
    
//총 페이지 수 구하기
//총 글의 개수/한 페이지당 보여질 개수로 나눔(7/5=1)
//나머지가 1이라도 있으면 무조건 1페이지 추가한다는 의미(1+1=2 페이지가 필요)
totalPage=totalCount/perPage+(totalCount%perPage==0?0:1);
	
//각 블럭당 보여야할 시작페이지
//perBlock=5일 경우는 현재페이지 1~5 시작:1 끝:5
//현재페이지 13일 경우 시작:11 끝:15
startPage=(currentPage-1)/perBlock*perBlock+1;
     
endPage=startPage+perBlock-1;
    
//perBlock=5일 경우 맨 마지막 페이지가 23이라면 마지막 블록은 25가 아니라 23이 마지막 페이지이기 때문에 예외처리
//총 페이지가 23일 경우 마지막 블럭은 25가 아니라 23이다
if(endPage>totalPage)
    endPage=totalPage;

//각 페이지에서 보여질 시작번호
//1페이지:0, 2페이지:5, 3페이지:10.....
//=(현재페이지-1)*각 페이지마다
startNum=(currentPage-1)*perPage;
  	
  	
//각 페이지당 출력할 시작번호 구하기...no(넘버링)
//총 글 개수가 23이면 1페이지 시작번호:23, 2페이지는 18, 3페이지는 13......
no=totalCount-(currentPage-1)*perPage;
  	
  	
  	
//페이지에서 보여질 글만 가져오기
List<SimpleBoardDto> list=dao.getPagingList(startNum, perPage);

전체개수

int totalCount=dao.getTotalCount();

총 페이지 수

int totalPage;

각 블럭에서 보여질 시작페이지

int startPage;

각 블럭에서 보여질 끝페이지

int endPage;

db에서 가져올 글의 시작번호(mysql은 첫 글 번호가 0, oracle은 1)

int startNum;

1페이지당 보여질 글의 갯수

int perPage=3;

1블럭당 보여질 페이지 갯수

boardlist.jsp

<%@page import="java.text.SimpleDateFormat"%>
<%@page import="db.simpleboard.SimpleBoardDto"%>
<%@page import="java.util.List"%>
<%@page import="db.simpleboard.SimpleBoardDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Cute+Font&family=Diphylleia&family=Dokdo&family=Nanum+Brush+Script&family=Nanum+Gothic+Coding&family=Noto+Sans+KR&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css">
<script src="https://code.jquery.com/jquery-3.7.0.js"></script>
<title>Insert title here</title>
<style type="text/css">
	body *{
		font-family: 'Cute Font';
		font-size: 20px;
	}
	
	/* when a tag links and visits */
	a:link,a:visited{
		text-decoration: none;
		color: black;
	}
	
	a:hover {
		text-decoration: underline;
		color: gray;
	}
</style>
</head>
<%-- list 시작 --%>
<%
	SimpleBoardDao dao=new SimpleBoardDao();
	//List<SimpleBoardDto> list=dao.getAllBoards();
	
	
	int totalCount=dao.getTotalCount(); // 전체개수
	int totalPage; // 총 페이지 수
	int startPage; // 각 블럭에서 보여질 시작페이지
	int endPage; // 각 블럭에서 보여질 끝페이지
	int startNum; // db에서 가져올 글의 시작번호(mysql은 첫 글 번호가 0, oracle은 1)
	int perPage=3; // 1페이지당 보여질 글의 갯수
	int perBlock=5; // 1블럭당 보여질 페이지 갯수
	int currentPage; // 현재페이지
	int no; //각 페이지당 출력할 시작번호...해도안해도 상관없지만 미리 처리해줌

	//현재페이지 읽기(단 null일 경우는 1페이지로 준다)		계산을 해야하므로 int로 형변환해줌
	if(request.getParameter("currentPage")==null) //현재 페이지가 없다
    	currentPage=1; //현재 페이지를 1로 하겠다
 	else //현재 페이지가 있다
    	currentPage=Integer.parseInt(request.getParameter("currentPage")); //받은 페이지로 현재 페이지를 하겠다
    
    //총 페이지 수 구하기
    //총 글의 개수/한 페이지당 보여질 개수로 나눔(7/5=1)
    //나머지가 1이라도 있으면 무조건 1페이지 추가한다는 의미(1+1=2 페이지가 필요)
  	totalPage=totalCount/perPage+(totalCount%perPage==0?0:1);
	
    //각 블럭당 보여야할 시작페이지
    //perBlock=5일 경우는 현재페이지 1~5 시작:1 끝:5
    //현재페이지 13일 경우 시작:11 끝:15
  	startPage=(currentPage-1)/perBlock*perBlock+1;
     
  	endPage=startPage+perBlock-1;
    
  	//perBlock=5일 경우 맨 마지막 페이지가 23이라면 마지막 블록은 25가 아니라 23이 마지막 페이지이기 때문에 예외처리
  	//총 페이지가 23일 경우 마지막 블럭은 25가 아니라 23이다
	if(endPage>totalPage)
    	endPage=totalPage;

  	//각 페이지에서 보여질 시작번호
  	//1페이지:0, 2페이지:5, 3페이지:10.....
  	//=(현재페이지-1)*각 페이지마다
  	startNum=(currentPage-1)*perPage;
  	
  	
  	//각 페이지당 출력할 시작번호 구하기...no(넘버링)
  	//총 글 개수가 23이면 1페이지 시작번호:23, 2페이지는 18, 3페이지는 13......
  	no=totalCount-(currentPage-1)*perPage;
  	
  	
  	
  	//페이지에서 보여질 글만 가져오기
  	List<SimpleBoardDto> list=dao.getPagingList(startNum, perPage);
	
%>
<body>
	<div style="margin: 30px; 30px; width: 800px;">
		<button type="button" class="btn btn-outline-info"
											<%-- bootstrap 5 아이콘 적용 방법 --%>
		onclick="location.href='addform.jsp'"><i class="bi bi-vector-pen"></i>글쓰기</button>
		
		<br><br>
		<h6><b>총 <%=totalCount %>개의 게시글이 있습니다</b></h6>
		<table class="table table-bordered">
			<caption align="top"><b>간단 게시판 목록</b></caption>
			<tr align="center">
				<th width="60">Number</th>
				<th width="360">Title</th>
				<th width="100">Writer</th>
				<th width="120">Day</th>
				<th width="60">Views</th>
			</tr>
			
			<%
				if(list.size()==0)
				{%>
					<tr>
						<td colspan="5" align="center">
							<h6><b>NO STORY EXIST</b></h6>
						</td>
					</tr>
				<%}
				else{
				
					SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
					
					for(int i=0;i<list.size();i++)
					{
						SimpleBoardDto dto=list.get(i);%>
						
						<tr>
							<td align="center"><%=no-- %></td>
							<%-- a Tag loads to content page...need a num(sequence) --%>
							<td align="center"><a href="detailview.jsp?num=<%=dto.getNum()%>"><%=dto.getSubject() %></a></td>
							<td align="center"><%=dto.getWriter() %></td>
							<td align="center"><%=sdf.format(dto.getWriteday()) %></td>
							<td align="center"><%=dto.getReadcount() %></td>
						</tr>
						
					<%}
				}
			%>
		</table>
		
		<%-- 페이지번호 출력 --%>
		<div>
			<ul class="pagination justify-content-center">
				<%
					//이전버튼
					if(startPage>1)
					{%>
						<li class="page-item">
      						<a class="page-link" href="boardlist.jsp?currentPage=<%=startPage-1 %>" tabindex="-1" aria-disabled="true">Previous</a>
   						</li>
					<%}
				
					for(int pp=startPage;pp<=endPage;pp++)
					{
						//현재페이지에 css추가를 위한 조건
						if(pp==currentPage)
						{%>
							<li class="page-item active">
								<a class="page-link" href="boardlist.jsp?currentPage=<%=pp%>"><%=pp %></a>
							</li>
						<%}
						else //선택 안한 페이지는 색이 다른 색이기 때문에 똑같이 적어도 괜찮다
						{%>
							<li class="page-item">
								<a class="page-link" href="boardlist.jsp?currentPage=<%=pp%>"><%=pp %></a>
							</li>
						<%}
					}
					
					//다음버튼
					if(endPage<totalPage)
					{%>
						<li class="page-item">
     						<a class="page-link" href="boardlist.jsp?currentPage=<%=endPage+1%>">Next</a>
    					</li>
					<%}
				%>
			</ul>
		</div>
	</div>
</body>
</html>
profile
백엔드 개발자로서 성장해 나가는 성현이의 블로그~

0개의 댓글