bbslist.jsp
<%@page import="dto.BbsDto"%>
<%@page import="java.util.List"%>
<%@page import="dao.BbsDao"%>
<%@page import="dto.MemberDto"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%!
//댓글의 depth와 image를 추가하는 함수 생성
public String arrow(int depth){//depth=1 ->' '(한칸), depth=2 -> ' '(두칸), 0이면 기본글로 추가아무것도안함
String rs = "<img src='./image/arrow.png' width='20px' height='20px' />";
String nbsp = " ";
String ts = "";
for(int i = 0; i < depth; i++){
ts += nbsp;
}
return depth==0?"":ts+rs;
}
%>
<%
Object ologin = session.getAttribute("login");
MemberDto mem = null;
if(ologin == null){//session이 날라갔을 때(시간경과 등)
%>
<script type="text/javascript">
alert("로그인 해 주십시오");
location.href = "login.jsp";
</script>
<%
}
mem = (MemberDto)ologin;
%>
<%
//검색
String searchWord = request.getParameter("searchWord");
String choice = request.getParameter("choice");
if(choice == null || choice.equals("")){
choice = "sel";
}
//검색어를 지정하지 않고 choice가 넘어왔을 때
if(choice.equals("sel")){
searchWord = "";//null->""로 변경해줌(=new String())
}
if(searchWord == null){
searchWord = "";
choice = "sel";
}
%>
<%
BbsDao dao = BbsDao.getInstance();
//페이지 생성
String spageNumber = request.getParameter("pageNumber");
int pageNumber = 0;//현재페이지
if(spageNumber != null && !spageNumber.equals("")){//하라미터가 넘어왔을때
pageNumber = Integer.parseInt(spageNumber);
}
System.out.println(pageNumber);
//List<BbsDto> list = dao.getBbsList();
//List<BbsDto> list = dao.getBbsList(choice, searchWord);
List<BbsDto> list = dao.getBbsPagingList(choice, searchWord, pageNumber);
int len = dao.getAllBbs( );
System.out.println(len);
//10개씩 넘김
int bbsPage = len/10;
if(len % 10 > 0){ //글의 수가 10개를 넘어갈경우 page한장을 넘겨줌
bbsPage = bbsPage + 1;// -> 2
}
//현재가 몇번째 페이지인지 초기화
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<style type="text/css">
table{
border-collapse: collapse;
/* max-width: 100%; */
}
.span{
font-size: 15pt;
color: #0000ff;
font-weight: bold;
text-decoration: none;
text-align: center;
}
.aa{
font-size: 15pt;
color: #000;
font-weight: bold;
text-decoration: none;
text-align: center;
}
.container {
margin-left: auto;
margin-right: auto;
padding-left: 10px;
padding-right: 10px;
}
.table-header {
background-color: #95A5A6;
font-size: 14px;
text-transform: uppercase;
letter-spacing: 0.03em;
}
.table-row {
background-color: #ffffff;
box-shadow: 0px 0px 9px 0px rgba(0,0,0,0.3);
}
.h4{
background-color: #f0f0f0;
text-align: right;
}
.wri{
text-align: right;
}
.btn {
font-size: 16px;
font-weight: bold;
background: gray;
border: none;
outline: none !important;
color: white;
border-radius: 2px;
border-radius: 100px;
}
.div{
text-align: center;
}
</style>
</head>
<body>
<h4 class="h4" >
환영합니다 <%=mem.getId() %>님<a href="login.jsp"> 로그아웃</a>
</h4>
<h1 align="center">게시판</h1>
<!-- <a href="bbswrite.jsp" id="btn" class="wri">글쓰기</a> -->
<a href="bbswrite.jsp">글쓰기</a>
<div align="center" class="container">
<table border="1">
<col width="70"><col width="600"><col width="60"><col width="100"><col width="100">
<tr class="table-header">
<th>번호</th><th>제목</th><th>조회수</th><th>작성자</th><th>작성일</th>
</tr>
<%
if(list == null || list.size()==0){//list가 없거나 글이 없을 때
%>
<tr>
<td colspan="5" style="text-align: center">작성된 글이 없습니다</td>
</tr>
<%
}else{
for(int i = 0; i < list.size(); i++){
BbsDto bbs = list.get(i);
%>
<tr class="table-row">
<th><%=i+1 %></th>
<%-- <td>
<%=arrow(bbs.getDepth()) %><!-- 여백+이미지 -->
<a href="bbsdetail.jsp?seq=<%=bbs.getSeq() %>">
<%=bbs.getTitle() %>
</a>
</td> --%>
<td>
<%
if(bbs.getDel() == 0){
%>
<%=arrow( bbs.getDepth() ) %>
<a href="bbsdetail.jsp?seq=<%=bbs.getSeq() %>">
<%=bbs.getTitle() %>
</a>
<%
}else{
%>
<font color="#ff0000">삭제된 글입니다</font>
<%
}
%>
</td>
<td align="center">
<%=bbs.getReadcount() %>
</td>
<td align="center">
<%=bbs.getId() %>
</td>
<td align="center">
<%=bbs.getWdate() %>
</td>
</tr>
<%
}
}
%>
</table>
<%
for(int i = 0; i < bbsPage; i++){
if(pageNumber == i){//1 [2] [3]
%>
<span class="span">
<%=i+1 %>
</span>
<%
}else{//그외 페이지
%>
<a class="aa" href="#none" title="<%=i+1 %>페이지" onclick="goPage(<%=i %>)">
<%=i+1 %>
</a>
<%
}
}
%>
</div>
<div class="div">
<!-- <form action="bbssearch.jsp" method="get"> -->
<select id="choice">
<option value="sel">----------선택해주세요</option>
<option value="title">제목</option>
<option value="content">내용</option>
<option value="writer">작성자</option>
</select>
<input type="text" id="search" placeholder="검색어를 입력해주세요">
<button class="btn" onclick="searchBbs()">검색</button>
<!-- </form> -->
</div>
<script type="text/javascript">
function write() {
location.href = "bbswrite.jsp";
}
function searchBbs() {
let choice = document.getElementById("choice").value;
let word = document.getElementById("search").value;
location.href = "bbslist.jsp?searchWord=" + word + "&choice="+choice;
}
function goPage(pageNum) {
location.href = "bbslist.jsp?pageNumber="+pageNum;
}
</script>
</body>
</html>
dao
//글의 모든 갯수
public int getAllBbs() {
String sql = " SELECT COUNT(*) FROM BBS ";
Connection conn = null;
PreparedStatement psmt = null;
ResultSet rs = null;
int len = 0;
try {
conn = DBConnection.getConnection();
psmt = conn.prepareStatement(sql);
rs = psmt.executeQuery();
if(rs.next()) {
len = rs.getInt(1);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
DBClose.close(psmt, conn, rs);
}
return len;
}
//페이징
public List<BbsDto> getBbsPagingList(String choice, String searchWord, int page) {
/*
1. row 번호
2. 검색
3. 정렬
4. 범위설정 1~10까지
*/
String sql = " SELECT SEQ, ID, REF, STEP, DEPTH,"
+ " TITLE, CONTENT, WDATE,"
+ " DEL, READCOUNT "
+ " FROM ";
sql += "(SELECT ROW_NUMBER()OVER(ORDER BY REF DESC, STEP ASC) AS RNUM, " +
" SEQ, ID, REF, STEP, DEPTH, TITLE, CONTENT, WDATE, DEL, READCOUNT " +
" FROM BBS ";
String sqlWord = "";
if (choice.equals("title")) {
sqlWord = " WHERE TITLE LIKE '%" + searchWord.trim() + "%' AND DEL=0";
}else if(choice.equals("writer")) {
sqlWord = " WHERE ID='" + searchWord.trim() + "'";
}else if(choice.equals("content")) {
sqlWord = " WHERE CONTENT LIKE '%" +searchWord.trim() + "%' ";
}
sql = sql + sqlWord;
sql += " ORDER BY REF DESC, STEP ASC)";
sql += " WHERE RNUM >=? AND RNUM <= ?";
int start, end;
start = 1 + 10 * page;
end = 10 + 10 * page;
/*
* String sql = " SELECT SEQ, ID, REF, STEP, DEPTH," + " TITLE, CONTENT, WDATE,"
* + " DEL, READCOUNT " + " FROM BBS ";
*
* String sqlWord = "";//아래 조건들이 다 안맞을경우 ""로 들어감 if (choice.equals("title")) {
* sqlWord = " WHERE TITLE LIKE '%" + searchWord.trim() + "%' AND DEL=0";
* //del=0은 del이 지워지지 않았을 떄 }else if (choice.equals("writer")) {//작성자명은 포함(중복)이
* 아닌 동일시 sqlWord = " WHERE ID='" + searchWord.trim() + "'"; }else if
* (choice.equals("content")) { sqlWord = " WHERE CONTENT LIKE '%" +
* searchWord.trim() + "%' AND DEL=0"; } sql = sql + sqlWord; sql +=
* " ORDER BY REF DESC, STEP ASC ";
*/
Connection conn = null;
PreparedStatement psmt = null;
ResultSet rs = null;
List<BbsDto> list = new ArrayList<BbsDto>();
try {
conn = DBConnection.getConnection();
System.out.println("1/6 getBbsList success");
psmt = conn.prepareStatement(sql);
psmt.setInt(1, start);
psmt.setInt(2, end);
System.out.println("2/6 getBbsList success");
rs = psmt.executeQuery();
System.out.println("3/6 getBbsList success");
while(rs.next()) {
int i = 1;
BbsDto dto = new BbsDto(rs.getInt(i++),
rs.getString(i++),
rs.getInt(i++),
rs.getInt(i++),
rs.getInt(i++),
rs.getString(i++),
rs.getString(i++),
rs.getString(i++),
rs.getInt(i++),
rs.getInt(i++));
list.add(dto);
}
System.out.println("4/6 getBbsList success");
} catch (Exception e) {
e.printStackTrace();
}finally {
DBClose.close(psmt, conn, rs);
}
return list;
}
'MVC1 > 게시판' 카테고리의 다른 글
게시판// 업데이트 (0) | 2020.07.30 |
---|---|
게시판// select search 검색 (0) | 2020.07.30 |
게시판// 글삭제, 댓글추가, 글 detail (0) | 2020.07.29 |
게시판 list/ 글쓰기 (0) | 2020.07.28 |