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

huangapple 未分类评论47阅读模式
标题翻译

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

问题

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

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

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

<table border="0" cellpadding="10">
    <tr>
        <td>Description: </td>
        <td><input type="text" name="*{description}" /></td>
    </tr>
    <tr>
        <td>Price: </td>
        <td><input type="text" name="*{price}" /> </td>
    </tr>
    <tr>
        <td colspan="2"><button type="submit">Save</button> </td>
    </tr>
</table>

控制器:

package com.invoicebuilder;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.batch.BatchProperties.Job;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

@Controller
public class AppController {

    @Autowired
    private InvoiceService service;

    @RequestMapping("/")
    public String viewHomePage(Model model) {
        List<job> listJobs = service.listAll();
        model.addAttribute("listJobs", listJobs);

        return "index";
    }

    @RequestMapping("/new")
    public String showNewJobForm(Model model) {
        Job job = new Job();
        model.addAttribute("job", job);

        return "new_job";
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String saveJob(@ModelAttribute("job") job job) {
        service.save(job);

        return "redirect:/";
    }
}

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

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

(剩余部分省略)

英文翻译

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:

&lt;table border=&quot;0&quot; cellpadding=&quot;10&quot;&gt;
			&lt;tr&gt;
				&lt;td&gt;Description: &lt;/td&gt;
				&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;*{description}&quot; /&gt;&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
				&lt;td&gt; Price: &lt;/td&gt;
				&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;*{price}&quot; /&gt;&lt;/td&gt; 
			&lt;/tr&gt;
			&lt;tr&gt;
				&lt;td colspan=&quot;2&quot;&gt;&lt;button type=&quot;submit&quot;&gt;Save&lt;/button&gt; &lt;/td&gt;
			&lt;/tr&gt;
			
		&lt;/table&gt;

Controller:

/**
 * 
 */
package com.invoicebuilder;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.batch.BatchProperties.Job;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;


/**
 * @author jmens
 *
 */
@Controller
public class AppController {

    @Autowired
    private InvoiceService service;

    @RequestMapping(&quot;/&quot;)
    public String viewHomePage(Model model) {
	    List&lt;job&gt; listJobs = service.listAll();
	    model.addAttribute(&quot;listJobs&quot;, listJobs);
	
	    return &quot;index&quot;;
		
    }

    @RequestMapping(&quot;/new&quot;)
    public String showNewJobForm(Model model) {
	    Job	job = new Job();
	    model.addAttribute(&quot;job&quot;, job);
	
	    return &quot;new_job&quot;;
	
    }
    @RequestMapping(value = &quot;/save&quot;, method = RequestMethod.POST)
    public String saveJob(@ModelAttribute(&quot;job&quot;) job job) {
	service.save(job);
	
	return &quot;redirect:/&quot;;
    }
}`

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:

java.sql.SQLIntegrityConstraintViolationException: Column &#39;description&#39; cannot be null
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:117) ~[mysql-connector- 
java-8.0.19.jar:8.0.19]
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) ~[mysql-connector-java- 
8.0.19.jar:8.0.19]
at 
com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) ~ 
[mysql-connector-java-8.0.19.jar:8.0.19]
at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:953) ~ 
[mysql-connector-java-8.0.19.jar:8.0.19]
at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1092) 
~[mysql-connector-java-8.0.19.jar:8.0.19]
at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1040) 
~[mysql-connector-java-8.0.19.jar:8.0.19]
at com.mysql.cj.jdbc.ClientPreparedStatement.executeLargeUpdate(ClientPreparedStatement.java:1347) ~ 
[mysql-connector-java-8.0.19.jar:8.0.19]
at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdate(ClientPreparedStatement.java:1025) ~ 
[mysql-connector-java-8.0.19.jar:8.0.19]
at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeUpdate(ProxyPreparedStatement.java:61) ~ 
[HikariCP-3.2.0.jar:na]
at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeUpdate(HikariProxyPreparedStatement.java) ~[HikariCP-3.2.0.jar:na]
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:175) ~[hibernate-core-5.3.15.Final.jar:5.3.15.Final]
at org.hibernate.dialect.identity.GetGeneratedKeysDelegate.executeAndExtract(GetGeneratedKeysDelegate.java:57) ~[hibernate-core-5.3.15.Final.jar:5.3.15.Final]
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:42) ~[hibernate-core-5.3.15.Final.jar:5.3.15.Final]
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3090) ~[hibernate-core-5.3.15.Final.jar:5.3.15.Final]
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3683) ~[hibernate-core-5.3.15.Final.jar:5.3.15.Final]
at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:81) ~[hibernate-core-5.3.15.Final.jar:5.3.15.Final]
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:645) ~[hibernate-core-5.3.15.Final.jar:5.3.15.Final]
at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:282) ~[hibernate-core-5.3.15.Final.jar:5.3.15.Final]
at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:263) ~[hibernate-core-5.3.15.Final.jar:5.3.15.Final]
at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:317) ~[hibernate-core-5.3.15.Final.jar:5.3.15.Final]
at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:332) ~[hibernate-core-5.3.15.Final.jar:5.3.15.Final]
at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:289) ~[hibernate-core-5.3.15.Final.jar:5.3.15.Final]
at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:196) ~[hibernate-core-5.3.15.Final.jar:5.3.15.Final]
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:127) ~[hibernate-core-5.3.15.Final.jar:5.3.15.Final]
at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:192) ~[hibernate-core-5.3.15.Final.jar:5.3.15.Final]
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:135) ~[hibernate-core-5.3.15.Final.jar:5.3.15.Final]
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:62) ~[hibernate-core-5.3.15.Final.jar:5.3.15.Final]
at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:804) ~[hibernate-core-5.3.15.Final.jar:5.3.15.Final]
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:789) ~[hibernate-core-5.3.15.Final.jar:5.3.15.Final]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_221]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_221]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_221]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_221]
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:351) ~[spring-orm-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at com.sun.proxy.$Proxy77.persist(Unknown Source) ~[na:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_221]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_221]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_221]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_221]
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:310) ~[spring-orm-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at com.sun.proxy.$Proxy77.persist(Unknown Source) ~[na:na]
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.save(SimpleJpaRepository.java:535) ~[spring-data-jpa-2.1.16.RELEASE.jar:2.1.16.RELEASE]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_221]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_221]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_221]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_221]
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]
at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:200) ~[spring-data-commons-2.1.16.RELEASE.jar:2.1.16.RELEASE]
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]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.14.RELEASE.jar:5.1.14.RELEASE]
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]
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]
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]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59) ~[spring-data-commons-2.1.16.RELEASE.jar:2.1.16.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:295) ~[spring-tx-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98) ~[spring-tx-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139) ~[spring-tx-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.14.RELEASE.jar:5.1.14.RELEASE]
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]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.14.RELEASE.jar:5.1.14.RELEASE]
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]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93) ~[spring-aop-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61) ~[spring-data-commons-2.1.16.RELEASE.jar:2.1.16.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) ~[spring-aop-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at com.sun.proxy.$Proxy79.save(Unknown Source) ~[na:na]
at com.invoicebuilder.InvoiceService.save(InvoiceService.java:20) ~[classes/:na]
at com.invoicebuilder.AppController.saveJob(AppController.java:46) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_221]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_221]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_221]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_221]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105) ~[spring-webmvc-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:893) ~[spring-webmvc-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:798) ~[spring-webmvc-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) ~[spring-webmvc-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) ~[spring-webmvc-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909) ~[spring-webmvc-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:660) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.31.jar:9.0.31]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:94) ~[spring-web-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-9.0.31.jar:9.0.31]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) [tomcat-embed-core-9.0.31.jar:9.0.31]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) [tomcat-embed-core-9.0.31.jar:9.0.31]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.31.jar:9.0.31]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) [tomcat-embed-core-9.0.31.jar:9.0.31]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat-embed-core-9.0.31.jar:9.0.31]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:367) [tomcat-embed-core-9.0.31.jar:9.0.31]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) [tomcat-embed-core-9.0.31.jar:9.0.31]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) [tomcat-embed-core-9.0.31.jar:9.0.31]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1639) [tomcat-embed-core-9.0.31.jar:9.0.31]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.31.jar:9.0.31]
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na:1.8.0_221]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [na:1.8.0_221]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.31.jar:9.0.31]
at java.lang.Thread.run(Unknown Source) [na:1.8.0_221]

答案1

得分: 2

我认为描述字段没有填写

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

<form th:action="@{/save}" th:object="${job}" method="POST">
    <table border="0" cellpadding="10">
        <tr>
            <td>Description: </td>
            <td><input type="text" th:field="*{description}" /></td>
        </tr>
        <tr>
            <td>Price: </td>
            <td><input type="text" th:field="*{price}" /></td>
        </tr>
        <tr>
            <td colspan="2"><button type="submit">Save</button> </td>
        </tr>
    </table>
</form>

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

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

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:

&lt;form th:action=&quot;@{/save}&quot; th:object=&quot;${job}&quot; method=&quot;POST&quot;&gt;
 &lt;table border=&quot;0&quot; cellpadding=&quot;10&quot;&gt;
   &lt;tr&gt;
     &lt;td&gt;Description: &lt;/td&gt;
     &lt;td&gt;&lt;input type=&quot;text&quot; th:field=&quot;*{description}&quot; /&gt;&lt;/td&gt;
   &lt;/tr&gt;
   &lt;tr&gt;
     &lt;td&gt; Price: &lt;/td&gt;
     &lt;td&gt;&lt;input type=&quot;text&quot; th:field=&quot;*{price}&quot; /&gt;&lt;/td&gt; 
   &lt;/tr&gt;
   &lt;tr&gt;
      &lt;td colspan=&quot;2&quot;&gt;&lt;button type=&quot;submit&quot;&gt;Save&lt;/button&gt; &lt;/td&gt;
   &lt;/tr&gt;
 &lt;/table&gt;
&lt;/form&gt;

In your Controller change job ==> Job

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

答案2

得分: 0

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

以下内容应该可行:

<form action="#" th:action="@{/save}" modelAttribute="job" th:object="${job}" method="POST">
    <label>Description:</label>
    <input type="text" class="input" th:field="*{description}">
    <label>Price</label>
    <input type="text" class="input" th:field="*{price}">

    <input type="submit" class="btn" value="Save">
</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:

&lt;form action=&quot;#&quot; th:action=&quot;@{/save}&quot; modelAttribute=&quot;job&quot; th:object=&quot;${job}&quot; method=&quot;POST&quot;&gt;
    &lt;label&gt;Description:&lt;/label&gt;
    &lt;input type=&quot;text&quot; class=&quot;input&quot; th:field=&quot;*{description}&quot;&gt;
    &lt;label&gt;Price&lt;/label&gt;
    &lt;input type=&quot;text&quot; class=&quot;input&quot; th:field=&quot;*{price}&quot;&gt;

  &lt;input type=&quot;submit&quot; class=&quot;btn&quot; value=&quot;Save&quot;&gt;
&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://java.coder-hub.com/60712273.html
匿名

发表评论

匿名网友

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

确定