spring boot and thymeleaf

Zeus

I need help with spring enter image description hereboot and thymeleaf because it doesn't show me the user roles

because it shows me this information and not the roles

this is my database

enter image description here

my controller

@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/";
        
    }

My 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>

my entity

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>();

gettters and setters.....

I don't have much spring boot experience, can someone help me

pleft

You are getting this because the ${user.roles} is a list, so what's displayed is this list's (and consequently the Role's) default toString() method,

You need to loop in your user.roles and print, for example, the role's name like below:

<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>

For the sake of complicity you could also override the toString() method in your Role class and let your HTML as is, i.e.

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

Since you haven't posted Role class below is a "guess" of what it might be and the toString() method is overridden to display the name field.

Role class

@Entity
public class Role {

    private String name;
    // rest of properties

    // getters and setters

    @Override
    public String toString() {
        return this.name;
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related