org.springframework.beans.NotReadablePropertyException:bean类的属性'userName'无效:Bean属性'userName'不可读或有

时间:2016-04-16 16:47:04

标签: angularjs hibernate spring-mvc jpa-2.0

我正在开发Spring MVC + AngularJS + JPA示例。在这个例子中,我希望用户注册,现在当我点击注册链接时,我看到以下错误即将到来,不确定出了什么问题?

我在下面看到错误:

org.springframework.beans.NotReadablePropertyException: Invalid property 'userName' of bean class [com.sivalabs.angularcrud.entities.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:723) ~[spring-beans-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:714) ~[spring-beans-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.validation.AbstractPropertyBindingResult.getActualFieldValue(AbstractPropertyBindingResult.java:99) ~[spring-context-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.validation.AbstractBindingResult.getFieldValue(AbstractBindingResult.java:229) ~[spring-context-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:120) ~[spring-webmvc-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:168) ~[spring-webmvc-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.web.servlet.tags.form.ErrorsTag.shouldRender(ErrorsTag.java:141) ~[spring-webmvc-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.web.servlet.tags.form.AbstractHtmlElementBodyTag.writeTagContent(AbstractHtmlElementBodyTag.java:48) ~[spring-webmvc-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:84) ~[spring-webmvc-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:80) ~[spring-webmvc-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.apache.jsp.WEB_002dINF.views.register_jsp._jspService(register_jsp.java:219) [springmvc-angular-crud/:na]
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) [jasper.jar:8.0.30]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) [servlet-api.jar:na]
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438) [jasper.jar:8.0.30]
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396) [jasper.jar:8.0.30]
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340) [jasper.jar:8.0.30]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) [servlet-api.jar:na]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291) [catalina.jar:8.0.30]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) [catalina.jar:8.0.30]
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) [tomcat-websocket.jar:8.0.30]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) [catalina.jar:8.0.30]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) [catalina.jar:8.0.30]
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:719) [catalina.jar:8.0.30]
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:465) [catalina.jar:8.0.30]
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:390) [catalina.jar:8.0.30]
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:317) [catalina.jar:8.0.30]
    at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:209) [spring-webmvc-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:266) [spring-webmvc-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1225) [spring-webmvc-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1012) [spring-webmvc-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959) [spring-webmvc-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876) [spring-webmvc-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:931) [spring-webmvc-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:822) [spring-webmvc-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:622) [servlet-api.jar:na]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:807) [spring-webmvc-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) [servlet-api.jar:na]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291) [catalina.jar:8.0.30]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) [catalina.jar:8.0.30]
    at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:177) [spring-orm-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108) [spring-web-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) [catalina.jar:8.0.30]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) [catalina.jar:8.0.30]
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) [tomcat-websocket.jar:8.0.30]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) [catalina.jar:8.0.30]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) [catalina.jar:8.0.30]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212) [catalina.jar:8.0.30]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) [catalina.jar:8.0.30]
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) [catalina.jar:8.0.30]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141) [catalina.jar:8.0.30]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) [catalina.jar:8.0.30]
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616) [catalina.jar:8.0.30]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) [catalina.jar:8.0.30]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:521) [catalina.jar:8.0.30]
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1096) [tomcat-coyote.jar:8.0.30]
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:674) [tomcat-coyote.jar:8.0.30]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500) [tomcat-coyote.jar:8.0.30]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456) [tomcat-coyote.jar:8.0.30]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_45]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_45]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-util.jar:8.0.30]
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_45]
Apr 16, 2016 10:01:08 PM org.apache.catalina.core.ApplicationDispatcher invoke
SEVERE: Servlet.service() for servlet jsp threw exception

的login.jsp:

<!DOCTYPE html>
<%@include file="taglib.jsp" %>
<html>
<head>
<title>Login</title>
<base href="${rootUrl}">
<%@ include file="assets.jspf" %>  
</head>
<body>
        <div class="col-md-6 col-md-offset-2">
        <c:if test="${param.error != null}">
             <div class="alert alert-danger">
                 Invalid UserName and Password.
             </div>
         </c:if>
         <c:if test="${param.logout != null}">
             <div class="alert alert-success">
                 You have been logged out.
             </div>
         </c:if>    
         </div>  

     <div class="row">
        <div class="col-md-6 col-md-offset-2">
            <h2>User Login Form</h2>
            <form:form id="loginForm" method="post" action="login" modelAttribute="user" 
        class="form-horizontal" role="form" cssStyle="width: 800px; margin: 0 auto;">
          <div class="form-group">
            <label for="username" class="col-sm-2 control-label">UserName*</label>
            <div class="col-sm-4">
              <input type="text" id="username" name="username" class="form-control" placeholder="UserName" />
            </div>
          </div>
          <div class="form-group">
            <label for="password" class="col-sm-2 control-label">Password*</label>
            <div class="col-sm-4">
              <input type="password" id="password" name="password" class="form-control" placeholder="Password" />
            </div>
          </div>
          <div class="form-group">
            <div class="col-sm-offset-2 col-sm-4">
              <input type="submit" class="btn btn-primary" value="Login">
            </div>
          </div>
           <div class="form-group">
            <div class="col-sm-offset-2 col-sm-4">
              New User? <a href="register">Register</a>
            </div>
          </div>

        </form:form>
    </div>
</div>
</body>
</html>

register.jsp:

<!DOCTYPE html>
<%@include file="taglib.jsp"%>
<html>
<head>
<title>Create User</title>
<script type="text/javascript">
$(document).ready(function() {

    $("#registrationForm").submit(function( event ) {
          var userName = $.trim($("#userName").val());
          var password = $.trim($("#password").val());
          var firstName = $.trim($("#firstName").val());
          var email = $.trim($("#email").val());

          if(userName == '' || password == '' || firstName == '' || email == ''){
                alert("Please enter all mandatory fields");
                event.preventDefault();
                return false;
          }
    });
});
</script>
</head>
<body>

    <div class="col-md-6 col-md-offset-2">
        <c:if test="${ERROR != null }">
            <div class="alert alert-danger">
                <p>${ERROR}
            </div>
        </c:if>
    </div>

    <div class="row">
        <div class="col-md-6 col-md-offset-2">
            <h2>User Registration Form</h2>
            <form:form id="registrationForm" method="post" action="register"
                modelAttribute="user" cssStyle="width: 800px; margin: 0 auto;"
                class="form-horizontal" role="form">
                <div class="form-group">
                    <label for="userName" class="col-sm-2 control-label">UserName*</label>
                    <div class="col-sm-4">
                        <input type="text" id="userName" name="userName"
                            class="form-control" placeholder="UserName" />
                        <form:errors path="userName" />
                    </div>
                </div>
                <div class="form-group">
                    <label for="password" class="col-sm-2 control-label">Password*</label>
                    <div class="col-sm-4">
                        <input type="password" id="password" name="password"
                            class="form-control" placeholder="Password" />
                        <form:errors path="password" />
                    </div>
                </div>

                <div class="form-group">
                    <label for="email" class="col-sm-2 control-label">Email*</label>
                    <div class="col-sm-4">
                        <input type="text" id="email" name="email" class="form-control"
                            placeholder="Email" />
                        <form:errors path="email" />
                    </div>
                </div>

                <div class="form-group">
                    <label for="firstName" class="col-sm-2 control-label">FirstName*</label>
                    <div class="col-sm-4">
                        <input type="text" id="firstName" name="firstName"
                            class="form-control" placeholder="FirstName" />
                        <form:errors path="firstName" />
                    </div>
                </div>

                <div class="form-group">
                    <label for="lastName" class="col-sm-2 control-label">LastName</label>
                    <div class="col-sm-4">
                        <input type="text" id="lastName" name="lastName"
                            class="form-control" placeholder="LastName" />
                        <form:errors path="lastName" />
                    </div>
                </div>

                <div class="form-group">
                    <label for="dob" class="col-sm-2 control-label">Date Of Birth</label>
                    <div class="col-sm-4">
                        <input type="text" id="dob" name="dob" class="form-control"
                            placeholder="dd-MM-yyyy" />
                        <form:errors path="dob" cssClass="text-danger" />
                    </div>
                </div>

                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-4">
                        <input type="submit" class="btn btn-primary" value="Register">
                    </div>
                </div>

                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-4">
                        Already Registered? <a href="login">Login</a>
                    </div>
                </div>
            </form:form>
        </div>
    </div>
</body>
</html>

User.java

@Entity
@Table(name = "USERS")
public class User implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Integer id;
    @Column(name = "email", nullable = false, unique = true, length = 50)
    private String email;
    @Column(name = "password", nullable = false, length = 50)
    private String password;
    @Column(name = "firstname", nullable = false, length = 50)
    private String firstName;
    @Column(name = "lastname", length = 50)
    private String lastName;

    @Temporal(TemporalType.DATE)
    private Date dob;
    private boolean enabled=true;

    @OneToMany(fetch=FetchType.EAGER, cascade=CascadeType.ALL)
    @JoinColumn(name="user_id")
    private Set<Role> roles = new HashSet<>();

    @OneToMany(cascade=CascadeType.ALL, mappedBy="user")
    //@JoinColumn(name="user_id")
    private Set<Contact> contacts = new HashSet<>();

    public User(){ }

    public User(Integer id){
        this.id = id;
    }

    public User(Integer id, String email, String password, String firstName){
        this.id = id;
        this.email = email;
        this.password = password;
        this.firstName = firstName;
    }

    public User(Integer id, String email, String password, String firstName, String lastName, Date dob){
        this.id = id;
        this.email = email;
        this.password = password;
        this.firstName = firstName;
        this.lastName = lastName;
        this.dob = dob;
    }
    // setters and getters
}

}

Contact.java

@Entity
@Table(name = "CONTACTS")
@JsonIgnoreProperties({"user"})
public class Contact implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "contact_id")
    private Integer id;
    @Column(name = "email",length = 50)
    private String email;
    @Column(name = "firstname", length = 50)
    private String firstName;
    @Column(name = "lastname", length = 50)
    private String lastName;    
    @Temporal(TemporalType.DATE)
    private Date dob;
    @ManyToOne
    @JoinColumn(name="user_id")
    private User user;
    // setters and getters
}

UserController.java

@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping(value = "login", method = RequestMethod.GET)
    public String loginForm(Model model){
        model.addAttribute("user", new User());
        return "login";
    }

    @RequestMapping(value = "/register", method = RequestMethod.GET)
    public String registrationForm(Model model){
        model.addAttribute("user", new User());
        return "register";
    }


    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public String logOutForm(Model model){
        return "redirect:/login"; 
    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public String handleRegistration(@ModelAttribute("user") User user, BindingResult errors, Model model){
        if (errors.hasErrors()) {
            return "register";
        }
        try {
            userService.createUser(user);
            return "redirect:login";
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("ERROR", e.getMessage());
            return "register";
        }
    }
}

请帮忙

1 个答案:

答案 0 :(得分:0)

您忘记了班级private String username中的媒体资源User