Constructor Dependency Injection in Spring MVC 3

I'm trying to do a dependency injection in my controller, instead using setters methods, but it didnot work. What I want: just bind the domain model with my form, in a controller class using springframework 3.1. My domain class: public class Person { private String name; @Autowired public Person(String name) { this.name = name; } public String getName() { return this.name; } } My jsp(person-get):
NAME:
My Controller: @Controller public class PersonController { @RequestMapping(value = "test-person", method = RequestMethod.GET) public String loadViewGet() { return "person-get"; } @RequestMapping(value = "test-person", method = RequestMethod.POST) public String doPost(Person person, Model model) { model.addAttribute("person", person); return "person-post"; } } And the last jsp showing the entered name of the person by the user (person-post):
NAME: ${person.name}
The throwed exception: java.lang.NoSuchMethodException: models.Person.() java.lang.Class.getConstructor0(Unknown Source) java.lang.Class.getDeclaredConstructor(Unknown Source) org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:104) org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttri bute(ModelAttributeMethodProcessor.java:132) org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodPr ocessor.createAttribute(ServletModelAttributeMethodProcessor.java:81) org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgu ment(ModelAttributeMethodProcessor.java:103) org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resol veArgument(HandlerMethodArgumentResolverComposite.java:75) org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValu es(InvocableHandlerMethod.java:156) org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(Invo cableHandlerMethod.java:117) org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod .invokeAndHandle(ServletInvocableHandlerMethod.java:96) org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter. invokeHandlerMethod(RequestMappingHandlerAdapter.java:617) org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter. handleInternal(RequestMappingHandlerAdapter.java:578) org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(Abst ractHandlerMethodAdapter.java:80) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java :923) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java: 852) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.ja va:882) org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789) javax.servlet.http.HttpServlet.service(HttpServlet.java:641) javax.servlet.http.HttpServlet.service(HttpServlet.java:722) br.gov.sp.mococa.filtros.FiltroDeConexao.doFilter(FiltroDeConexao.java:31) I think that the spring is looking for a default constructor whitout parameters. Why it is hapenning? Thanks!
Where and how did you declare your Person bean in ApplicationContext?

以上就是Constructor Dependency Injection in Spring MVC 3的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏