본문 바로가기

MVC1/게시판

게시판// select search 검색

검색버튼을 누르면 list화면을 리셋시키는 원리

-> dao에서 getlist를 오버라이딩하여 select에서 선택한 검색 조건과, 검색어 search를 넣어줌

-> 버튼을 눌렀을때 list화면 java칸에서 값을 호출해와 뿌려주는 형식

 

 

 

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 = "&nbsp;&nbsp;&nbsp;&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();
	//List<BbsDto> list = dao.getBbsList();
	List<BbsDto> list = dao.getBbsList(choice, searchWord);
%>
<!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%; */
	
}
.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">&nbsp;&nbsp;로그아웃</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="3">작성된 글이 없습니다</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>
</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;
}
</script>
</body>
</html>

 

 

 

 

BbsDao.java

	//목록 오버라이딩 select
		public List<BbsDto> getBbsList(String choice, String searchWord) {
			
			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);
				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
게시판// 업데이트  (0) 2020.07.30
게시판// 글삭제, 댓글추가, 글 detail  (0) 2020.07.29
게시판 list/ 글쓰기  (0) 2020.07.28