Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring, Thymleaf and lists of strings

Okay, I a wet-behind-the-ears newcomer to Spring and Thymleaf. I'm trying to do something so simple it should be a no-brainer. But I can't get it to work. The simple question is - how do you show a list of strings in an web page?

I have the following model

import java.util.List;
public class TestModel {

    private List<String> list = null;
    private String content;

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public List<String> getList() { return list; }

    public void setList(final List<String> list) {
        this.list = list;
    }
}

My web page contains the following:

<div th:if="${greeting.list != null}">
    <h1>Result</h1>
    <ul>
    <th:block th:object="${greeting}" th:each="item : ${list}">
     <li th:text="${item.name}">Item description here...</li>
    </th:block>
    </ul>
</div>

I added the ".name" to "item" only because I found a couple of examples where they had a list of strings and did something similar. But they had the ".name" on the object.

But it still doesn't work. The unordered list ends up empty. I.e. There isn't any list items inside the unordered tags.

What am I doing wrong? Pointers gladly accepted.

like image 789
Lori Cook Avatar asked Sep 25 '14 23:09

Lori Cook


People also ask

How do you compare strings in Thymeleaf?

You can even compare String objects in thymeleaf. Under the hood, Thymeleaf uses the String. compareTo() method from the Comparable interface. That is, You can compare any two objects of the same type if they are Comparable .

How do I get a list index in Thymeleaf?

In Thymeleaf, we use th:each attribute for iteration. Thymeleaf th:each allows you to declare an iteration status variable. The employeeStat is the aggregation of the variable employee with the suffix Stat.


1 Answers

Since there's no example of filling model I supposed you put some strings into instance of TestModel's list field like this.

TestModel greeting= new TestModel();
List<String> list = new ArrayList<>();
list.add("A");
list.add("B");
model.addAttribute("greeting", greeting);

Then there are more errors in your Thymeleaf template example.

  1. If you are using object selection through th:object you must at first place use asterix * to access object properties. Asterisk syntax evaluates expressions on selected objects instead of context variables map.
  2. Object selection affects only children nodes in DOM.
  3. In your example you want iterate over list of strings (List<String>) but you want to access property name which in fact don't exists on Java String object.

You must fix your Thymeleaf template in one way - see examples.

No object selection at all

<div th:if="${greeting.list != null}">
    <h1>Result</h1>
    <ul>
       <li th:each="item : ${greeting.list}" th:text="${item}">Item description here...</li>
    </ul>
</div>

Proper object selection

<div th:if="${greeting.list != null}">
    <h1>Result</h1>
    <ul>
    <th:block th:object="${greeting}">
       <li th:each="item : *{list}" th:text="${item}">Item description here...</li>
    </th:block>
    </ul>
</div>
like image 170
michal.kreuzman Avatar answered Oct 30 '22 11:10

michal.kreuzman