Developer Cafe

jstl을 thymeleaf로 바꾸는 방법 ( 예제 본문

Spring/Thymeleaf

jstl을 thymeleaf로 바꾸는 방법 ( 예제

개발자 카페 2021. 5. 4. 17:18
728x90

spring에서는 thymeleaf가 권장된다.

https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#introducing-thymeleaf

 

jstl 형식 (바꾸기전)

<div class="pageNumber">
			<%-- 글갯수가 0보다 크면 페이지블록 계산해서 출력하기 --%>
			<c:if test="${ pageDto.count gt 0 }">
				<%-- [이전] --%>
				<c:if test="${ pageDto.startPage gt pageDto.pageBlock }">
					<a href="/fileNotice/list?pageNum=${ pageDto.startPage - pageDto.pageBlock }&category=${ pageDto.category }&search=${ pageDto.search }">이전</a>
				</c:if>

				<%-- 시작페이지 ~ 끝페이지 --%>
				<c:forEach var="i" begin="${ pageDto.startPage }" end="${ pageDto.endPage }" step="1">

					<c:choose>
					<c:when test="${ i eq pageNum }">
						<a href="/fileNotice/list?pageNum=${ i }&category=${ pageDto.category }&search=${ pageDto.search }" class="active">${ i }</a>
					</c:when>
					<c:otherwise>
						<a href="/fileNotice/list?pageNum=${ i }&category=${ pageDto.category }&search=${ pageDto.search }">${ i }</a>
					</c:otherwise>
					</c:choose>

				</c:forEach>

				<%-- [다음] --%>
				<c:if test="${ pageDto.endPage lt pageDto.pageCount }">
					<a href="/fileNotice/list?pageNum=${ pageDto.startPage + pageDto.pageBlock }&category=${ pageDto.category }&search=${ pageDto.search }">다음</a>
				</c:if>
			</c:if>
		</div>

 

thymeleaf 형식 (바꾼후)

<nav>
        <div class="text-center btn-lg">
            <ul th:if="${ pageDto.count gt 0 }" class="pagination">

                <li th:if="${ pageDto.startPage gt pageDto.pageBlock }" class="page-item">
                    <a th:href="'/reviews?pageNum=' + ${ pageDto.startPage - pageDto.pageBlock }">이전</a>
                </li>

                <li th:each="i: ${#numbers.sequence(pageDto.startPage, pageDto.endPage)}" class="page-item">
                    <a th:href="'/reviews?pageNum=' + ${i}" th:classappend="${i eq pageNum} ? active : ''" th:text="${i}"></a>
                </li>

                <li th:if="${ pageDto.endPage lt pageDto.pageCount }" class="page-item">
                    <a th:href="'/reviews?pageNum='+${ pageDto.startPage + pageDto.pageBlock }">다음</a>
                </li>
            </ul>
        </div>
    </nav>

 

결과화면

 

 

간단하게 보기

<c:forEach begin="1" end="12" var="item"></c:forEach>
<th:block th:each="item: ${#numbers.sequence(1, 12)}"></th:block>
728x90
Comments