简体   繁体   English

我如何获得在 spring boot 中具有角色的所有用户的列表?

[英]How can i get the liste of all users with roles in spring boot?

I have 3 tables (User(id_user...) - Role(id_role) - users_roles(id_role,id_user) (manytomany)).我有 3 个表 (User(id_user...) - Role(id_role) - users_roles(id_role,id_user) (manytomany))。 I want to get the list of all User with Roles .我想获取所有具有Roles User的列表。

// Service // 服务

public List<User> AllUsers() {
    return loginRepo.findAll();
}

// Controller // 控制器

@GetMapping("/gestion_utilisateurs")
public String GetAllUsers(Model model) {
    List<User> all_users = loginServ.AllUsers();
    model.addAttribute("all_users", all_users);
    return "administration/identite_patient/gestion_utilisateurs";
}

// View // 看法

<th:block th:each="all_users : ${all_users}">
  <tr>
      <td th:style = "${all_users.enabled == true } ? 'color: green' : 'color: red' " > &diams; </td>
      <td th:text="${all_users.nom}"></td>
      <td th:text="${all_users.prenom}"></td>
      <td th:text="${all_users.username}"></td>
      <td th:text="${all_users.roles.role_name}" ></td>
      <td></td>
  </tr>
<th:block>

I want to show role_name but I don't know how to get it (all_users.roles.role_name doesn't work).我想显示 role_name 但我不知道如何获取它(all_users.roles.role_name 不起作用)。 Please if someone has an idea.请如果有人有想法。

Since you can have multiple roles in a user , you would need to iterate over them jsut like you are doing with all_users to list all users.由于您可以在user拥有多个roles ,因此您需要像使用all_users一样对它们进行迭代以列出所有用户。 So something along the following lines:所以有以下几点:

<th:block th:each="all_users : ${all_users}">
  <tr>
      <td th:style = "${all_users.enabled == true } ? 'color: green' : 'color: red' " > &diams; </td>
      <td th:text="${all_users.nom}"></td>
      <td th:text="${all_users.prenom}"></td>
      <td th:text="${all_users.username}"></td>
      <td th:text="${all_users.roles.role_name}"></td>
      <td th:each="role :${all_users.roles.role_name}">
          <td th:text="${role.role_name}"></td>
      </td>
      <td></td>
  </tr>
<th:block>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM