2

I am getting this error "Invalid property 'username' of bean class [java.util.ArrayList]: Bean property 'username' is not readable or has an invalid getter method".

I have the correct form bean with the name username though, but still i am getting this error, could you please help me to resolve this error..?

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

}

And my jsp page is below

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

and my controller is provided below

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

    }


}
4

2 回答 2

3

What you are doing is wrong!

You are binding the form with a list of Users instead of the User object.

Change your controller method to this

 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;
}
于 2013-08-13T21:16:55.270 回答
0

Try to this code:

 @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;      
    }
于 2013-08-13T21:16:45.360 回答