Spring boot Thymeleaf:在模板中打印一组

Spring boot Thymeleaf:在模板中打印一组,spring-boot,thymeleaf,Spring Boot,Thymeleaf,我有一个基本的SpringBoot 2.0.4.0版本应用程序。使用Spring初始值设定项、JPA、嵌入式Tomcat、Thymeleaf模板引擎,并将包作为可执行JAR文件 我有这个实体: @Entity @Table(name="t_user") public class User implements Serializable, UserDetails { @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, fetch =

我有一个基本的SpringBoot 2.0.4.0版本应用程序。使用Spring初始值设定项、JPA、嵌入式Tomcat、Thymeleaf模板引擎,并将包作为可执行JAR文件

我有这个实体:

@Entity
@Table(name="t_user")
public class User implements Serializable, UserDetails {

 @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JsonIgnore
    private Set<UserRole> userRoles = new HashSet<>();

public Set<UserRole> getUserRoles() {
        return userRoles;
    }

    public void setUserRoles(Set<UserRole> userRoles) {
        this.userRoles = userRoles;
    }
}
并希望打印用户的所有角色,因此我使用以下代码:

 <td class="col_name"   th:text="${user.userRoles.role}"></td><!-- ROLES -->

您需要遍历对象集以打印
角色
值。您可以使用Thymeleaf的
th:each
语法来执行此操作:

<td class="col_name">
    <span th:each="userRole : ${user.userRoles}" th:text="${userRole.role}">[role]</span><!-- format with line breaks as needed -->
</td>
您可以选择使用
th:remove=“tag”
删除
标记

com.tdk.backend.persistence.domain.backend.User.userRoles
<td class="col_name">
    <span th:each="userRole : ${user.userRoles}" th:text="${userRole.role}">[role]</span><!-- format with line breaks as needed -->
</td>
<td class="col_name">
    <span th:text="${#lists.toString(user.userRole.role)}">[role]</span>
</td>