【问题标题】:Invalid property 'username' of bean class [java.util.ArrayList]: Bean property 'username' is not readable or has an invalid getter methodbean 类 [java.util.ArrayList] 的无效属性“用户名”:bean 属性“用户名”不可读或具有无效的 getter 方法
【发布时间】:2013-08-13 21:11:46
【问题描述】:

我收到此错误“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
@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);       

    }


}

【问题讨论】:

    标签: spring-mvc


    【解决方案1】:

    你做错了!

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

    把你的控制器方法改成这个

     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;
    }
    

    【讨论】:

    • 帮助很大。感谢您的快速周转。从昨天开始,我一直在努力度过难关。非常感谢..
    【解决方案2】:

    试试这个代码:

     @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;      
        }
    

    【讨论】:

    • 帮助很大。感谢您的快速周转。从昨天开始,我一直在努力度过难关。非常感谢..
    猜你喜欢
    • 1970-01-01
    • 2019-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-03
    • 2020-11-27
    • 2021-06-16
    相关资源
    最近更新 更多