'th:checked throws exception
My Thymeleaf cant compile, i can't understand what's wrong. I need to create checkbox and make checked only roles, which user contains. I reсeive this execption: Caused by: org.attoparser.ParseException: Exception evaluating SpringEL expression: "user.getRoles().contains(${role})" (template: "editUser" - line 19, col 54) at org.attoparser.MarkupParser.parseDocument(MarkupParser.java:393) ~[attoparser-2.0.5.RELEASE.jar:2.0.5.RELEASE] at org.attoparser.MarkupParser.parse(MarkupParser.java:257) ~[attoparser-2.0.5.RELEASE.jar:2.0.5.RELEASE] at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:230) ~[thymeleaf-3.0.15.RELEASE.jar:3.0.15.RELEASE] ... 89 common frames omitted
Role(entity):
@Entity
@Table(name = "sweater_roles")
public class Role implements GrantedAuthority {
public static List<Role> allRoles = Arrays.asList(new Role("USER"), new Role("ADMIN"));
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
public Role(String name) {
this.name = name;
}
public Role() {
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Role)) return false;
Role role = (Role) o;
return getName().equals(role.getName());
}
@Override
public int hashCode() {
return Objects.hash(getName());
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String getAuthority() {
return getName();
}
}
User(entity):
@Entity
@Table(name = "sweater_user")
public class User implements UserDetails{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String username;
private String password;
private boolean active;
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(
name = "sweater_user_role",
joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id")
)
private Collection<Role> roles;
public User(String username, String password, boolean active, Collection<Role> roles) {
this.username = username;
this.password = password;
this.active = active;
this.roles = roles;
}
public User() {
}
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return true;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return isActive();
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return getRoles();
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = 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 boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
public Collection<Role> getRoles() {
return roles;
}
public void setRoles(Collection<Role> roles) {
this.roles = roles;
}
}
Controller:
@GetMapping("{user}")
public String editUser(@PathVariable User user, Model model){
model.addAttribute("user", user);
model.addAttribute("roles", Role.allRoles);
return "editUser";
}
HTML file:
Roles:
<th:block th:each="role : ${roles}">
<div>
<label th:for="${role}" th:text="${role}"></label>
<input type="checkbox" th:name="${role}" th:checked="${user.getRoles().contains(${role})}">
</div>
</th:block>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
