正如标题所示,在百里香和春天的一些实验中,我遇到了一些问题。例外:
org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'username' cannot be found on object of type 'com.black.spring.boot.eventsharing.demo.domain.User' - maybe not public or not valid?
这是我的用户控制器:
@GetMapping
public ModelAndView list(@RequestParam(value="async",required=false) boolean async,
@RequestParam(value="pageIndex",required=false,defaultValue="0") int pageIndex,
@RequestParam(value="pageSize",required=false,defaultValue="10") int pageSize,
@RequestParam(value="name",required=false,defaultValue="") String name,
Model model) {
Pageable pageable = PageRequest.of(pageIndex, pageSize);
Page<User> page = userService.listUsersByNameLike(name, pageable);
List<User> list = page.getContent();
model.addAttribute("page", page);
model.addAttribute("userList", list);
for (User user : list) {
System.out.println("!!!!!!" + user.toString() + "!!!!!");
}
return new ModelAndView(async?"users/list :: #mainContainerRepleace":"users/list", "userModel", model);
}
当我的代码调用控制器时,我已经做了一些基本的实验,这是系统打印:
!!!!!!User{id=1, name='asd', email='asd@12', username='asd', password='asd', avatar='null'}!!!!!
这是我的html代码:
<tr th:each="user : ${userModel.userList}">
<td th:text="${user.id}">place_holder_id</td>
<td th:text="${user.username}">place_holder_user_name</td>
<td th:text="${user.name}">place_holder_name</td>
<td th:text="${user.email}">place_holder_email</td>
<td> ...
这是我的User.java
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotEmpty(message = "Name should not be empty")
@Size(min=2, max=20)
@Column(nullable = false, length = 20)
private String name;
@NotEmpty(message = "Email should not be empty")
@Size(max=50)
@Email(message= "invalid email format" )
@Column(nullable = false, length = 50, unique = true)
private String email;
@NotEmpty(message = "Username should not be empty")
@Size(min=3, max=20)
@Column(nullable = false, length = 20, unique = true)
private String username;
@NotEmpty(message = "Password should not be empty")
@Size(max=100)
@Column(length = 100)
private String password;
@Column(length = 200)
private String avatar;
protected User() {
}
public User(Long id, String name, String email, String username) {
this.id = id;
this.name = name;
this.email = email;
this.username = username;
}
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 getAvatar() { return avatar; }
public void setAvatar(String avatar) { this.avatar = avatar; }
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; }
public String getEmail() { return email; }
public void setEmail(String email) { this.email = email; }
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", email='" + email + '\'' +
", username='" + username + '\'' +
", password='" + password + '\'' +
", avatar='" + avatar + '\'' +
'}';
}
}
我认为错误消息意味着在我的用户中,没有名为“用户名”的属性,但是我确实拥有该变量。我不知道出了什么问题,所有提示都将不胜感激。
答案 0 :(得分:0)
您的User
实体必须遵循JavaBean契约,但是由于用户名字段的获取者/设置者命名不正确(getUserName/setUserName
),因此不是这样。适当的是:
public String getUsername() { return username; }
public void setUsername(String username) { this.username = username; }