0

I'm trying to figure it out with pagination. I wrote everything as in the tutorial, but when it starts it gives an error. Why it appeared, I don't understand. My construction logic is correct. Tell me how to get rid of this error.

ERROR 3256 --- [nio-8080-exec-1] org.thymeleaf.TemplateEngine             : [THYMELEAF][http-nio-8080-exec-1] Exception processing template "/blog": Exception evaluating SpringEL expression: "pagePost.totalPage > 0" (template: "/blog" - line 90, col 37)

org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "pagePost.totalPage > 0" (template: "/blog" - line 90, col 37)
at org.thymeleaf.spring5.expression.SPELVariableExpressionEvaluator.evaluate(SPELVariableExpressionEvaluator.java:292) ~[thymeleaf-spring5-3.0.12.RELEASE.jar:3.0.12.RELEASE]
at org.thymeleaf.standard.expression.VariableExpression.executeVariableExpression(VariableExpression.java:166) ~[thymeleaf-3.0.12.RELEASE.jar:3.0.12.RELEASE]
at org.thymeleaf.standard.expression.SimpleExpression.executeSimple(SimpleExpression.java:66) ~[thymeleaf-3.0.12.RELEASE.jar:3.0.12.RELEASE]
at org.thymeleaf.standard.expression.Expression.execute(Expression.java:109) ~[thymeleaf-3.0.12.RELEASE.jar:3.0.12.RELEASE]
at org.thymeleaf.standard.expression.Expression.execute(Expression.java:138) ~[thymeleaf-3.0.12.RELEASE.jar:3.0.12.RELEASE]
at org.thymeleaf.standard.expression.Expression.execute(Expression.java:125) ~[thymeleaf-3.0.12.RELEASE.jar:3.0.12.RELEASE]
at org.thymeleaf.standard.processor.StandardIfTagProcessor.isVisible(StandardIfTagProcessor.java:59) ~[thymeleaf-3.0.12.RELEASE.jar:3.0.12.RELEASE]
at org.thymeleaf.standard.processor.AbstractStandardConditionalVisibilityTagProcessor.doProcess(AbstractStandardConditionalVisibilityTagProcessor.java:61) ~[thymeleaf-3.0.12.RELEASE.jar:3.0.12.RELEASE]
at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:74) ~[thymeleaf-3.0.12.RELEASE.jar:3.0.12.RELEASE]
at org.thymeleaf.processor.element.AbstractElementTagProcessor.process(AbstractElementTagProcessor.java:95) ~[thymeleaf-3.0.12.RELEASE.jar:3.0.12.RELEASE]
at org.thymeleaf.util.ProcessorConfigurationUtils$ElementTagProcessorWrapper.process(ProcessorConfigurationUtils.java:633) ~[thymeleaf-3.0.12.RELEASE.jar:3.0.12.RELEASE]
at org.thymeleaf.engine.ProcessorTemplateHandler.handleOpenElement(ProcessorTemplateHandler.java:1314) ~[thymeleaf-3.0.12.RELEASE.jar:3.0.12.RELEASE]

2021-05-21 10:53:55.505 ERROR 3256 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : 
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request 
processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: 
Exception evaluating SpringEL expression: "pagePost.totalPage > 0" (template: "/blog" - line 90, col 
37)] with root cause

org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'totalPage' cannot be found on null
at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:213) ~[spring-expression-5.3.6.jar:5.3.6]
at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:104) ~[spring-expression-5.3.6.jar:5.3.6]
at org.springframework.expression.spel.ast.PropertyOrFieldReference.access$000(PropertyOrFieldReference.java:51) ~[spring-expression-5.3.6.jar:5.3.6]
at org.springframework.expression.spel.ast.PropertyOrFieldReference$AccessorLValue.getValue(PropertyOrFieldReference.java:406) ~[spring-expression-5.3.6.jar:5.3.6]
at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:92) ~[spring-expression-5.3.6.jar:5.3.6]
at org.springframework.expression.spel.ast.OpGT.getValueInternal(OpGT.java:47) ~[spring-expression-5.3.6.jar:5.3.6]
at org.springframework.expression.spel.ast.OpGT.getValueInternal(OpGT.java:37) ~[spring-expression-5.3.6.jar:5.3.6]
at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:112) ~[spring-expression-5.3.6.jar:5.3.6]
at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:337) ~[spring-expression-5.3.6.jar:5.3.6]
at org.thymeleaf.spring5.expression.SPELVariableExpressionEvaluator.evaluate(SPELVariableExpressionEvaluator.java:265) ~[thymeleaf-spring5-3.0.12.RELEASE.jar:3.0.12.RELEASE]
at org.thymeleaf.standard.expression.VariableExpression.executeVariableExpression(VariableExpression.java:166) ~[thymeleaf-3.0.12.RELEASE.jar:3.0.12.RELEASE]
at org.thymeleaf.standard.expression.SimpleExpression.executeSimple(SimpleExpression.java:66) ~[thymeleaf-3.0.12.RELEASE.jar:3.0.12.RELEASE]
at org.thymeleaf.standard.expression.Expression.execute(Expression.java:109) ~[thymeleaf-3.0.12.RELEASE.jar:3.0.12.RELEASE]
at org.thymeleaf.standard.expression.Expression.execute(Expression.java:138) ~[thymeleaf-3.0.12.RELEASE.jar:3.0.12.RELEASE]
at org.thymeleaf.standard.expression.Expression.execute(Expression.java:125) ~[thymeleaf-3.0.12.RELEASE.jar:3.0.12.RELEASE]
at org.thymeleaf.standard.processor.StandardIfTagProcessor.isVisible(StandardIfTagProcessor.java:59) ~[thymeleaf-3.0.12.RELEASE.jar:3.0.12.RELEASE]
at org.thymeleaf.standard.processor.AbstractStandardConditionalVisibilityTagProcessor.doProcess(AbstractStandardConditionalVisibilityTagProcessor.java:61) ~[thymeleaf-3.0.12.RELEASE.jar:3.0.12.RELEASE]
at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:74) ~[thymeleaf-3.0.12.RELEASE.jar:3.0.12.RELEASE]
at org.thymeleaf.processor.element.AbstractElementTagProcessor.process(AbstractElementTagProcessor.java:95) ~[thymeleaf-3.0.12.RELEASE.jar:3.0.12.RELEASE]
at org.thymeleaf.util.ProcessorConfigurationUtils$ElementTagProcessorWrapper.process(ProcessorConfigurationUtils.java:633) ~[thymeleaf-3.0.12.RELEASE.jar:3.0.12.RELEASE]
at org.thymeleaf.engine.ProcessorTemplateHandler.handleOpenElement(ProcessorTemplateHandler.java:1314) ~[thymeleaf-3.0.12.RELEASE.jar:3.0.12.RELEASE]
at org.thymeleaf.engine.OpenElementTag.beHandled(OpenElementTag.java:205) ~[thymeleaf-3.0.12.RELEASE.jar:3.0.12.RELEASE]
at org.thymeleaf.engine.Model.process(Model.java:282) ~[thymeleaf-3.0.12.RELEASE.jar:3.0.12.RELEASE]
at org.thymeleaf.engine.Model.process(Model.java:290) ~[thymeleaf-3.0.12.RELEASE.jar:3.0.12.RELEASE]
at org.thymeleaf.engine.IteratedGatheringModelProcessable.processIterationModel(IteratedGatheringModelProcessable.java:367) ~[thymeleaf-3.0.12.RELEASE.jar:3.0.12.RELEASE]
at org.thymeleaf.engine.IteratedGatheringModelProcessable.process(IteratedGatheringModelProcessable.java:221) ~[thymeleaf-3.0.12.RELEASE.jar:3.0.12.RELEASE]
at org.thymeleaf.engine.ProcessorTemplateHandler.handleCloseElement(ProcessorTemplateHandler.java:1640) ~[thymeleaf-3.0.12.RELEASE.jar:3.0.12.RELEASE]
at org.thymeleaf.engine.CloseElementTag.beHandled(CloseElementTag.java:139) ~[thymeleaf-3.0.12.RELEASE.jar:3.0.12.RELEASE]
at org.thymeleaf.engine.TemplateModel.process(TemplateModel.java:136) ~[thymeleaf-3.0.12.RELEASE.jar:3.0.12.RELEASE]
at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:661) ~[thymeleaf-3.0.12.RELEASE.jar:3.0.12.RELEASE]
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098) ~[thymeleaf-3.0.12.RELEASE.jar:3.0.12.RELEASE]

Here is my implementation

public interface PostRepository extends JpaRepository<Post, Long> {
Iterable<Post> findByTitleContainingIgnoreCase (String title);

@Override
Page<Post> findAll(Pageable pageable);
}

Controller

@Controller
public class BlogController {

@Autowired
PostRepository postRepository;

@GetMapping("/pagination")
public String listPerson(Model model,@RequestParam(value = "page", required = false, defaultValue = "0") Integer page){

    Page<Post> pagePosts = postRepository.findAll(PageRequest.of(page,3));
    model.addAttribute("pagePost", pagePosts);
    model.addAttribute("numbers", IntStream.range(0, pagePosts.getTotalPages()).toArray());

    return "/blog";
}

Blog.html

    <div th:each="el : ${allArticles} " class="blog_post">
                        <div class="blog_post_image">
                            <a th:href=" '/blog/' + ${el.id}"><img src="images/blog_6.jpg" alt=""></a>
                          <!--  <a href="#"><img th:src="*{'/image/' + post.id}" alt="img"></a> -->

                            <div class="blog_post_date">
                                <span th:data="${timeArticle}"  class="d-flex flex-column align-items-center justify-content-center">
                                    <span th:text="${timeArticle}"></span>

                                </span>
                                </div>
                            </div>
                            <div class="blog_post_content">
                                <div sec:authorize=" hasRole('ROLE_ADMIN') " class="blog_post_title">
                                    <h3>
                                        <a th:href=" '/blog/' + ${el.id}" th:text="${el.title}"></a>
                                    </h3>
                                    <div class="admin_tool">
                                        <a th:href=" '/blog/' + ${el.id} +'/edit'  " class="editLink" >
                                            <i class="fa fa-pencil" aria-hidden="true" title="Редактировать пост"></i>
                                        </a>
                                        <form method="post" th:action=" '/blog/' + ${el.id} +'/remove'  "  class="formDelete">
                                            <button class="btn btn-warning btnDelete" type="submit">
                                                <i class="fa fa-trash-o" aria-hidden="true" title="Удалить пост"></i>
                                            </button>
                                        </form>
                                    </div>

                                </div>
                                <div class="blog_post_info d-flex flex-row align-items-center justify-content-start">
                                    <div class="author_image"><img src="images/blog_post_author.jpg" alt=""></div>
                                    <div class="author_name">
                                        <span>Автор: </span>
                                        <span th:text="${el.author}"></span>
                                    </div>
                                </div>
                                <div class="blog_data">
                                    <span th:text="${el.timeArticle}"></span>
                                </div>
                                <div class="blog_post_text">
                                    <p th:text="${el.anons}"></p>
                                </div>
                                <div class="button blog_post_button"><a th:href=" '/blog/' + ${el.id}" >Подробнее</a></div>
                            </div>

                            <div class="page_nav">
                                <ul th:if="${pagePost.totalPage > 0}" class="d-flex flex-row align-items-center justify-content-start flex-wrap">

                                    <li th:each="pageNumber : ${numbers}" th:class="${pageNumber == pagePost.number} ? active "  >
                                        <a th:href="@{'/pagination?page=' + ${pageNumber} }" th:text="${pageNumber +1}"></a>
                                    </li>

                                </ul>
                            </div>
                    </div>

Tell me how to solve this problem. I really don't understand what I'm doing wrong.

  • Try logging the result of the findAll first off, cause it seems like pagePost attribute is nul – L_Cleo May 21 '21 at 08:39
  • What part of 'Property or field 'totalPage' cannot be found on null' didn't you understand? – user207421 May 21 '21 at 09:55
  • 2
    That `findAll` method should normally never return `null`. Can you try to remove the method from your `PostRepository` as `JpaRepository` already has this method (via the `PagingAndSortingRepository` super interface? – Wim Deblauwe May 21 '21 at 11:14

0 Answers0