Spring MVC提交表单报异常:org.springframework.beans.NotReadablePropertyException

青涩春天 9月前

我的代码出现了NotReadablePropertyException异常,代码如下:


package com.nutsaboutcandywebproject.model;

import static javax.persistence.GenerationType.IDENTITY;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;


@Entity
@Table(name = "users", catalog = "db_nutsaboutcandy")
public class User {

    private int userId;
    private String username;
    private String firstname;
    private String lastname;
    private String password;
    private String role;
    private String address;

    public User(){

    }


    public User(int userId, String username, String firstname,
            String lastname, String password, String role, String address) {
        super();
        this.userId = userId;
        this.username = username;
        this.firstname = firstname;
        this.lastname = lastname;
        this.password = password;
        this.role = role;
        this.address = address;
    }
    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "user_id", unique = true, nullable = false)
    public Integer getUserId() {
        return userId;
    }
    public void setUserId(int userId) {
        this.userId = userId;
    }

    @Column(name = "username", nullable = false)
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }

    @Column(name = "firstname", nullable = false)
    public String getFirstname() {
        return firstname;
    }
    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    @Column(name = "lastname", nullable = false)
    public String getLastname() {
        return lastname;
    }
    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    @Column(name = "password", nullable = false)
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        if(password.length()>=6){
            this.password = password;
        }
        else{
            throw new IllegalArgumentException("Password length must be more than 6 characters");
        }
    }

    @Column(name = "user_role", nullable = false)
    public String getRole() {
        return role;
    }
    public void setRole(String role) {
        this.role = role;
    }

    @Column(name = "address", nullable = false)
    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

}


这是我的控制器

@RequestMapping(value = "/login",method = RequestMethod.POST)
public ModelAndView login(@ModelAttribute("user")User user, BindingResult bindingResult, 
        HttpServletRequest request, HttpServletResponse response, 
        @RequestParam("usernametxt") String username, @RequestParam("passwordtxt") String password) 
{
    try 
    {
        // Using Spring ValidationUtils class to check for empty fields.
        // This will add the error if any in the bindingResult object.
        ValidationUtils.rejectIfEmptyOrWhitespace(bindingResult,"userName","userName", "Username can not be empty.");
        ValidationUtils.rejectIfEmptyOrWhitespace(bindingResult,"password","password", "Password not be empty");

        if (bindingResult.hasErrors())
        {
            //returning the errors on same page if any errors..
            return new ModelAndView("/LoginPage", "user", user);
        }
        else
        {
            // If the user details is validated then redirecting the user to success page, 
            // else returning the error message on login page.
            ServiceFacade facade = new ServiceFacadeImpl();
            List<User> userList = facade.getAllUsers();
            for(int index = 0; index < userList.size(); index++){
                String uname = userList.get(index).getUsername();
                String pword = userList.get(index).getPassword();
                if(username.equals(uname) && password.equals(pword))
                {
                    request.getSession().setAttribute("user", user);
                    //Creating a redirection view to success page. This will redirect to UsersController
                    RedirectView redirectView = new RedirectView("success.do", true);
                    return new ModelAndView(redirectView);
                }
                else
                {
                    bindingResult.addError(new ObjectError("Invalid", "Invalid credentials. " +
                            "Username or Password is incorrect."));
                    //return new ModelAndView("login", "user", user);
                }
            }

        }
    } catch (Exception e) {
        System.out.println("Exception in LoginController "+e.getMessage());
        e.printStackTrace();
        return new ModelAndView("/LoginPage", "user", user);
    }
    return null;
}


异常堆栈信息如下:

org.springframework.beans.NotReadablePropertyException: Invalid property 'userName' of bean class [com.nutsaboutcandywebproject.model.User]: Bean property 'userName' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
    at org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:725)
    at org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:716)
    at org.springframework.validation.AbstractPropertyBindingResult.getActualFieldValue(AbstractPropertyBindingResult.java:99)
    at org.springframework.validation.AbstractBindingResult.getFieldValue(AbstractBindingResult.java:229)
    at org.springframework.validation.ValidationUtils.rejectIfEmptyOrWhitespace(ValidationUtils.java:245)
    at org.springframework.validation.ValidationUtils.rejectIfEmptyOrWhitespace(ValidationUtils.java:203)
    at com.nutsaboutcandywebproject.controller.UserController.login(UserController.java:62)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:690)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:928)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:539)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:300)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:724)