我正在尝试在我的应用程序中显示我的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尝试访问角色 thymeleaf。因为我正在使用百日咳的额外弹簧安全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";
}
这是我的userService
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;
}
}
答案 0 :(得分: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>
答案 1 :(得分:0)
你的Thymeleaf代码看起来是正确的。您是否将对象添加到您的ClientRole实体集中?你是否在model.addAttribute("clientsList", clientRoles);
的Spring Controller中将它添加到模型中?
如果没有,请向我们提供Controller类和ClientRole实体的代码。
答案 2 :(得分:0)
您的代码存在的问题是您正在使用此行代码获取对象Role
<td th:text="${clients.clientRoles?.role}">...</td>
。
您所要做的就是更深层次地获取Role
名称。
像这样更新你的代码
<td th:text="${clients.clientRoles?.role.name}">...</td>
更新1
由于clientRoles
实体将@OneToMany
映射为Client
实体,因此您的client
对象将包含clientRoles
列表。
这行代码<td th:text="${clients.clientRoles?.role}">...</td>
不正确,因为clients.clientRoles
实际上是一个角色列表。
您需要迭代clientRoles
以获取客户端所拥有的所有roles
。