java - 在 Thymeleaf 中将 <ENUM> 设置为带分隔符的字符串

标签 java spring spring-mvc thymeleaf

我有一个名为“角色”的类,其类型为 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/

相关文章:

java - Android 未处理的异常错误

java - 胶水应该放在哪里?

java - 将 spring 从 3.1.1 升级到 4.3.3 后出现 Spring UnsatisfiedDependencyException

java - 使用 Spring Tools Suite 打开 Eclipse 时出错

java - Spring MVC : disable DefaultAnnotationHandlerMapping

spring - 使用Spring的ServiceLocatorFactoryBean

java - 用 Hashmap<String, List<String>> 比较两个字符串

java - 两个线程一个同步方法scjp

java - 前端 (HTML) 未从 Microsoft Azure 中的 mysql 数据库获取 Spring Boot 应用程序中的数据

java - Spring MVC 处理程序拦截器不运行