gpt4 book ai didi

spring-mvc - bean 类 [java.util.ArrayList] : Bean property 'username' is not readable or has an invalid getter method 的无效属性 'username'

转载 作者:行者123 更新时间:2023-12-04 04:45:44 24 4
gpt4 key购买 nike

我收到此错误“bean 类 [java.util.ArrayList] 的无效属性‘用户名’:Bean 属性‘用户名’不可读或有无效的 getter 方法”。

我有正确的表单bean,名称为username,但我仍然收到此错误,请您帮我解决此错误..?

public class User {

private int userId;
private String username;
private String firstname;
private String lastname;
private String email;
private String lastUpdatedBy;
private String lastUpdatedDate;


public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
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 getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getLastUpdatedBy() {
return lastUpdatedBy;
}
public void setLastUpdatedBy(String lastUpdatedBy) {
this.lastUpdatedBy = lastUpdatedBy;
}
public String getLastUpdatedDate() {
return lastUpdatedDate;
}
public void setLastUpdatedDate(String lastUpdatedDate) {
this.lastUpdatedDate = lastUpdatedDate;
}

}

我的jsp页面在下面
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>UTVA Application</title>
</head>
<body>
<form:form modelAttribute="user" method="post" action="/findUser.html">
<table>
<tr>
<td><form:label path="username">User ID:</form:label></td>
<td><form:input path="username" /></td>
</tr>
<tr>
<td><form:label path="firstname">First Name</form:label></td>
<td><form:input path="firstname" /></td>
</tr>
<tr>
<td><form:label path="lastname">Last Name:</form:label></td>
<td><form:input path="lastname" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="findUser" /></td>
</tr>
</table>
</form:form>
<table>
<tr>
<td>User Id</td>
<td>First Name</td>
<td>Last Name</td>
<td>Email Address</td>
<td>Last Update Date</td>
<td>Last Update By</td>
</tr>
<c:forEach var="list" items="${userList}">
<tr>
<td>${list.userId}</td>
<td>${list.username}</td>
<td>${list.firstname}</td>
<td>${list.lastname}</td>
<td>${list.email}</td>
<td>${list.lastUpdatedBy}</td>
<td>${list.lastUpdatedDate}</td>
<td><a href="listVendors.html?userId=${list.userId}">assignVendor</a></td>
</tr>
</c:forEach>
</table>

</body>
</html>

我的 Controller 在下面提供
@Controller
@SessionAttributes
public class UserController {

protected Log logger = LogFactory.getLog(getClass());

@Autowired
UserService userService;

@RequestMapping(value="/userList")
public ModelAndView getUserList(){

List<User> userList = userService.getUserList();

return new ModelAndView("userList", "user", userList);
}

@RequestMapping(value="/findUser")
public ModelAndView findUser(@ModelAttribute("user") User user,
BindingResult result){

List<User> userResultsList = null;

//model.addAttribute("user", new User());

userResultsList = userService.findUser(user.getUsername(), user.getFirstname(),
user.getLastname());

return new ModelAndView("userList", "user", userResultsList);

}


}

最佳答案

你的做法是错误的!

您正在将表单与用户列表而不是用户对象绑定(bind)。

将您的 Controller 方法更改为此

 public ModelAndView getUserList(ModelMap model){
List<User> userList = userService.getUserList();
ModelAndView model = new ModelAndView("userList");
model.addAttribute("userList", userList);
model.addAttribute("user", new User());
return model;
}

关于spring-mvc - bean 类 [java.util.ArrayList] : Bean property 'username' is not readable or has an invalid getter method 的无效属性 'username',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18219305/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com