【问题标题】:Displaying inner objects in thymeleaf在百里香中显示内部对象
【发布时间】:2018-05-26 14:21:59
【问题描述】:

我正在尝试在我的应用程序中显示我的 user_roles。

这是我的豆子

private Set<ClientRole> clientRoles = new HashSet<>();

这是我试图在 Thymeleaf 中显示的 bean 中的对象。

<tr th:each="clients : ${clientsList}">
                                    <td th:text="${clients.client_id}">...</td>
                                    <td th:text="${clients.emailaddress}">...</td>
                                    <td th:text="${clients.firstname}">...</td>
                                    <td th:text="${clients.lastname}">...</td>
                                    <td th:text="${clients.phone}">...</td>
                                    <td th:text="${clients.companyname}">...</td>
                                    <td th:text="${clients.companyurl}">...</td>
                                    <td th:text="${clients.clientRoles?.role}">...</td>
                                </tr>

客户角色实体

@Entity
@Table(name="user_role")
public class ClientRole {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long userRoleId;

public ClientRole(Client client, Role role) {
    this.client = client;
    this.role = role;
}


@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "client_id")
private Client client;


@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "role_id")
private Role role;

public ClientRole() {}

public long getUserRoleId() {
    return userRoleId;
}

public void setUserRoleId(long userRoleId) {
    this.userRoleId = userRoleId;
}

public Client getClient() {
    return client;
}

public void setClient(Client client) {
    this.client = client;
}

public Role getRole() {
    return role;
}

public void setRole(Role role) {
    this.role = role;
}

}

这里是角色实体。 Bean试图访问角色 百里香叶。因为我正在使用百里香附加弹簧安全 4。我会 像管理员用户一样能够修改角色。我希望能够 显示和修改。其他字段显示正常,但 clientRolse 给了我一个很长的字符串。 com.zenopoint.domain.Client.clientRoles

@Entity
public class Role {
@Id
private int roleId;
private String name;

@OneToMany(mappedBy = "role", cascade = CascadeType.ALL, fetch = 
 FetchType.LAZY)
private Set<ClientRole> clientRoles = new HashSet<>();

public Role() {

}

public int getRoleId() {
    return roleId;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Set<ClientRole> getClientRoles() {
    return clientRoles;
}

public void setClientRoles(Set<ClientRole> clientRoles) {
    this.clientRoles = clientRoles;
}

public void setRoleId(int roleId) {
    this.roleId = roleId;
}

}

这是我的控制器

@RequestMapping("/accounts")
 public String accounts(Model model) {
    List<Client> clientsList = userservice.findUserList();
    model.addAttribute("clientsList", clientsList);
    return "app/accounts";
}

这是我的用户服务

 public List<Client> findUserList() {
    return clientDao.findAll();
}


public interface ClientDao extends CrudRepository<Client , Long> {
    Client findByPhone(String phone);
    Client findByEmailaddress(String emailaddress);
    Client findByUsername(String username);
    List<Client> findAll();
}



@Entity
public class  Client  implements  UserDetails{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long client_id;
    @Column(unique = true, nullable = false)
    @NotEmpty
    private String username;
    @NotEmpty
    private String password;
    @Email
    @Column(nullable = false, unique = true)
    private String emailaddress;  
    @NotEmpty
    private String companyname;
    @Column(nullable = false)
    private String companyurl;
    @NotEmpty
    private String street;
    @NotEmpty
    private String city;
    @NotEmpty
    private String region;
    private String zip;
    @Temporal(TemporalType.DATE)
    private Date dob;
    @Temporal(TemporalType.TIMESTAMP)
    private Date doj = new Date();
    @NotEmpty
    private String firstname;
    @NotEmpty
    private String lastname;
    private String nationality;
    @Column(unique = true, nullable = false)
    @NotEmpty
    private String phone;
    @NotEmpty
    private String position;    
    private boolean enabled = true;
    private String registrationnumber;
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "client", cascade = CascadeType.ALL)
    private List<Transactions> transactions;
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "client", cascade = CascadeType.ALL)
    private List<Tokenization> tokenization;
    @OneToMany(mappedBy = "client", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JsonIgnore
    private Set<ClientRole> clientRoles = new HashSet<>();

    public Client() {}

    public Long getClient_id() {
        return client_id;
    }


    public void setClient_id(Long client_id) {
        this.client_id = client_id;
    }


    public String getUsername() {
        return username;
    }


    public void setUsername(String username) {
        this.username = username;
    }


    public String getPassword() {
        return password;
    }


    public void setPassword(String password) {
        this.password = password;
    }


    public String getEmailaddress() {
        return emailaddress;
    }


    public void setEmailaddress(String emailaddress) {
        this.emailaddress = emailaddress;
    }


    public String getCompanyname() {
        return companyname;
    }


    public void setCompanyname(String companyname) {
        this.companyname = companyname;
    }


    public String getCompanyurl() {
        return companyurl;
    }


    public void setCompanyurl(String companyurl) {
        this.companyurl = companyurl;
    }


    public String getStreet() {
        return street;
    }


    public void setStreet(String street) {
        this.street = street;
    }


    public String getCity() {
        return city;
    }


    public void setCity(String city) {
        this.city = city;
    }


    public String getRegion() {
        return region;
    }


    public void setRegion(String region) {
        this.region = region;
    }


    public String getZip() {
        return zip;
    }


    public void setZip(String zip) {
        this.zip = zip;
    }


    public Date getDob() {
        return dob;
    }


    public void setDob(Date dob) {
        this.dob = dob;
    }


    public Date getDoj() {
        return doj;
    }

    public void setDoj(Date doj) {
        this.doj = doj;
    }

    public String getFirstname() {
        return firstname;
    }


    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }


    public String getLastname() {
        return lastname;
    }


    public void setLastname(String lastname) {
        this.lastname = lastname;
    }


    public String getNationality() {
        return nationality;
    }


    public void setNationality(String nationality) {
        this.nationality = nationality;
    }


    public String getPhone() {
        return phone;
    }


    public void setPhone(String phone) {
        this.phone = phone;
    }


    public String getPosition() {
        return position;
    }


    public void setPosition(String position) {
        this.position = position;
    }


    public boolean isEnabled() {
        return enabled;
    }


    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }


    public String getRegistrationnumber() {
        return registrationnumber;
    }


    public void setRegistrationnumber(String registrationnumber) {
        this.registrationnumber = registrationnumber;
    }


    public List<Transactions> getTransactions() {
        return transactions;
    }


    public void setTransactions(List<Transactions> transactions) {
        this.transactions = transactions;
    }


    public List<Tokenization> getTokenization() {
        return tokenization;
    }


    public void setTokenization(List<Tokenization> tokenization) {
        this.tokenization = tokenization;
    }


    public Set<ClientRole> getClientRoles() {
        return clientRoles;
    }


    public void setClientRoles(Set<ClientRole> clientRoles) {
        this.clientRoles = clientRoles;
    }

    @Override
    public String toString() {
        return "Client [client_id=" + client_id + ", username=" + username + ", password=" + password
                + ", emailaddress=" + emailaddress + ", companyname=" + companyname + ", companyurl=" + companyurl
                + ", street=" + street + ", city=" + city + ", region=" + region + ", zip=" + zip + ", dob=" + dob
                + ", doj=" + doj + ", firstname=" + firstname + ", lastname=" + lastname + ", nationality="
                + nationality + ", phone=" + phone + ", position=" + position + ", enabled=" + enabled
                + ", registrationnumber=" + registrationnumber + ", transactions=" + transactions + ", tokenization="
                + tokenization + ", clientRoles=" + clientRoles + "]";
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        Set<GrantedAuthority> authorities = new HashSet<>();
        clientRoles.forEach(ur -> authorities.add(new Authority(ur.getRole().getName())));
        return authorities;
    }

    @Override
    public boolean isAccountNonExpired() {
        // TODO Auto-generated method stub
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        // TODO Auto-generated method stub
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        // TODO Auto-generated method stub
        return true;
    }

}

【问题讨论】:

  • 所有对象都正确显示,但对于 clientRole

标签: spring-mvc spring-boot spring-data-jpa thymeleaf


【解决方案1】:

这是嵌套循环:

<td data-th-text="${clients.clientRoles}">...</td>
<td>
    <tr th:each="a: ${clients.clientRoles}">
        <td data-th-text="${a.role.name}">...</td>
    </tr>
</td>

【讨论】:

  • 完美运行
【解决方案2】:

您的代码的问题是您使用这行代码获取了一个对象Role
&lt;td th:text="${clients.clientRoles?.role}"&gt;...&lt;/td&gt;

您所要做的就是深入一层以获得Role 名称

像这样更新你的代码

<td th:text="${clients.clientRoles?.role.name}">...</td>

更新 1

由于您的clientRoles 在您的Client 实体中映射为@OneToMany 实体,因此您的client 对象将具有clientRoles 的列表。

&lt;td th:text="${clients.clientRoles?.role}"&gt;...&lt;/td&gt; 这行代码不正确,因为clients.clientRoles 实际上是一个角色列表。

您需要迭代 clientRoles 以获取客户端拥有的所有 roles

【讨论】:

  • EL1008E:在“java.lang.String”类型的对象上找不到属性或字段“名称” - 可能不是公共的?
  • 请同时使用Client 实体更新您的帖子。我认为映射中似乎存在一些差异。
  • 问题也可能是因为clientRoles 将是roles 的列表。
  • 添加了客户端实体@Abdullah Khan
【解决方案3】:

您的 Thymeleaf 代码看起来正确。您是否将对象添加到您的 ClientRole 实体集?您是否使用 model.addAttribute("clientsList", clientRoles); 将其添加到 Spring 控制器中的模型中?

如果没有,请向我们提供您的 Controller 类和 ClientRole 实体的代码。

【讨论】:

  • 所以要显示角色名称和角色 ID,您必须在 thymeleaf ${clients.role.name}${clients.role.roleId} 中使用以下内容
  • 请提供您使用 model.addAttribute(...,..) 的控制器代码。目前我不知道您是否将角色对象列表或 ClientRole 对象添加到您的视图中
  • EL1008E:在“com.zenopoint.domain.Client”类型的对象上找不到属性或字段“角色” - 可能不公开?
  • 我以为您将 ClientRole 对象列表传递给您的视图?请给我看你的控制器
  • 我确实得到了每个客户的角色列表,但它是这样显示的。 [com.zenopoint.entities.security.ClientRole@7bcddd01]。我该如何转换?具有 2 个或更多角色的客户获得其中的 2 或 3 个,用逗号分隔
猜你喜欢
  • 1970-01-01
  • 2019-08-10
  • 2018-04-25
  • 1970-01-01
  • 2018-11-27
  • 1970-01-01
  • 2021-01-25
  • 2018-10-12
  • 2017-08-02
相关资源
最近更新 更多