在 thymeleaf 中编写 Java 代码

Aam*_*amy 0 java jsp thymeleaf

简而言之

我正在尝试在 thymleaf 中创建一个用于分页的标签。


详细

我已经在 jsp 中有一个例子。但我被困在中间。我不知道如何在thymleaf.

我用谷歌搜索了一下,但结果非常令人困惑。

示例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>
 <%
  }
 %>
Run Code Online (Sandbox Code Playgroud)

谁能帮我?我被卡住了...

Far*_*ook 5

Thymeleaf 或任何框架都不鼓励您在视图中编写逻辑。这是糟糕的编码实践。

您可以改为执行以下操作。

使用下面的逻辑创建一个 bean 方法

@Bean("urlUtil")
class UrlUtil {
    String doSomthing() {
       newUrl.removeParameter("page");
       newUrl.addParameter("page", "0");
       return newUrl.toString();
    }
}
Run Code Online (Sandbox Code Playgroud)

访问 thymeleaf 布局内的 bean

<a th:href="@{__${@urlUtil.doSomthing()}__}">First</a>
Run Code Online (Sandbox Code Playgroud)

参考http://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html#the-springstandard-dialect