본문 바로가기

Spring

페이징/서치/search jsp로 불러오기

paging.jsp

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

<!--  Paging 연산 
	<< < [1] [2] 3 [4] [5] > >>
	
	[1] ~ [10]  [11] ~ [20]
-->


<%
	int totalRecordCount;	//전체 글 수 
	int pageNumber;			//현재 페이지 
	int pageCountPerScreen;	//스크린당 페이지 수 [1]~[5]  
	int recordCountPerPage;	//페이지당 글 수 
	
	String st1 = request.getParameter("totalRecordCount");
	if(st1 == null) totalRecordCount = 0;
	else			totalRecordCount = Integer.parseInt(st1);

	String st2 = request.getParameter("pageNumber");
	if(st2 == null) pageNumber = 0;
	else			pageNumber = Integer.parseInt(st2);
	
	String st3 = request.getParameter("pageCountPerScreen");
	if(st3 == null) pageCountPerScreen = 0;
	else			pageCountPerScreen = Integer.parseInt(st3);
	
	String st4 = request.getParameter("recordCountPerPage");
	if(st4 == null) recordCountPerPage = 0;
	else			recordCountPerPage = Integer.parseInt(st4);
	
	// 총 페이지수 	23 -> 3페이지 
	int totalPageCount = totalRecordCount / recordCountPerPage;
	//		2		=		23			/			10
	
	if( (totalRecordCount % recordCountPerPage) != 0){
		totalPageCount++;	// 3
	}
	
	//시작페이지  1,11,21 ~ 
	int screenStartPageIndex = ((pageNumber + 1) / pageCountPerScreen) * pageCountPerScreen;
	//			0					3					10						10
	//								9					10					
	
	//끝 페이지 0부터 10보다 작을 때 까지 
	int screenEndPageIndex = (((pageNumber + 1) / pageCountPerScreen) * pageCountPerScreen) + pageCountPerScreen;
	//			10	
	
	//끝페이지는 다시 계산 [1] ~ [10] -> [1] ~ [3]
	if(screenEndPageIndex > totalPageCount){
		screenEndPageIndex = totalPageCount;
	}
	
	//0 ~ 9 -> 1 ~ 10
	if((pageNumber + 1) % pageCountPerScreen == 0){
		screenStartPageIndex = (((pageNumber + 1) / pageCountPerScreen) * pageCountPerScreen) - pageCountPerScreen;
		screenEndPageIndex = pageNumber + 1;		// 0 ->	1	9 -> 10 
	}
	
%>

<!-- Paging 출력  -->
<div style="float: left; width: 96%; text-align: center;">
<!-- << 제일 첫번째 페이지로 이동 -->
	<a href="#none" title="처음페이지" onclick="goPage(0)">
		<img alt="" src="./image/arrow_first.gif" style="width: 10px; height: 10px;">
	</a>
	
	
	<!-- < 이 전 페이지로 이동(총 글 수가 100개가 넘어야 보인다 )  -->
	<%
	if(screenStartPageIndex > 1){	//screenStartPageIndex 1=(1~10)  2=(11~20)
		%>
		<a href="#none" title="이전페이지" onclick="goPage('<%=screenStartPageIndex-1 %>')">
			<img alt="" src="./image/arrow_back.gif" style="width: 10px; height: 10px;">
		</a>
		
		<%
	}
	%>
	
	<!-- [1] 2 [3] -->
	<%
	for(int i = screenStartPageIndex; i < screenEndPageIndex; i++){
		if(i == pageNumber){	//현재 페이지 
			%>
				<span style="font-size: 10pt; color: #000; font-weight: bold;">
					<%=i+1 %>
				</span>
			<%
		}else{					//그 외의 페이지 
			%>
				<a href="#none" title="<%=i+1 %>페이지" onclick="goPage(<%=i%>)" 
					style="font-size: 9.5pt; color: #000; font-weight: normal;">
					[<%=i+1 %>]
				</a>
			<%
		}
	}
	%>
	
	<!--  >  -->
	<%
	if(screenEndPageIndex < totalPageCount){
		%>
			<a href="#none" title="다음페이지" onclick="goPage(<%=screenEndPageIndex%>)">
				<img alt="" src="./image/arrow_next.gif" style="width: 10px; height: 10px">
			</a>
		<%
	}
	int end_page = 0;
	if(totalPageCount > 0){
		end_page = totalPageCount -1;
	}
	%>
	
	<!-- >> -->
	<a href="#none" title="마지막페이지" onclick="goPage(<%=end_page%>)">
		<img alt="" src="./image/arrow_end.gif" style="width: 10px; height: 10px;">
	</a>
</div>

 

 

 

 

 

 

 

bbslist.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
	String choice = (String)request.getAttribute("choice");
	if(choice == null) choice = "";
	
	String searchWord = (String)request.getAttribute("searchWord");
	if(searchWord == null) searchWord = ""; 
%>
<script>
let choice = "<%=choice %>";
let searchWord = "<%=searchWord %>";
$(document).ready(function(){
	$("#_choice").val(choice);
	//$("#_searchWord").val(searchWord);

	document.frmForm1.searchWord.value = searchWord;
});
</script>



<!-- 검색 -->
<div class="box_border" style="margin-top: 5px; margin-bottom: 10px">

<form action="" name="frmForm1" id="_frmFormSearch" method="get">

<table style="margin-left: auto; margin-right: auto; margin-top: 3px; margin-bottom: 3px">
<tr>
	<td>검색</td>
	<td style="padding-left: 5px">
		<select id="_choice" name="choice">
			<option value="" selected="selected">선택</option>
			<option value="title">제목</option>
			<option value="content">내용</option>
			<option value="writer">작성자</option>		
		</select>
	</td>
	<td style="padding-left: 5px">
		<input type="text" id="_searchWord" name="searchWord">		
	</td>
	<td style="padding-left: 5px">
		<span class="button blue">
			<button type="button" id="btnSearch">검색</button>
		</span>
	</td>
</tr>
</table>

<!-- search 를 눌렀을 때 같이 날라가야해서 추가 기입이 필요  -->
<input type="hidden" name="pageNumber" id="_pageNumber" value="${(empty pageNumber)?0:pageNumber }">
<input type="hidden" name="recordCountPerPage" id="_recordCountPerPage" value="${(empty recordCountPerPage)?0:recordCountPerPage }">
</form>

</div>


<!-- paging 생성!! -->








<!-- arrow class 생성 -->
<jsp:useBean id="ubbs" class="bit.com.a.util.BbsArrow" />

<table class="list_table" style="width: 85%">
<colgroup>
	<col style="width:70px">
	<col style="width:auto">
	<col style="width:100px">
</colgroup>

<thead>
	<tr>
		<th>번호</th><th>제목</th><th>작성자</th>
	</tr>
</thead>

<tbody>
	<c:if test="${empty bbslist }">
	<tr>
		<td colspan="3">작성된 글이 없습니다</td>		
	</tr>	
	</c:if>


	<c:forEach items="${bbslist }" var="bbs" varStatus="vs">
	
		<!-- arrow를 setting -->
		<jsp:setProperty property="depth" name="ubbs" value="${bbs.depth }"/>
		
		<tr class="_hover_tr">
			<td>${vs.count }</td>
			<td style="text-align: left;">
				<!-- arrow를 출력 -->
				<jsp:getProperty property="arrow" name="ubbs"/>
				
				<c:if test="${bbs.del eq 1}">
					***이 글은 관리자에 의해서 삭제 되었습니다***
				</c:if>
				<c:if test="${bbs.del eq 0}">
					<a href="bbsdetail.do?seq=${bbs.seq }">
						${bbs.title}
					</a>					
				</c:if>			
			</td>
			<td>${bbs.id}</td>
		</tr>		
	</c:forEach>
</tbody>
</table>
<br>

<!-- Paging!!!!!!  -->
<div id="paging_wrap">
	<jsp:include page="/WEB-INF/views/bbs/paging.jsp" flush="false">
		<jsp:param value="${totalRecordCount }" name="totalRecordCount"/>
		<jsp:param value="${pageNumber }" name="pageNumber"/>
		<jsp:param value="${pageCountPerScreen }" name="pageCountPerScreen"/>
		<jsp:param value="${recordCountPerPage }" name="recordCountPerPage"/>
		
	</jsp:include>
</div>

<script>
function goPage( pageNumber ){
	$("#_pageNumber").val( pageNumber );
	$("#_frmFormSearch").attr("action","bbslist.do").submit();
}

$("#btnSearch").click(function(){
	//alert("써취 ");
	$("#_frmFormSearch").attr("action","bbslist.do").submit();
});
</script>

 

 

 

 

 

 

 

bbsController.java

package bit.com.a.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import bit.com.a.dto.BbsDto;
import bit.com.a.dto.BbsParam;
import bit.com.a.service.BbsService;

@Controller
public class BbsController {
	
	@Autowired
	BbsService service;	
	
/*	@RequestMapping(value = "bbslist.do", method = RequestMethod.GET)
	public String bbslist(Model model) {
		model.addAttribute("doc_title", "글목록");
		
		List<BbsDto> list = service.getBbsList();
		model.addAttribute("bbslist", list);
		
		return "bbslist.tiles";
	}
*/	
	//paging추가 
	@RequestMapping(value = "bbslist.do", method = RequestMethod.GET)
	public String bbslist(Model model, BbsParam param) {
		model.addAttribute("doc_title", "글목록");
		
		System.out.println("choice:"+ param.getChoice());
		System.out.println("search:"+param.getSearchWord());
		
		// paging 처리
		int sn = param.getPageNumber();		// 현재 페이지
		int start = sn * param.getRecordCountPerPage() + 1;	// 1 11 21
		int end = (sn + 1) * param.getRecordCountPerPage();	// 10 20 30
		
		param.setStart(start);
		param.setEnd(end);
				
		List<BbsDto> list = service.getBbsList( param );
		model.addAttribute("bbslist", list);
		
		// 글의 총수
		int totalRecordCount = service.getBbsCount( param );
		
		model.addAttribute("pageNumber", sn);
		model.addAttribute("pageCountPerScreen", 10);
		model.addAttribute("recordCountPerPage", param.getRecordCountPerPage());
		model.addAttribute("totalRecordCount", totalRecordCount);		
		
		model.addAttribute("choice", param.getChoice());
		model.addAttribute("searchWord", param.getSearchWord());
		
		return "bbslist.tiles";
	}
	
	
	
	@RequestMapping(value = "bbswrite.do", method = RequestMethod.GET)
	public String bbswrite() {
		System.out.println("bbs write()" + new Date());
		
		return "bbswrite.tiles";
	}
	
	@RequestMapping(value = "bbswriteAf.do", method = RequestMethod.POST)
	public String bbswriteAf(BbsDto dto) {
		System.out.println("bbs writeAf()" + new Date());
		boolean b = service.writeBbs(dto);
		if(b) {
			System.out.println("글추가 성공 ");
			return "redirect:/bbslist.do";	
		}else {
			System.out.println("글추가 실패 ");
			return "redirect:/bbslist.do";	
		}		
	}
	
	@RequestMapping(value = "bbsdetail.do", method = RequestMethod.GET)
	public String bbsdetail(int seq, Model model) {
		System.out.println("bbs detail()" + new Date());
		
		BbsDto bbs = service.getBbs(seq);
		model.addAttribute("bbs", bbs);
		
		return "bbsdetail.tiles";
	}
	
	@RequestMapping(value = "bbsupdate.do", method = RequestMethod.GET)
	public String bbsupdate(int seq, Model model) {
		System.out.println("bbs update()" + new Date());
		
		BbsDto bbs = service.getBbs(seq);
		model.addAttribute("bbs", bbs);
		
		return "bbsupdate.tiles";
	}
	
	@RequestMapping(value = "bbsupdateAf.do", method = RequestMethod.POST)
	public String bbsupdateAf(BbsDto dto) {
		System.out.println("bbs updateAf()" + new Date());
		System.out.println(dto.toString());
		boolean b = service.updateBbs(dto);
		if(b) {
			System.out.println("글수정 성공 ");
			return "redirect:/bbslist.do";	
		}else {
			System.out.println("글수정 실패 ");
			return "redirect:/bbslist.do";	
		}		
	}
	
	@RequestMapping(value = "bbsdelete.do", method = RequestMethod.GET)
	public String bbsdelete(int seq) {
		System.out.println("bbs delete()" + new Date());
		boolean b = service.deleteBbs(seq);
		if(b) {
			System.out.println("삭제 성공 ");
			return "redirect:/bbslist.do";	
		}else {
			System.out.println("삭제 실패 ");
			return "redirect:/bbslist.do";	
		}		
	}
	
	
	@RequestMapping(value = "answer.do", method = RequestMethod.GET)
	public String answer(int seq,Model model) {
		System.out.println("answer()"+ seq);
		
		BbsDto dto = service.getBbs(seq);
		model.addAttribute("dto", dto);

		return "answer.tiles";
	}
	
	@RequestMapping(value = "answerAf.do", method = RequestMethod.POST)
	public String answerAf(BbsDto dto) {
		System.out.println("answerAf()"+ dto.toString());
		System.out.println(dto.toString());
		boolean b = service.replyBbsInsert(dto);
		
		if(b) {
			System.out.println("추가 성공 ");
			return "redirect:/bbslist.do";	
		}else {
			System.out.println("추가 실패 ");
			return "redirect:/bbslist.do";	
			
		}		
	}		
}

 

 

 

 

 

sqls-> Bbs.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  
<mapper namespace="Bbs">

<!-- <select id="bbslist" resultType="bit.com.a.dto.BbsDto">
	SELECT SEQ, ID, REF, STEP, DEPTH, TITLE, CONTENT, WDATE, DEL, READCOUNT 
	FROM BBS
	ORDER BY REF DESC, STEP ASC
</select> -->

<select id="bbslist" parameterType="bit.com.a.dto.BbsParam" 
		resultType="bit.com.a.dto.BbsDto">
	SELECT SEQ, ID, REF, STEP, DEPTH, TITLE, CONTENT, WDATE, DEL, READCOUNT 
	FROM 
		( SELECT ROW_NUMBER()OVER(ORDER BY REF DESC, STEP ASC)AS RNUM,
				SEQ, ID, REF, STEP, DEPTH, TITLE, CONTENT, WDATE, DEL, READCOUNT 
			FROM BBS
			WHERE 1=1
		<if test="choice != null and choice != ''
			and searchWord != null and searchWord != ''">
			<if test="choice == 'title'">
				AND TITLE LIKE '%'||#{searchWord}||'%'
			</if>
			<if test="choice == 'content'">
				AND CONTENT LIKE '%'||#{searchWord}||'%'
			</if>
			<if test="choice == 'writer'">
				AND ID=#{searchWord}
			</if>	
		</if>
		) A
		WHERE RNUM BETWEEN ${start} AND ${end}
</select>

<!-- 글의 총 수  -->
<select id="getBbsCount" parameterType="bit.com.a.dto.BbsParam" resultType="java.lang.Integer">
	SELECT NVL(COUNT(*), 0) AS CNT
	FROM BBS
	WHERE 1=1
		<if test="choice != null and choice != ''
			and searchWord != null and searchWord != ''">
			<if test="choice == 'title'">
				AND TITLE LIKE '%'||#{searchWord}||'%'
			</if>
			<if test="choice == 'content'">
				AND CONTENT LIKE '%'||#{searchWord}||'%'
			</if>
			<if test="choice == 'writer'">
				AND ID=#{searchWord}
			</if>	
		</if>
</select>


<insert id="writeBbs" parameterType="bit.com.a.dto.BbsDto">
	INSERT INTO BBS(SEQ, ID, REF, STEP, DEPTH, TITLE, CONTENT, WDATE, DEL, READCOUNT)
	VALUES(SEQ_BBS.NEXTVAL, #{id}, (SELECT NVL(MAX(REF)+1, 0) FROM BBS), 0, 0, 
			#{title}, #{content}, SYSDATE, 0, 0)
</insert>

<insert id="updateBbs" parameterType="bit.com.a.dto.BbsDto">
		UPDATE BBS 
		SET TITLE=#{title} , CONTENT=#{content} 
		WHERE SEQ=#{seq}
</insert>

<insert id="deleteBbs" parameterType="java.lang.Integer">
	UPDATE BBS 
	SET DEL=1 
	WHERE SEQ=#{seq}
</insert>




<select id="getBbs" parameterType="java.lang.Integer"
	resultType="bit.com.a.dto.BbsDto">
	SELECT SEQ, ID, REF, STEP, DEPTH, TITLE, CONTENT, WDATE, DEL, READCOUNT 
	FROM BBS
	WHERE SEQ=#{seq}
</select>

<update id="readCount" parameterType="java.lang.Integer">
	UPDATE BBS
	SET READCOUNT=READCOUNT+1
	WHERE SEQ=#{seq}
</update>

<update id="replyBbsUpdate"
	parameterType="bit.com.a.dto.BbsDto">
	UPDATE BBS 
	SET STEP=STEP+1
	WHERE REF=(SELECT REF FROM BBS WHERE SEQ=#{seq})
		AND STEP>(SELECT STEP FROM BBS WHERE SEQ=#{seq})		
</update>

<insert id="replyBbsInsert"
	parameterType="bit.com.a.dto.BbsDto">
	INSERT INTO BBS
		(SEQ, ID, REF, STEP, DEPTH, 
		TITLE, CONTENT, WDATE, DEL, READCOUNT)
	VALUES(SEQ_BBS.NEXTVAL, #{id},
		(SELECT REF FROM BBS WHERE SEQ=#{seq}),
		(SELECT STEP FROM BBS WHERE SEQ=#{seq})+1,
		(SELECT DEPTH FROM BBS WHERE SEQ=#{seq})+1,
		#{title}, #{content}, SYSDATE,
		0, 0)	
</insert>


<select id="getBbsSearchList" parameterType="bit.com.a.dto.BbsParam"
	resultType="bit.com.a.dto.BbsDto">
	SELECT SEQ, ID, REF, STEP, DEPTH, TITLE, CONTENT, WDATE, DEL, READCOUNT
	FROM BBS
	WHERE 1=1
	<if test="choice != null and choice != ''
			and searchWord != null and searchWord != ''">
		<if test="choice == 'title'">
			AND TITLE LIKE '%'||#{searchWord}||'%'
		</if>
		<if test="choice == 'content'">
			AND CONTENT LIKE '%'||#{searchWord}||'%'
		</if>
		<if test="choice == 'writer'">
			AND ID=#{searchWord}
		</if>	
	</if>
	ORDER BY REF DESC, STEP ASC

</select>


</mapper>

 

 

 

 

 

BbsDao.java

package bit.com.a.dao;

import java.util.List;

import bit.com.a.dto.BbsDto;
import bit.com.a.dto.BbsParam;

public interface BbsDao {

	List<BbsDto> getBbsList(BbsParam param);	
	int getBbsCount(BbsParam param);
	
	
	boolean writeBbs(BbsDto dto);
	boolean updateBbs(BbsDto dto);
	boolean deleteBbs(int seq);
	
	BbsDto getBbs(int seq);
	void readCount(int seq);
	
	boolean replyBbsUpdate(BbsDto bbs);
	boolean replyBbsInsert(BbsDto dto);
	
//	List<BbsDto> getBbsList(BbsParam bbs);	
	
}

 

 

 

 

 

BbsService.java

package bit.com.a.service;

import java.util.List;

import bit.com.a.dto.BbsDto;
import bit.com.a.dto.BbsParam;

public interface BbsService {

	List<BbsDto> getBbsList(BbsParam param);
	int getBbsCount(BbsParam param);
	boolean deleteBbs(int seq);
	
	boolean writeBbs(BbsDto dto);
	boolean updateBbs(BbsDto dto);
	
	BbsDto getBbs(int seq);
	void readCount(int seq);
	
	public void reply(BbsDto bbs);
	
	boolean replyBbsInsert(BbsDto dto);
	
//	List<BbsDto> getBbsList(BbsParam bbs);
}

'Spring' 카테고리의 다른 글

ajax  (0) 2020.09.09
페이지네이션 pagination 파일  (0) 2020.09.09
코어, 타일즈 태그  (0) 2020.09.07
Tiles 타일즈  (0) 2020.09.07
AOP: 관점지향 프로그래밍  (0) 2020.09.07