Thymeleaf iteration status variable is null

shashaank srivastava

This is my data class survey nothing unusual here I even have eager loading to prevent issues

package com.based.basedsurvey.data;

import jakarta.persistence.*;
import lombok.*;

import java.util.ArrayList;
import java.util.List;

@Entity
@Data
@NoArgsConstructor
public class Survey {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    @NonNull
    private String name;
    private boolean open;

    @ToString.Exclude
    @EqualsAndHashCode.Exclude
    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
    private List<Question> questions = new ArrayList<>();

    public Survey(String name){
        this.name = name;
        open = false;
    }
}

in the controller this is also very standard just returning all of the items

    @GetMapping(path = "/")
    public String homePage(Model model) {
        model.addAttribute("surveys", surveyRepository.findAll(PageRequest.of(0,10)));
        return "index";
    }

repository class

package com.based.basedsurvey.repo;

import com.based.basedsurvey.data.Survey;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.CrudRepository;

public interface SurveyRepository extends JpaRepository<Survey, Long> {
    Survey findSurveyById(long id);

}

html, this is a small snippet, the important part is surveyStat.odd and surveyStat.last

 <tbody>
    <tr th:each="survey: ${surveys}" th:class="${surveyStat.odd}? 'odd'">
        <td>
          <div th:attr="hx-reveal=${surveyStat.last ? 'revealed' : null}"></div>
          <p th:text="${survey.isOpen()?'OPEN':'CLOSED'}"/>
        </td>
    </tr>
</tbody>

EL1021E: A problem occurred whilst attempting to access the property 'last': 'Unable to access property 'last' through getter method'

Unable to access property 'last' through getter method

Cannot invoke "java.lang.Integer.intValue()" because "this.size" is null

I'm not sure why when i use surveyStat.odd there is no issue but when I use surveyStat.last I'm given an error and the index page is no longer able to load

There is no issue on compilation but rather when the view index is attempting to load the error EL1021E shown below is thrown.

tobifasc
surveyRepository.findAll(PageRequest.of(0,10))

returns a Page<T> (provided your surveyRepository is a default PagingAndSortingRepository)

This Page neither implements Collection nor Map and it isn't an array. So thymeleaf will compute the size as null. You can check the relevant code here.

What you could do is instead of passing the Page to the model pass a Collection instead:

@GetMapping(path = "/")
public String homePage(Model model) {
    List<Survey> surveys = surveyRepository.findAll(PageRequest.of(0,10)).getContent();
    model.addAttribute("surveys", surveys);
    return "index";
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

TOP Ranking

HotTag

Archive