Needing help to solve error message after attempting to create new entry with CRUD app Spring MVC MySql

huangapple go评论100阅读模式
英文:

Needing help to solve error message after attempting to create new entry with CRUD app Spring MVC MySql

问题

以下是您提供的内容的翻译:

我没有输入空条目,不确定这与什么有关。我已经尝试更改输入类型和数据库属性。我是Java的新手,并且正在使用Spring MVC。

用于创建“新”表单的HTML文件:

  1. <table border="0" cellpadding="10">
  2. <tr>
  3. <td>Description: </td>
  4. <td><input type="text" name="*{description}" /></td>
  5. </tr>
  6. <tr>
  7. <td>Price: </td>
  8. <td><input type="text" name="*{price}" /> </td>
  9. </tr>
  10. <tr>
  11. <td colspan="2"><button type="submit">Save</button> </td>
  12. </tr>
  13. </table>

控制器:

  1. package com.invoicebuilder;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.boot.autoconfigure.batch.BatchProperties.Job;
  4. import org.springframework.stereotype.Controller;
  5. import org.springframework.ui.Model;
  6. import org.springframework.web.bind.annotation.ModelAttribute;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RequestMethod;
  9. import java.util.List;
  10. @Controller
  11. public class AppController {
  12. @Autowired
  13. private InvoiceService service;
  14. @RequestMapping("/")
  15. public String viewHomePage(Model model) {
  16. List<job> listJobs = service.listAll();
  17. model.addAttribute("listJobs", listJobs);
  18. return "index";
  19. }
  20. @RequestMapping("/new")
  21. public String showNewJobForm(Model model) {
  22. Job job = new Job();
  23. model.addAttribute("job", job);
  24. return "new_job";
  25. }
  26. @RequestMapping(value = "/save", method = RequestMethod.POST)
  27. public String saveJob(@ModelAttribute("job") job job) {
  28. service.save(job);
  29. return "redirect:/";
  30. }
  31. }

这是我遇到问题的错误代码,我正在使用MySQL数据库,我能够一直到达创建新工作条目的屏幕,但在点击保存后,它给我一个如下所示的错误:

  1. java.sql.SQLIntegrityConstraintViolationException: Column 'description' cannot be null
  2. ...

(剩余部分省略)

英文:

I am not inputting a null entry, not sure what this has to do with. I have tried changing the input types and the database properties. I am new to Java and using Spring MVC for this.

HTML File to for create "new" form:

  1. &lt;table border=&quot;0&quot; cellpadding=&quot;10&quot;&gt;
  2. &lt;tr&gt;
  3. &lt;td&gt;Description: &lt;/td&gt;
  4. &lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;*{description}&quot; /&gt;&lt;/td&gt;
  5. &lt;/tr&gt;
  6. &lt;tr&gt;
  7. &lt;td&gt; Price: &lt;/td&gt;
  8. &lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;*{price}&quot; /&gt;&lt;/td&gt;
  9. &lt;/tr&gt;
  10. &lt;tr&gt;
  11. &lt;td colspan=&quot;2&quot;&gt;&lt;button type=&quot;submit&quot;&gt;Save&lt;/button&gt; &lt;/td&gt;
  12. &lt;/tr&gt;
  13. &lt;/table&gt;

Controller:

  1. /**
  2. *
  3. */
  4. package com.invoicebuilder;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.boot.autoconfigure.batch.BatchProperties.Job;
  7. import org.springframework.stereotype.Controller;
  8. import org.springframework.ui.Model;
  9. import org.springframework.web.bind.annotation.ModelAttribute;
  10. import org.springframework.web.bind.annotation.RequestMapping;
  11. import org.springframework.web.bind.annotation.RequestMethod;
  12. import java.util.List;
  13. /**
  14. * @author jmens
  15. *
  16. */
  17. @Controller
  18. public class AppController {
  19. @Autowired
  20. private InvoiceService service;
  21. @RequestMapping(&quot;/&quot;)
  22. public String viewHomePage(Model model) {
  23. List&lt;job&gt; listJobs = service.listAll();
  24. model.addAttribute(&quot;listJobs&quot;, listJobs);
  25. return &quot;index&quot;;
  26. }
  27. @RequestMapping(&quot;/new&quot;)
  28. public String showNewJobForm(Model model) {
  29. Job job = new Job();
  30. model.addAttribute(&quot;job&quot;, job);
  31. return &quot;new_job&quot;;
  32. }
  33. @RequestMapping(value = &quot;/save&quot;, method = RequestMethod.POST)
  34. public String saveJob(@ModelAttribute(&quot;job&quot;) job job) {
  35. service.save(job);
  36. return &quot;redirect:/&quot;;
  37. }
  38. }`

This is the error code that I am having issues with, I am using MySQL for the database and am able to make it all the way to the screen where I am able to create the new job entry but after clicking save, it gives me an error as shown below:

  1. java.sql.SQLIntegrityConstraintViolationException: Column &#39;description&#39; cannot be null
  2. at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:117) ~[mysql-connector-
  3. java-8.0.19.jar:8.0.19]
  4. at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) ~[mysql-connector-java-
  5. 8.0.19.jar:8.0.19]
  6. at
  7. com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) ~
  8. [mysql-connector-java-8.0.19.jar:8.0.19]
  9. at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:953) ~
  10. [mysql-connector-java-8.0.19.jar:8.0.19]
  11. at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1092)
  12. ~[mysql-connector-java-8.0.19.jar:8.0.19]
  13. at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1040)
  14. ~[mysql-connector-java-8.0.19.jar:8.0.19]
  15. at com.mysql.cj.jdbc.ClientPreparedStatement.executeLargeUpdate(ClientPreparedStatement.java:1347) ~
  16. [mysql-connector-java-8.0.19.jar:8.0.19]
  17. at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdate(ClientPreparedStatement.java:1025) ~
  18. [mysql-connector-java-8.0.19.jar:8.0.19]
  19. at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeUpdate(ProxyPreparedStatement.java:61) ~
  20. [HikariCP-3.2.0.jar:na]
  21. at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeUpdate(HikariProxyPreparedStatement.java) ~[HikariCP-3.2.0.jar:na]
  22. at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:175) ~[hibernate-core-5.3.15.Final.jar:5.3.15.Final]
  23. at org.hibernate.dialect.identity.GetGeneratedKeysDelegate.executeAndExtract(GetGeneratedKeysDelegate.java:57) ~[hibernate-core-5.3.15.Final.jar:5.3.15.Final]
  24. at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:42) ~[hibernate-core-5.3.15.Final.jar:5.3.15.Final]
  25. at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3090) ~[hibernate-core-5.3.15.Final.jar:5.3.15.Final]
  26. at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3683) ~[hibernate-core-5.3.15.Final.jar:5.3.15.Final]
  27. at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:81) ~[hibernate-core-5.3.15.Final.jar:5.3.15.Final]
  28. at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:645) ~[hibernate-core-5.3.15.Final.jar:5.3.15.Final]
  29. at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:282) ~[hibernate-core-5.3.15.Final.jar:5.3.15.Final]
  30. at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:263) ~[hibernate-core-5.3.15.Final.jar:5.3.15.Final]
  31. at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:317) ~[hibernate-core-5.3.15.Final.jar:5.3.15.Final]
  32. at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:332) ~[hibernate-core-5.3.15.Final.jar:5.3.15.Final]
  33. at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:289) ~[hibernate-core-5.3.15.Final.jar:5.3.15.Final]
  34. at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:196) ~[hibernate-core-5.3.15.Final.jar:5.3.15.Final]
  35. at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:127) ~[hibernate-core-5.3.15.Final.jar:5.3.15.Final]
  36. at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:192) ~[hibernate-core-5.3.15.Final.jar:5.3.15.Final]
  37. at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:135) ~[hibernate-core-5.3.15.Final.jar:5.3.15.Final]
  38. at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:62) ~[hibernate-core-5.3.15.Final.jar:5.3.15.Final]
  39. at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:804) ~[hibernate-core-5.3.15.Final.jar:5.3.15.Final]
  40. at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:789) ~[hibernate-core-5.3.15.Final.jar:5.3.15.Final]
  41. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_221]
  42. at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_221]
  43. at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_221]
  44. at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_221]
  45. at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:351) ~[spring-orm-5.1.14.RELEASE.jar:5.1.14.RELEASE]
  46. at com.sun.proxy.$Proxy77.persist(Unknown Source) ~[na:na]
  47. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_221]
  48. at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_221]
  49. at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_221]
  50. at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_221]
  51. at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:310) ~[spring-orm-5.1.14.RELEASE.jar:5.1.14.RELEASE]
  52. at com.sun.proxy.$Proxy77.persist(Unknown Source) ~[na:na]
  53. at org.springframework.data.jpa.repository.support.SimpleJpaRepository.save(SimpleJpaRepository.java:535) ~[spring-data-jpa-2.1.16.RELEASE.jar:2.1.16.RELEASE]
  54. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_221]
  55. at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_221]
  56. at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_221]
  57. at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_221]
  58. at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:359) ~[spring-data-commons-2.1.16.RELEASE.jar:2.1.16.RELEASE]
  59. at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:200) ~[spring-data-commons-2.1.16.RELEASE.jar:2.1.16.RELEASE]
  60. at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:644) ~[spring-data-commons-2.1.16.RELEASE.jar:2.1.16.RELEASE]
  61. at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.14.RELEASE.jar:5.1.14.RELEASE]
  62. at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:608) ~[spring-data-commons-2.1.16.RELEASE.jar:2.1.16.RELEASE]
  63. at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lambda$invoke$3(RepositoryFactorySupport.java:595) ~[spring-data-commons-2.1.16.RELEASE.jar:2.1.16.RELEASE]
  64. at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:595) ~[spring-data-commons-2.1.16.RELEASE.jar:2.1.16.RELEASE]
  65. at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.14.RELEASE.jar:5.1.14.RELEASE]
  66. at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59) ~[spring-data-commons-2.1.16.RELEASE.jar:2.1.16.RELEASE]
  67. at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.14.RELEASE.jar:5.1.14.RELEASE]
  68. at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:295) ~[spring-tx-5.1.14.RELEASE.jar:5.1.14.RELEASE]
  69. at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98) ~[spring-tx-5.1.14.RELEASE.jar:5.1.14.RELEASE]
  70. at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.14.RELEASE.jar:5.1.14.RELEASE]
  71. at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139) ~[spring-tx-5.1.14.RELEASE.jar:5.1.14.RELEASE]
  72. at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.14.RELEASE.jar:5.1.14.RELEASE]
  73. at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:144) ~[spring-data-jpa-2.1.16.RELEASE.jar:2.1.16.RELEASE]
  74. at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.14.RELEASE.jar:5.1.14.RELEASE]
  75. at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$ExposeRepositoryInvocationInterceptor.invoke(CrudMethodMetadataPostProcessor.java:364) ~[spring-data-jpa-2.1.16.RELEASE.jar:2.1.16.RELEASE]
  76. at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.14.RELEASE.jar:5.1.14.RELEASE]
  77. at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93) ~[spring-aop-5.1.14.RELEASE.jar:5.1.14.RELEASE]
  78. at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.14.RELEASE.jar:5.1.14.RELEASE]
  79. at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61) ~[spring-data-commons-2.1.16.RELEASE.jar:2.1.16.RELEASE]
  80. at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.14.RELEASE.jar:5.1.14.RELEASE]
  81. at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) ~[spring-aop-5.1.14.RELEASE.jar:5.1.14.RELEASE]
  82. at com.sun.proxy.$Proxy79.save(Unknown Source) ~[na:na]
  83. at com.invoicebuilder.InvoiceService.save(InvoiceService.java:20) ~[classes/:na]
  84. at com.invoicebuilder.AppController.saveJob(AppController.java:46) ~[classes/:na]
  85. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_221]
  86. at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_221]
  87. at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_221]
  88. at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_221]
  89. at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.1.14.RELEASE.jar:5.1.14.RELEASE]
  90. at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.1.14.RELEASE.jar:5.1.14.RELEASE]
  91. at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105) ~[spring-webmvc-5.1.14.RELEASE.jar:5.1.14.RELEASE]
  92. at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:893) ~[spring-webmvc-5.1.14.RELEASE.jar:5.1.14.RELEASE]
  93. at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:798) ~[spring-webmvc-5.1.14.RELEASE.jar:5.1.14.RELEASE]
  94. at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.1.14.RELEASE.jar:5.1.14.RELEASE]
  95. at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) ~[spring-webmvc-5.1.14.RELEASE.jar:5.1.14.RELEASE]
  96. at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) ~[spring-webmvc-5.1.14.RELEASE.jar:5.1.14.RELEASE]
  97. at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.1.14.RELEASE.jar:5.1.14.RELEASE]
  98. at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909) ~[spring-webmvc-5.1.14.RELEASE.jar:5.1.14.RELEASE]
  99. at javax.servlet.http.HttpServlet.service(HttpServlet.java:660) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
  100. at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.1.14.RELEASE.jar:5.1.14.RELEASE]
  101. at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
  102. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
  103. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
  104. at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.31.jar:9.0.31]
  105. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
  106. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
  107. at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.1.14.RELEASE.jar:5.1.14.RELEASE]
  108. at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.1.14.RELEASE.jar:5.1.14.RELEASE]
  109. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
  110. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
  111. at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.1.14.RELEASE.jar:5.1.14.RELEASE]
  112. at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.1.14.RELEASE.jar:5.1.14.RELEASE]
  113. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
  114. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
  115. at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:94) ~[spring-web-5.1.14.RELEASE.jar:5.1.14.RELEASE]
  116. at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.1.14.RELEASE.jar:5.1.14.RELEASE]
  117. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
  118. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
  119. at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.1.14.RELEASE.jar:5.1.14.RELEASE]
  120. at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.1.14.RELEASE.jar:5.1.14.RELEASE]
  121. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
  122. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
  123. at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
  124. at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-9.0.31.jar:9.0.31]
  125. at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) [tomcat-embed-core-9.0.31.jar:9.0.31]
  126. at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) [tomcat-embed-core-9.0.31.jar:9.0.31]
  127. at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.31.jar:9.0.31]
  128. at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) [tomcat-embed-core-9.0.31.jar:9.0.31]
  129. at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat-embed-core-9.0.31.jar:9.0.31]
  130. at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:367) [tomcat-embed-core-9.0.31.jar:9.0.31]
  131. at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) [tomcat-embed-core-9.0.31.jar:9.0.31]
  132. at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) [tomcat-embed-core-9.0.31.jar:9.0.31]
  133. at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1639) [tomcat-embed-core-9.0.31.jar:9.0.31]
  134. at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.31.jar:9.0.31]
  135. at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na:1.8.0_221]
  136. at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [na:1.8.0_221]
  137. at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.31.jar:9.0.31]
  138. at java.lang.Thread.run(Unknown Source) [na:1.8.0_221]

答案1

得分: 2

我认为描述字段没有填写

我猜您正在使用Thymeleaf,所以请确保将您的控制器与表单进行映射,如下所示:

  1. <form th:action="@{/save}" th:object="${job}" method="POST">
  2. <table border="0" cellpadding="10">
  3. <tr>
  4. <td>Description: </td>
  5. <td><input type="text" th:field="*{description}" /></td>
  6. </tr>
  7. <tr>
  8. <td>Price: </td>
  9. <td><input type="text" th:field="*{price}" /></td>
  10. </tr>
  11. <tr>
  12. <td colspan="2"><button type="submit">Save</button> </td>
  13. </tr>
  14. </table>
  15. </form>

在您的控制器中将 job ==> Job:

  1. @RequestMapping(value = "/save", method = RequestMethod.POST)
  2. public String saveJob(@ModelAttribute("job") Job job) {
  3. service.save(job);
  4. return "redirect:/";
  5. }
英文:

I think the description field is not filled

I suppose you are using Thymeleaf, so make sure to map your controller with your form like:

  1. &lt;form th:action=&quot;@{/save}&quot; th:object=&quot;${job}&quot; method=&quot;POST&quot;&gt;
  2. &lt;table border=&quot;0&quot; cellpadding=&quot;10&quot;&gt;
  3. &lt;tr&gt;
  4. &lt;td&gt;Description: &lt;/td&gt;
  5. &lt;td&gt;&lt;input type=&quot;text&quot; th:field=&quot;*{description}&quot; /&gt;&lt;/td&gt;
  6. &lt;/tr&gt;
  7. &lt;tr&gt;
  8. &lt;td&gt; Price: &lt;/td&gt;
  9. &lt;td&gt;&lt;input type=&quot;text&quot; th:field=&quot;*{price}&quot; /&gt;&lt;/td&gt;
  10. &lt;/tr&gt;
  11. &lt;tr&gt;
  12. &lt;td colspan=&quot;2&quot;&gt;&lt;button type=&quot;submit&quot;&gt;Save&lt;/button&gt; &lt;/td&gt;
  13. &lt;/tr&gt;
  14. &lt;/table&gt;
  15. &lt;/form&gt;

In your Controller change job ==> Job

  1. @RequestMapping(value = &quot;/save&quot;, method = RequestMethod.POST)
  2. public String saveJob(@ModelAttribute(&quot;job&quot;) job job) {
  3. service.save(job);
  4. return &quot;redirect:/&quot;;}
  5. }

答案2

得分: 0

如果您想通过名称访问模型属性,就像@ModelAttribute("job")一样,您必须在视图的<form>元素中设置相同的名称。目前,您没有将它包装在<form>中,而只是一个HTML<table>

以下内容应该可行:

  1. <form action="#" th:action="@{/save}" modelAttribute="job" th:object="${job}" method="POST">
  2. <label>Description:</label>
  3. <input type="text" class="input" th:field="*{description}">
  4. <label>Price</label>
  5. <input type="text" class="input" th:field="*{price}">
  6. <input type="submit" class="btn" value="Save">
  7. </form>

上面的代码可能外观不太好,但在技术上应该是可行的,您可以进一步调整它以使其显示正确。

英文:

If you want to access a model attribute by name like @ModelAttribute(&quot;job&quot;) you have to set the same name in the &lt;form&gt; element of your view. Right now you don't wrap it inside a &lt;form&gt; and just an HTML &lt;table&gt;

The following should work:

  1. &lt;form action=&quot;#&quot; th:action=&quot;@{/save}&quot; modelAttribute=&quot;job&quot; th:object=&quot;${job}&quot; method=&quot;POST&quot;&gt;
  2. &lt;label&gt;Description:&lt;/label&gt;
  3. &lt;input type=&quot;text&quot; class=&quot;input&quot; th:field=&quot;*{description}&quot;&gt;
  4. &lt;label&gt;Price&lt;/label&gt;
  5. &lt;input type=&quot;text&quot; class=&quot;input&quot; th:field=&quot;*{price}&quot;&gt;
  6. &lt;input type=&quot;submit&quot; class=&quot;btn&quot; value=&quot;Save&quot;&gt;
  7. &lt;/form&gt;

The code above might not have the best look-and-feel but should technically work and you can adjust it further to look properly

huangapple
  • 本文由 发表于 2020年3月17日 03:42:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/60712273.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定