본문 바로가기
스프링 게시판

스프링게시판 만들기 리스트 , 페이지 나누기

by 뇽꾸리 2021. 1. 12.
반응형

옛날코드인데 20개이상 글을써야해서 올립니다. 별로 추천하지 않는 코드이고 노트북 포맷으로인해 다 날라가서 짤막짤막하게 저장한것들 대충 복구한상태라 조금 다를수도.. 

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width ,initial-scale=1">
<link rel="stylesheet"
href="<c:url value="/resources/user/css/bootstrap.min.css"/>">
<script src="https://code.jquery.com/jquery-2.2.3.min.js"></script>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="<c:url value="/resources/user/js/bootstrap.min.js"/>"></script>
<title>home</title>
<style>
body {
background-color: black
}
</style>

<script>
function list(page){
location.href="usernotice.do?curPage="+page
+"&search_option=${map.search_option}"
+"&keyword=${map.keyword}";
}
</script>
</head>

<body>
<br>
<%@include file="/resources/user/jsp/loginlogout.jsp"%> 
<header>
<h1 class="text-center"><a href="home.do"><img src="<c:url value="/resources/user/runaway.png"/>"></a></h1>
</header>
<br>
<%@include file="/resources/user/nav/nav.jsp"%> 
<br><br>
<!-- 공지게시판 -->
<br><br>
<div class="container">
<div class="row">
<div style="color:white">${map.count} 개의 게시물이 있습니다</div>
<br>
<form name="form1" method="post" action="usernotice.do">
<table style="text-align: center; border: 1px solid #BDBDBD">
<tr>
<td>
<select name="search_option" class="form-control">
<c:choose>
<c:when test="${map.search_option == 'all' }">
<option value="all" selected>내용 + 제목</option>
<option value="content">내용</option>
<option value="title">제목</option>
</c:when>
<c:when test="${map.search_option == 'title' }">
<option value="all">내용 + 제목</option>
<option value="title" selected>제목</option>
<option value="content">내용</option>
</c:when>
<c:when test="${map.search_option == 'content' }">
<option value="all">내용 + 제목</option>
<option value="title">제목</option>
<option value="content" selected>내용</option>
</c:when>
</c:choose>
</select>
</td>
<td><input name="keyword" value="${map.keyword}"></td>
<td><input class="btn btn-ouline-success" type="submit" value="search"></td>
</tr>
</table>
</form>
<br>
<form>
<table class="table table-striped table-bordered"
style="text-align: center; border: 1px solid #BDBDBD">
<thead>
<tr>
<th colspan="4" style= "background-color: #A6A6A6; text-align: center;"><h4><strong>공지사항</strong></h4></th>
</tr>
<tr>
<th style="background-color: #eeeeee; text-align: center;">제목</th>
<th style="background-color: #eeeeee; text-align: center;">날짜</th>
<th style="background-color: #eeeeee; text-align: center;">조회수</th>
<th style="background-color: #eeeeee; text-align: center;">작성일</th>
</tr>
</thead>
<tbody>
<c:forEach var="row" items="${map.list}">
<tr>
<td><a href="usernoticeview.do?bno=${row.bno}
<%--

&curPage=${map.pager.curPage}

&search_option=${map.search_option}

$keyword=${map.keyword} --%>">${row.title}</a>

<!-- 댓글의 갯수 -->

<c:if test="${row.cnt>0}">

<span style="color:black;">(${row.cnt})</span>

</c:if>

</td>

<td>${row.regdate}</td>

<td>${row.viewcnt}</td>

<td>${row.regdate}</td>

</tr>

</c:forEach>

</tbody>

​

​

<!-- 페이지 네비게이션 -->

<tr>

<td colspan="5" align="center">

​

<!--현재 페이지 블럭이 1보다 크면 처음페이지 이동 -->

<c:if test="${map.pager.curBlock > 1}">

<a href="javascript:list('1')">[처음]</a>

</c:if>

​

<!-- 현재 페이지블럭이 1보단크면 [이전]을 출력 -->

<c:if test="${map.pager.curBlock > 1}">

<a href="javascript:list('${map.pager.prevPage}')">[이전]</a>

</c:if>

​

<!-- 페이지블럭 처음부터 마지막 블럭까지 1씩 증가하는 페이지출력 -->

<c:forEach var="bno" begin="${map.pager.blockBegin}"

end="${map.pager.blockEnd}">

<c:choose>

<c:when test="${bno == map.pager.curPage}">

<span style="color:black;">${bno}</span> 

</c:when>

<c:otherwise>

<a href="javascript:list('${bno}')">${bno}</a> 

</c:otherwise>

</c:choose>

</c:forEach>

​

<!-- 다음 페이지 블록으로 이동 -->

<c:if test="${map.pager.curBlock <=map.pager.totBlock}">

<a href="javascript:list('${map.pager.nextPage}')">[다음]</a>

</c:if>

​

<!-- 끝 페이지로 이동 -->

<c:if test="${map.pager.curBlock <= map.pager.totPage}">

<a href="javascript:list('${map.pager.totPage}')">[끝]</a>

</c:if>

</td>

</tr>

</table>

</form>

</div>

</div>

</body>

</html>

 

<select id="noticeList" resultType="noticeBoardVO">
<include refid="paging_header"/>
select bno,title,writer,username,viewcnt,regdate
,(select count(*) from noticereply where bno=b.bno) cnt
from noticeboard2 b,member m 
<include refid="search"></include>
order by bno desc
<include refid="paging_footer"/>
</select>

<select id="countarticle" resultType="int">
select count(*) from noticeboard2 b, member m 
<include refid="search"></include>
</select>

<sql id="search">
<choose>
<when test="search_option == 'all' "> 
where b.writer = m.userid
and (username like '%' ||#{keyword}|| '%'
or content like '%' ||#{keyword}|| '%'
or title like '%' || #{keyword}|| '%'
)
</when>
<otherwise>
where b.writer = m.userid
and ${search_option} like '%'||#{keyword}||'%'
</otherwise>
</choose>
</sql>

<sql id="paging_header">
SELECT *
FROM(
select rownum as rn,A.*
from(
</sql>
<sql id="paging_footer">
)A
)
where rn between #{start} and #{end}
</sql>
public interface NoticeBoardDAO {
 public List<NoticeBoardVO> noticeList(int start, int end, String search_option, String keyword)throws Exception;
 public int countarticle(String search_option, String keyword) throws Exception 
}
@Repository
public class NoticeBoardDAOImpl implements NoticeBoardDAO {

@Inject
SqlSession session;

@Override
public List<NoticeBoardVO> noticeList(int start, int end, String search_option, String keyword) throws Exception {
Map<String,Object> map = new HashMap<String,Object>();
map.put("search_option", search_option);
map.put("keyword", keyword);
map.put("start", start);
map.put("end", end);
return session.selectList("user.noticeList",map);

@Override
public int countarticle(String search_option, String keyword) throws Exception {
Map<String,String> map = new HashMap<String, String>();
map.put("search_option", search_option);
map.put("keyword", keyword);
return session.selectOne("user.countarticle",map);
}

}
public interface NoticeBoardService {
    public List<NoticeBoardVO> noticeList(int start, int end, String search_option, String keyword) throws Exception;
    public int countarticle(String search_option, String keyword)throws Exception;
}
@Service
public class NoticeBoardServiceImpl implements NoticeBoardService {

@Inject
NoticeBoardDAO dao;

@Override
public List<NoticeBoardVO> noticeList(int start, int end, String search_option, String keyword) throws Exception {
return dao.noticeList(start, end, search_option, keyword);
}

@Override
public int countarticle(String search_option, String keyword)throws Exception {
return dao.countarticle(search_option, keyword);
}

}
@Controller
public class NoticeBoardController {

@Inject
NoticeBoardService service;

@RequestMapping(value = "/usernotice.do")
public ModelAndView noticeList(@RequestParam(defaultValue = "1") int curPage,
@RequestParam(defaultValue = "all") String search_option, @RequestParam(defaultValue = "") String keyword)
throws Exception {
int count = service.countarticle(search_option, keyword);
Pager pager = new BoardPager(count, curPage);
int start = pager.getPageBegin();
int end = pager.getPageEnd();
List<NoticeBoardVO> noticeList = service.noticeList(start, end, search_option, keyword);
ModelAndView mav = new ModelAndView();
mav.setViewName("usernotice");
Map<String, Object> map = new HashMap<String, Object>();
map.put("list", noticeList);
map.put("count", count);
map.put("search_option", search_option);
map.put("keyword", keyword);
map.put("pager", pager);
mav.addObject("map", map);

return mav;

}
package com.spring.crazy.model;

public class BoardPager {

	public static final int PAGE_SCALE = 5;

	public static final int BLOCK_SCALE = 5;

	private int curPage;
	private int prevPage;
	private int nextPage;
	private int totPage;
	private int totBlock;
	private int curBlock;
	private int prevBlock;
	private int nextBlock;

	private int pageBegin;
	private int pageEnd;

	private int blockBegin;

	private int blockEnd;

	public BoardPager(int count, int curPage) {
		curBlock = 1;
		this.curPage = curPage;
		setTotPage(count);
		setPageRange();
		setTotBlock();
		setBlockRange();
	}

	public void setBlockRange() {

		curBlock = (int) Math.ceil((curPage - 1) / BLOCK_SCALE) + 1;

		blockBegin = (curBlock - 1) * BLOCK_SCALE + 1;

		blockEnd = blockBegin + BLOCK_SCALE - 1;

		if (blockEnd > totPage)
			blockEnd = totPage;

		prevPage = (curPage == 1) ? 1 : (curBlock - 1) * BLOCK_SCALE;

		nextPage = curBlock > totBlock ? (curBlock * BLOCK_SCALE) : (curBlock * BLOCK_SCALE) + 1;

		if (nextPage >= totPage)
			nextPage = totPage;
	}

	public void setPageRange() {

		pageBegin = (curPage - 1) * PAGE_SCALE + 1;

		pageEnd = pageBegin + PAGE_SCALE - 1;
	}

	public int getCurPage() {
		return curPage;
	}

	public void setCurPage(int curPage) {
		this.curPage = curPage;
	}

	public int getPrevPage() {
		return prevPage;
	}

	public void setPrevPage(int prevPage) {
		this.prevPage = prevPage;
	}

	public int getNextPage() {
		return nextPage;
	}

	public void setNextPage(int nextPage) {
		this.nextPage = nextPage;
	}

	public int getTotPage() {
		return totPage;
	}

	public void setTotPage(int count) {

		totPage = (int) Math.ceil(count * 1.0 / PAGE_SCALE);
	}

	public int getTotBlock() {
		return totBlock;
	}

	public void setTotBlock() {

		totBlock = (int) Math.ceil(totPage / BLOCK_SCALE);
	}

	public int getCurBlock() {
		return curBlock;
	}

	public void setCurBlock(int curBlock) {
		this.curBlock = curBlock;
	}

	public int getPrevBlock() {
		return prevBlock;
	}

	public void setPrevBlock(int prevBlock) {
		this.prevBlock = prevBlock;
	}

	public int getNextBlock() {
		return nextBlock;
	}

	public void setNextBlock(int nextBlock) {
		this.nextBlock = nextBlock;
	}

	public int getPageBegin() {
		return pageBegin;
	}

	public void setPageBegin(int pageBegin) {
		this.pageBegin = pageBegin;
	}

	public int getPageEnd() {
		return pageEnd;
	}

	public void setPageEnd(int pageEnd) {
		this.pageEnd = pageEnd;
	}

	public int getBlockBegin() {
		return blockBegin;
	}

	public void setBlockBegin(int blockBegin) {
		this.blockBegin = blockBegin;
	}

	public int getBlockEnd() {
		return blockEnd;
	}

	public void setBlockEnd(int blockEnd) {
		this.blockEnd = blockEnd;
	}
}
반응형

댓글