在表单中输入日期时抛出af/spring错误

oxf4rvwz  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(237)

当我试图将从MyThymeleaf html表单输入的日期保存到控制器时,收到一条错误消息。它似乎是作为字符串从表单发送的,但我希望它作为日期发送。我设置input type=date,所以我不知道为什么会出错。这是错误:

Validation failed for object='card'. Error count: 1
org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'card' on field 'expirationDate': rejected value [2021-12-12]; codes [typeMismatch.card.expirationDate,typeMismatch.expirationDate,typeMismatch.java.util.Date,typeMismatch]; 
 arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [card.expirationDate,expirationDate]; 
 arguments []; default message [expirationDate]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'expirationDate'; 
 nested exception is org.springframework.core.convert.ConversionFailedException: 

 Failed to convert from type [java.lang.String] to type [@javax.persistence.Temporal @javax.persistence.Column java.util.Date] for value '2021-12-12'; nested exception is java.lang.IllegalArgumentException]
    at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:170)
    at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121)
    at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:170)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:894)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1060)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:962)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:652)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
    at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:357)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:893)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1707)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.base/java.lang.Thread.run(Thread.java:834)

下面是该部分的html代码,expirationdate是我发送回控制器的card对象的一个属性:

<div class="col-12 mb-2">
                        <label for="exp" id="expLabel"><strong>Expiration date:</strong></label>
                        <input type="date" class="form-control" id="exp"
                               name="exp" value="2022-01-01" th:field="*{expirationDate}">
                    </div>

这是控制器。获取日期值并在表单中预先填充它可以正常工作,但是由于某种原因,当我提交表单时,它抛出了错误:

@PostMapping(value={"/editprofile", "editprofile.html"})
public String editUser(@ModelAttribute("user") User user,
                       @ModelAttribute("card") PaymentCard card,
                       @ModelAttribute("address") Address address) {
    // process and validate form data -- similar to createAccount process
    userRepo.save(user);
    //cardRepo.save(card); // even with this line commented, there is the error.
    addressRepo.save(address);

    return "redirect:/editprofile";
}

以下是pojo定义的第一部分:

@Entity
@Table(name = "payment_card", catalog = "bookstore")
public class PaymentCard implements java.io.Serializable {

    @Id
    @Column(name = "PaymentCardID", unique = true, nullable = false)
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int paymentCardId;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "UserID")
    private User user;

    @Column(name = "cardNumber", nullable = false)
    private String cardNumber; // int and long aren't big enough to hold 16 digits

    @Column(name = "cardType", nullable = false, length = 20)
    private String cardType;

    @Temporal(TemporalType.DATE)
    @Column(name = "expirationDate", nullable = false, length = 10)
    private Date expirationDate;
bt1cpqcv

bt1cpqcv1#

你用错了日期包。
使用此选项:

import java.sql.Date;

相关问题