spring No default constructor found; nested exception is java.lang.NoSuchMethodException: com.slj.mo

再見孫悟空slj發表於2016-02-24

還是在學習spring mvc 過程中學習頁面,頁面到 controller 中引數傳遞的時候在form表單提交 轉為物件的時候報的錯。


type Exception report

message Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.slj.model.Student]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.slj.model.Student.<init>()

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.slj.model.Student]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.slj.model.Student.<init>()
	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:981)
	org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:871)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
	org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:845)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

root cause

org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.slj.model.Student]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.slj.model.Student.<init>()
	org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:107)
	org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttribute(ModelAttributeMethodProcessor.java:137)
	org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttribute(ServletModelAttributeMethodProcessor.java:80)
	org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:106)
	org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:78)
	org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:162)
	org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:129)
	org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
	org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:814)
	org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:737)
	org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
	org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
	org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:969)
	org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:871)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
	org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:845)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
分析後,它說我沒有model的無參建構函式,但是我傳遞過來的有參的。這就需要討論實現原理了。

首先感覺是接受頁面的parameter 可能name 不和model 的屬性不匹配,所以就相當於沒有該model的引數傳遞過來,呼叫該model的無參構造方法。由於bean 是在未呼叫前就已經建立的,所以建立bean 時候考慮了這樣的問題,所以必須有為空的建構函式。

相關文章