错误:java.lang.NoSuchMethodException:java.lang.Long()在spring MVC中
这@modelattribute('studentId') LongstudentId是问题的根源,因为spring找不到可以提供此Long对象的方法,因此它试图实例化一个对象并将其作为方法参数传递。 您可以:
@RequestMapping(value = '/read.html')
public String readStudent(Model model,Long studentId) { Student student = null; studentId = 2l; try {student = serviceFile.readStudent(studentId); } catch(Exception e){model.addAttribute(“message”, “Some thing went wrong !!!! Exception occured”);return “message”; } model.addAttribute(“student”, student); return “read”;}
在您的 中
@modelattribute
public void provideStudentId(Model model){ model.addAttribute(“studentId”, new Long(1));}
官方文件
@RequestMapping(path = '/owners/{ownerId}/pets/{petId}/edit', method = RequestMethod.POST)public String processSubmit(@modelattribute Pet pet) { }
给定以上示例,Pet实例可以从哪里来?有几种选择:
由于使用@SessionAttributes,它可能已经在模型中-请参阅“使用@SessionAttributes在请求之间的HTTP会话中存储模型属性”一节。由于同一控制器中的@modelattribute方法,它可能已经在模型中-如上一节中所述。可以基于URI模板变量和类型转换器(在下面更详细地说明)来检索它。可以使用其默认构造函数实例化它。如果studentId是参数的名字从你可以使用UI发送@RequestParam这样
@RequestMapping(value = '/read.html')public String readStudent(Model model, @RequestParam('studentId') Long studentId) { Student student = null; studentId = 2l; try {student = serviceFile.readStudent(studentId); } catch(Exception e) {model.addAttribute('message', 'Some thing went wrong !!!! Exception occoured');return 'message'; } model.addAttribute('student', student); return 'read';}解决方法
从数据库读取学生对象时出现此错误。
org.springframework.beans.BeanInstantiationException: Failed to instantiate [java.lang.Long]: No default constructor found; nested exception is java.lang.NoSuchMethodException: java.lang.Long.<init>()org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:107)
全栈跟踪:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [java.lang.Long]: No default constructor found; nested exception is java.lang.NoSuchMethodException: java.lang.Long.<init>() org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857) javax.servlet.http.HttpServlet.service(HttpServlet.java:617) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842) javax.servlet.http.HttpServlet.service(HttpServlet.java:717)root causeorg.springframework.beans.BeanInstantiationException: Failed to instantiate [java.lang.Long]: No default constructor found; nested exception is java.lang.NoSuchMethodException: java.lang.Long.<init>() org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:107) org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveModelAttribute(HandlerMethodInvoker.java:775) org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:368) org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:172) org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:446) org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:434) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857) javax.servlet.http.HttpServlet.service(HttpServlet.java:617) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842) javax.servlet.http.HttpServlet.service(HttpServlet.java:717)root causejava.lang.NoSuchMethodException: java.lang.Long.<init>() java.lang.Class.getConstructor0(Class.java:3082) java.lang.Class.getDeclaredConstructor(Class.java:2178) org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:104) org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveModelAttribute(HandlerMethodInvoker.java:775) org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:368) org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:172) org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:446) org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:434) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857) javax.servlet.http.HttpServlet.service(HttpServlet.java:617) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842) javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
学生.java
@Entity@Table(name='Student')public class Student implements Serializable{ private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy=GenerationType.AUTO) @Column(name='studentId') Long studentId; @Column(name='studentName') String studentName;
Controller.java
@RequestMapping(value = '/read.html') public String readStudent(Model model,@ModelAttribute('studentId') Long studentId) {Student student = null;studentId = 2l;try{ student = serviceFile.readStudent(studentId);}catch(Exception e){ model.addAttribute('message','Some thing went wrong !!!! Exception occoured'); return 'message';} model.addAttribute('student',student);return 'read'; }
daoImpl.java
@Repository@Transactionalpublic class DaoImplFile implements DaoFile { private EntityManager entityManager; public EntityManager getEntityManager() {return entityManager; } @PersistenceContext public void setEntityManager(EntityManager entityManager) {this.entityManager = entityManager; } @Override public Student read(Long studentId) throws NullPointerException { Student student = entityManager.find(Student.class,studentId);if (student!=null) { return student;} else { return null;} }
相关文章:
1. PHP中的$this代表当前的类还是方法?2. javascript - vue2.0中使用vue2-dropzone的demo,vue2-dropzone的github网址是什么??百度不到。3. 鼠标过导航标签时,无效果,请问如何查找问题4. javascript - avalon.js ms-on 事件绑定无效 ?5. html - 这种错位的时间轴怎么布局,然后用css实现?6. html5 - 目前 公司App 嵌入H5页面 做个 手机支付功能 没有做过 所以 请求各位有经验的 给个思路7. java - spring-data Jpa 不需要执行save 语句,Set字段就可以自动执行保存的方法?求解8. IOS app应用软件的id号怎么查询?比如百度贴吧的app-id=4779278139. java - butterknife怎么绑定多个view10. java - 如图代码,Collection 类中的iterator()是抽象方法,为什么可以调用?