简而言之
我想在thymleaf中创建一个分页标签。
详情
我已经在jsp中有一个例子。但是我被困在中间。我不知道如何在thymleaf
中编写这段代码。
我用Google搜索,但结果非常令人困惑。
示例jsp:
<%@tag description="Extended input tag to allow for sophisticated errors" pageEncoding="UTF-8" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@tag import="net.megalytics.util.Href" %>
<%@attribute name="currentPage" required="true" type="java.lang.Integer" %>
<%@attribute name="totalPages" required="true" type="java.lang.Integer" %>
<%@attribute name="totalItems" required="true" type="java.lang.Long" %>
<%
if (totalPages > 1) {
String currentUrl = request.getAttribute("javax.servlet.forward.request_uri").toString();
String queryString = "";
if (request.getQueryString() != null)
queryString = request.getQueryString();
Href newUrl = new Href(currentUrl + "?" + queryString);
newUrl.addParameter("page", String.valueOf(currentPage));
String url = "";
Integer totCount =0;
%>
<div class="pull-right">
<ul class="pagination">
<c:choose>
<c:when test="<%=currentPage == 0%>">
<li class="disabled"><a href="#">First</a></li>
</c:when>
<c:otherwise>
<li class="">
<%
newUrl.removeParameter("page");
newUrl.addParameter("page", "0");
url = newUrl.toString();
%>
<a href="<%=url%>">First</a>
</li>
</c:otherwise>
</c:choose>
<c:forEach var="count" step="1" begin="1" end="<%= totalPages %>">
<c:choose>
<c:when test="${(currentPage == count-1)}">
<li class="disabled"><a href="#">${count}</a></li>
</c:when>
<c:otherwise>
<li>
<%
newUrl.removeParameter("page");
newUrl.addParameter("page", String.valueOf(totCount));
url = newUrl.toString();
%>
<a href="<%=url%>">${count}</a>
</li>
</c:otherwise>
</c:choose>
<%
totCount++;
%>
</c:forEach>
<c:choose>
<c:when test="<%=currentPage == (totalPages-1) %>">
<li class="disabled"><a href="#">Last</a></li>
</c:when>
<c:otherwise>
<li class="">
<%
newUrl.removeParameter("page");
newUrl.addParameter("page", String.valueOf(totalPages - 1));
url = newUrl.toString();
%>
<a href="<%=url%>">Last</a>
</li>
</c:otherwise>
</c:choose>
</ul>
</div>
<%
}
%>
任何人都可以帮助我吗?我被卡住了......
答案 0 :(得分:4)
Thymeleaf或任何框架都不鼓励您在视图中编写逻辑。这是糟糕的编码实践。
您可以改为执行以下操作。
使用以下逻辑创建bean方法
@Bean("urlUtil")
class UrlUtil {
String doSomthing() {
newUrl.removeParameter("page");
newUrl.addParameter("page", "0");
return newUrl.toString();
}
}
访问百万富翁布局中的bean
<a th:href="@{__${@urlUtil.doSomthing()}__}">First</a>
参考http://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html#the-springstandard-dialect