【问题标题】:spring boot and thymeleaf弹簧靴和百里香叶
【发布时间】:2021-12-11 00:55:48
【问题描述】:

我需要有关 spring enter image description hereboot 和 thymeleaf 的帮助,因为它没有向我显示用户角色

因为它向我显示了这些信息而不是角色

这是我的数据库

enter image description here

我的控制器

@GetMapping("/listar")
public String listar(Model model) {
        
        List<User> listUsers= userService.findAll();        
        
    if(listUsers !=null) {  
    model.addAttribute("titulo", "Lista de usuarios y roles");
    model.addAttribute("listUsers", listUsers);
    return "user/listar";
    }
        
    return "redirect:/escuela/";
        
    }

我的 HTML

<tr th:each="user:  ${listUsers}">
    <td th:text="${user.nombre}"></td>
    <td th:text="${user.aPaterno}"></td>
    <td th:text="${user.aMaterno}"></td>
    <td th:text="${user.curp}">]</td>
    <td th:text="${user.puesto}"></td>
    <td th:text="${user.email}"></td>                           
    <td th:text="${user.roles}"></td>
</tr>

我的实体

public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Size(min = 2, max = 20)
    @NotBlank
    private String nombre;

    @Size(min = 2, max = 20)
    @NotBlank
    private String aPaterno;

    @Size(min = 2, max = 20)
    @NotBlank
    private String aMaterno;

    @Size(min = 18, max = 18)   
    @Column(length = 18, nullable = false, unique = true)
    private String curp;

    @Size(min = 2, max = 20)
    @NotBlank
    private String puesto;

    

    @Email
        @Column(length = 45, nullable = false, unique = true)
        private String email;
    
        @Size(min = 2, max = 20)
        @Column(length = 20, nullable = false)
        @NotBlank
        private String password;

@Valid
    @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinTable(name = "user_role", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
    private Set<Role> roles = new HashSet<Role>();

getter 和 setter.....

我没有太多的spring boot经验,谁能帮帮我

【问题讨论】:

    标签: spring spring-boot thymeleaf


    【解决方案1】:

    你得到这个是因为${user.roles}是一个列表,所以显示的是这个列表(以及Role的)默认toString()方法,

    您需要循环输入您的 user.roles 并打印,例如,如下所示的角色名称:

    <tr th:each="user:  ${listUsers}">
        <td th:text="${user.nombre}"></td>
        <td th:text="${user.aPaterno}"></td>
        <td th:text="${user.aMaterno}"></td>
        <td th:text="${user.curp}">]</td>
        <td th:text="${user.puesto}"></td>
        <td th:text="${user.email}"></td>  
        <td>
            <table>
                <tr th:each="role: ${user.roles}">
                     <td th:text="${role.name}"></td>
                </tr>
            </table>
        </td>                         
    </tr>
    

    为了同谋,您还可以在 Role 类中覆盖 toString() 方法并让您的 HTML 保持原样,即

    <td th:text="${user.roles}"></td>
    

    由于您尚未在下面发布 Role 类,因此可以“猜测”它可能是什么,并重写 toString() 方法以显示 name 字段。

    角色

    @Entity
    public class Role {
    
        private String name;
        // rest of properties
    
        // getters and setters
    
        @Override
        public String toString() {
            return this.name;
        }
    }
    

    【讨论】:

    • pleft,太好了,对我理解有很大帮助,谢谢
    猜你喜欢
    • 2018-04-02
    • 1970-01-01
    • 1970-01-01
    • 2016-06-01
    • 2018-11-20
    • 2018-11-24
    • 2018-04-03
    • 1970-01-01
    相关资源
    最近更新 更多