我有一个名为“角色”的类,其类型为 Set。
@Data
@Table(name = "usr")
@NoArgsConstructor
@Entity
public class User implements UserDetails{
//...
@ElementCollection(targetClass = Role.class, fetch = FetchType.EAGER)
@CollectionTable(name = "user_role", joinColumns = @JoinColumn(name = "user_id"))
@Enumerated(EnumType.STRING)
private Set<Role> roles;
//...
我需要将字段的所有元素连接成一个字符串,并在 Thymeleaf 的 th:text 属性中用逗号分隔。我怎样才能做到这一点?
最佳答案
您可以使用#strings.setJoin(set,separator)
:
<div th:text="${#strings.setJoin(user.roles, ',')}"></div>
参见https://doanduyhai.wordpress.com/2012/04/14/spring-mvc-part-iv-thymeleaf-advanced-usage/了解更多信息(“帮助对象的精彩之处”一章)
关于java - 在 Thymeleaf 中将 <ENUM> 设置为带分隔符的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56863747/