이번에는 글 목록, 즉 List 셋팅에 대한 부분이다. 본 프로그램은 MVC Model 1으로 제작되었다.
■ bbslist.jsp (View)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
<%@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를 추가하는 함수 depth=1 ' '-> depth=2 ' '->
public String arrow(int depth){
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){
%>
<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 = "";
}
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:" + 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();
int len = dao.getAllBbs(choice, searchWord);
System.out.println("총 글의 갯수:" + len);
int bbsPage = len / 10; // 예: 12개 -> 2page
if(len % 10 > 0){
bbsPage = bbsPage + 1; // -> 2
}
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>bbslist.jsp</title>
<script type="text/javascript">
$(document).ready(function() {
let searchWord = "<%=searchWord %>";
if(searchWord == "") return;
let obj = document.getElementById("choice");
obj.value = "<%=choice%>";
obj.setAttribute("selected", "selected");
});
</script>
</head>
<body>
<h4 align="right" style="background-color: #f0f0f0">
환영합니다 <%=mem.getId() %>님
</h4>
<h1>게시판</h1>
<div align="center">
<table border="1">
<col width="70"><col width="600"><col width="150">
<tr>
<th>번호</th><th>제목</th><th>작성자</th>
</tr>
<%
if(list == null || list.size() == 0){
%>
<tr>
<td colspan="3">작성된 글이 없습니다</td>
</tr>
<%
}else{
for(int i = 0;i < list.size(); i++){
BbsDto bbs = list.get(i);
%>
<tr>
<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.getId() %></td>
</tr>
<%
}
}
%>
</table>
<%
for(int i = 0;i < bbsPage; i++){
if(pageNumber == i){ // 1 [2] [3]
%>
<span style="font-size: 15pt; color: #0000ff; font-weight: bold;">
<%=i+1 %>
</span>
<%
}
else{ // 그외 페이지
%>
<a href="#none" title="<%=i+1 %>페이지" onclick="goPage(<%=i %>)"
style="font-size: 15pt; color: #000; font-weight:bold; text-decoration: none">
[<%=i+1 %>]
</a>
<%
}
}
%>
<br><br>
<a href="bbswrite.jsp">글쓰기</a>
</div>
<br>
<div align="center">
<select id="choice"> <!-- 높이:20 중간맞춤 -->
<option value="sel">선택</option>
<option value="title">제목</option>
<option value="writer">작성자</option>
<option value="content">내용</option>
</select>
<input type="text" id="search" value="<%=searchWord %>">
<button onclick="searchBbs()">검색</button>
</div>
<script type="text/javascript">
function searchBbs() {
var choice = document.getElementById("choice").value;
var word = document.getElementById("search").value;
// alert(choice);
// alert(word);
/* if(word == ""){
document.getElementById("search").value = "";
document.getElementById("choice").value = 'sel';
} */
location.href = "bbslist.jsp?searchWord=" + word + "&choice=" + choice;
}
function goPage( pageNum ) {
var choice = document.getElementById("choice").value;
var word = document.getElementById("search").value;
// location.href = "bbslist.jsp?pageNumber=" + pageNum;
location.href = "bbslist.jsp?searchWord=" + word + "&choice=" + choice + "&pageNumber=" + pageNum;
}
</script>
</body>
</html>
|
cs |
■ BbsDao.jsp (View)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
public BbsDto getBbs(int seq) {
String sql = " SELECT SEQ, ID, REF, STEP, DEPTH, "
+ " TITLE, CONTENT, WDATE,"
+ " DEL, READCOUNT "
+ " FROM BBS "
+ " WHERE SEQ=? ";
Connection conn = null;
PreparedStatement psmt = null;
ResultSet rs = null;
BbsDto dto = null;
try {
conn = DBConnection.getConnection();
System.out.println("1/6 getBbs success");
psmt = conn.prepareStatement(sql);
System.out.println("2/6 getBbs success");
psmt.setInt(1, seq);
rs = psmt.executeQuery();
System.out.println("3/6 getBbs success");
if(rs.next()) {
int i = 1;
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++));
}
System.out.println("4/6 getBbs success");
} catch (Exception e) {
System.out.println("getBbs fail");
e.printStackTrace();
} finally {
DBClose.close(psmt, conn, rs);
}
return dto;
}
public void readcount(int seq) {
String sql = " UPDATE BBS "
+ " SET READCOUNT=READCOUNT+1 "
+ " WHERE SEQ=? ";
Connection conn = null;
PreparedStatement psmt = null;
try {
conn = DBConnection.getConnection();
System.out.println("1/6 readcount success");
psmt = conn.prepareStatement(sql);
psmt.setInt(1, seq);
System.out.println("2/6 readcount success");
psmt.executeUpdate();
System.out.println("3/6 readcount success");
} catch (Exception e) {
System.out.println("readcount fail");
e.printStackTrace();
} finally {
DBClose.close(psmt, conn, null);
}
}
|
cs |
public List<BbsDto> getBbsList() {
String sql = " SELECT SEQ, ID, REF, STEP, DEPTH, "
+ " TITLE, CONTENT, WDATE, "
+ " DEL, READCOUNT "
+ " FROM BBS "
+ " 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);
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;
}
'Side Project' 카테고리의 다른 글
#06. [Side Project 1] JSP 로그인, 게시판 만들기 (6) :: 글 삭제 (0) | 2020.07.30 |
---|---|
#05. [Side Project 1] JSP 로그인, 게시판 만들기 (5) :: 글 내용 Detail 자세히 보기 (0) | 2020.07.30 |
#04. [Side Project 1] JSP 로그인, 게시판 만들기 (4) :: 답글, 댓글, (0) | 2020.07.30 |
#02. [Side Project 1] JSP 로그인, 게시판 만들기 (2) :: 글 작성, 글쓰기 (0) | 2020.07.30 |
#01. [Side Project 1] JSP 로그인, 게시판 만들기 (1) 로그인, 회원가입, ID체크 Model + View (0) | 2020.07.28 |