Reputation: 834
I am trying to perfom an if statement in thymeleaf engine for one of the properties of an objects in a List.
My purpose is if somoLetu.hSecondReading == null I should see a text of No Second Reading,but if not I should see Second Reading. But The output displays Second Reading text in both scenarios either null or not, What Am i doing wrong.
Here is how I do it in thymeleaf in masomo.html file
<div th:each="somoLetu : ${masomoYote}" class="container my-5">
<h1 th:text= "${somoLetu.date}"></h1>
<h1 th:text= "${somoLetu.familiaName}"></h1>
<h1 th:text= "${somoLetu.kanda}"></h1>
<div th:if= "${somoLetu.hSecondReading == null}" ><h1>No Second Reading</h1></div>
<div th:unless= "${somoLetu.hSecondReading == null}" ><h1>Second Reading</h1> <h1 th:text="${somoLetu.hSecondReading}"></h1></div>
</div>
</div>
These are the properties of my object
@Entity
@Table(name = "misale")
public class Misale {
@Id
@Column(name ="date")
private String date;
@Column(name ="h_second_reading")
private String hSecondReading;
private String familiaName;
private String kanda;
.................}
This is my method in Controller class to get all the list
@Autowired
private MisaleRepository misaleRepository;
@GetMapping("/masomo")
public String masomoAngalia(Model model){
model.addAttribute("masomoYote", misaleRepository.findAll() );
return "masomo";
}
Upvotes: 2
Views: 548
Reputation: 38431
What is the value of hSecondReading
? Are you sure it is null
and not an empty string? Those are not the same thing.
You should be aware whether a variable is (or can be) null or not. This can lead to more serious errors in Java, if you don't pay attention to that.
Maybe use Thymeleaf's isEmpty
helper: ${#strings.isEmpty(somoLetu.hSecondReading)}
Upvotes: 2