我收到此错误"无效的属性'用户名' bean类[java.util.ArrayList]:Bean属性'用户名'不可读或具有无效的getter方法"。
我有正确的名称用户名表单bean,但我仍然收到此错误,请你帮我解决这个错误..?
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
@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);
}
}
答案 0 :(得分:3)
你在做什么是错的!
您正在使用用户列表而不是用户对象绑定表单。
将您的控制器方法更改为此
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;
}
答案 1 :(得分:0)
尝试使用此代码:
@RequestMapping(value="/userList")
public ModelAndView getUserList(){
List<User> userList = userService.getUserList();
ModelAndView mav = new ModelAndView("userList");
mav.addObject("userList", userList);
mav.addObject("user", user);
return mav;
}