gpt4 book ai didi

java - Spring MVC 无效日期格式错误消息未显示

转载 作者:行者123 更新时间:2023-11-29 09:19:06 24 4
gpt4 key购买 nike

我在 Spring MVC 3 中遇到日期验证问题

客户端窗体.java

 public class ClientForm
{
private Date bday = new Date();

//Getters and setters
}

在我的 Controller 中

    @RequestMapping(value = "/save", method = RequestMethod.POST)
public String save(@Valid ClientForm form, BindingResult result)
{
if(result.hasErrors())
{
return "client.form";
}

return "redirect:search";
}

消息.属性

client.search.notnumber=Search value must be a number
typeMismatch.java.util.Date = Invalid date

jsp

<form:form action="save.html" method="post" commandName="clientForm">
<form:input path="bday" cssClass="date-pick dp-applied" />
<form:errors path="bday" element="label" cssClass="error"/>
</form:form>

“messages.properties”中的其他验证消息工作正常但是当我故意输入无效日期(即 111/12/2011)时

我收到了这个错误信息

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors Field error in object 'clientForm' on field 'bday': rejected value [21/05e/2011]; codes [typeMismatch.clientForm.bday,typeMismatch.bday,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [clientForm.bday,bday]; arguments []; default message [bday]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'bday'; nested exception is org.springframework.core.convert.ConversionFailedException: Unable to convert value "21/05e/2011" from type 'java.lang.String' to type 'java.util.Date'; nested exception is java.lang.IllegalArgumentException: Invalid format: "21/05e/2011" is malformed at "e/2011"] org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:656) org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560) javax.servlet.http.HttpServlet.service(HttpServlet.java:637) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:359) org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:275) org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90) org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83) org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:344) org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:272) org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:81) org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237) org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167) root cause org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors Field error in object 'clientForm' on field 'bday': rejected value [21/05e/2011]; codes [typeMismatch.clientForm.bday,typeMismatch.bday,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [clientForm.bday,bday]; arguments []; default message [bday]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'bday'; nested exception is org.springframework.core.convert.ConversionFailedException: Unable to convert value "21/05e/2011" from type 'java.lang.String' to type 'java.util.Date'; nested exception is java.lang.IllegalArgumentException: Invalid format: "21/05e/2011" is malformed at "e/2011"] org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doBind(HandlerMethodInvoker.java:810) org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:359) org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:153) org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426) org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644) org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560) javax.servlet.http.HttpServlet.service(HttpServlet.java:637) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:359) org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:275) org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90) org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83) org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:344) org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:272) org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:81) org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237) org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)

最佳答案

这是一个绑定(bind)异常。你有没有定义一个 Binder ?如果是,您是否注册了日期编辑器?也许你应该检查那个编辑器,你可能在那里有错字......日期中的'e'来自哪里?对我来说这似乎是一个转换错误。

关于java - Spring MVC 无效日期格式错误消息未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7552862/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com