SpringBoot 타임리프 page처리

jyp·2023년 3월 24일
0

맥북으로 혼공하기

목록 보기
22/32

java 쪽 처리 코드

@Override
public String list(Model model,HttpServletRequest request) {
	
	int page;
	
	if(request.getParameter("page")==null)
	{
		page = 1;
	}
	else
	{
		page = Integer.parseInt(request.getParameter("page"));
	}
	
	int index = (page-1)*20;
	int pstart = page/10;
	
	if(page%10==0)
		pstart = pstart-1;
	
	pstart = pstart*10+1;
	int pend = pstart+9;
	
	int chong = mapper.getChong();
	
	if(pend>chong)
		pend = chong;
	
	ArrayList<ThboardVo>list = mapper.list(index);
	
	model.addAttribute("page",page);
	model.addAttribute("pstart",pstart);
	model.addAttribute("pend",pend);
	model.addAttribute("chong",chong);
	
	model.addAttribute("list",list);
	
	return "list";
}

mapper 처리

public ArrayList<ThboardVo> list (@Param("index") int index);
public int getChong();

mapper.xml

<select id="list" resultType="com.example.demo.vo.ThboardVo">
	select *from board order by id desc limit #{index},10
</select>

<select id="getChong" resultType="int">
	select ceil(count(*)/10) from board
</select>

html

<table width="800" align="center">
	<tr>
		<td> 제 목 </td>
		<td> 작성자 </td>
		<td> 조회수 </td>
		<td> 작성일 </td>
	</tr>
	
	<tr th:each="bvo:${list}">
		<td th:text="${bvo.title}"> </td>
		<td th:text="${bvo.name}"> </td>
		<td th:text="${bvo.readnum}"> </td>
		<td th:text="${bvo.writeday}"> </td>
	</tr>
	
	<!-- pstart부터 pend까지 숫자 출력하기 -->
	<tr>
		<td colspan="4" align="center">
		
		<!-- 이전 그룹 -->
		<a th:if="${pstart != 1}" th:href="@{list(page=${pstart-1})}"> 이전 그룹 </a>
		<span th:if="${pstart == 1}"> 이전 그룹 </span>
		
		<!-- 이전페이지 이동 -->
		<a th:if="${page != 1}" th:href="@{list(page=${page-1})}"> 이전 </a>
		<span th:if="${page == 1}"> 이전 </span>
		
		<!-- 페이지 출력되는 부분 -->
			<th:block th:each="i:${#numbers.sequence(pstart,pend)}">
				<a th:if="${page != i}" th:href="@{list(page=${i})}" th:text="${i}"></a>
				<a th:if="${page == i}" style="color:red;" th:href="@{list(page=${i})}" th:text="${i}">
			</th:block>
			
		<!-- 다음페이지 이동 -->
		<a th:if="${page != chong}" th:href="@{list(page=${page+1})}"> 다음 </a>
		<span th:if="${page == chong}"> 다음 </span>
		
		<!-- 다음 그룹 -->
		<a th:if="${chong != pend}" th:href="@{list(page=${pend+1})}"> 다음 그룹 </a>
		<span th:if="${chong == pend}"> 다음 그룹 </span>
		</td>
	</tr>
</table>
profile
국비 코딩

0개의 댓글