jsp - Writing java code in thymeleaf -


in short

i tring create tag pagination in thymleaf.


in detail

i have example in jsp.but stuck in middle.i not sure how write code in thymleaf.

i googled it,but results confusing.

example jsp :

<%@tag description="extended input tag allow 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>  <%   }  %> 

can me? stuck...

thymeleaf or neither framework doesn't encourage write logic inside views. poor coding practice.

you can following instead.

create bean method logic below

@bean("urlutil") class urlutil {     string dosomthing() {        newurl.removeparameter("page");        newurl.addparameter("page", "0");        return newurl.tostring();     } } 

access bean inside thymeleaf layout

<a th:href="@{__${@urlutil.dosomthing()}__}">first</a> 

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


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -