<i id='h3Qak'><tr id='h3Qak'><dt id='h3Qak'><q id='h3Qak'><span id='h3Qak'><b id='h3Qak'><form id='h3Qak'><ins id='h3Qak'></ins><ul id='h3Qak'></ul><sub id='h3Qak'></sub></form><legend id='h3Qak'></legend><bdo id='h3Qak'><pre id='h3Qak'><center id='h3Qak'></center></pre></bdo></b><th id='h3Qak'></th></span></q></dt></tr></i><div id='h3Qak'><tfoot id='h3Qak'></tfoot><dl id='h3Qak'><fieldset id='h3Qak'></fieldset></dl></div>
  1. <tfoot id='h3Qak'></tfoot>
    <legend id='h3Qak'><style id='h3Qak'><dir id='h3Qak'><q id='h3Qak'></q></dir></style></legend>

      <bdo id='h3Qak'></bdo><ul id='h3Qak'></ul>

      <small id='h3Qak'></small><noframes id='h3Qak'>

      使用 JSR303 验证 Spring MVC 日期格式

      Spring MVC Date format validation with JSR303(使用 JSR303 验证 Spring MVC 日期格式)
      • <i id='MI3aJ'><tr id='MI3aJ'><dt id='MI3aJ'><q id='MI3aJ'><span id='MI3aJ'><b id='MI3aJ'><form id='MI3aJ'><ins id='MI3aJ'></ins><ul id='MI3aJ'></ul><sub id='MI3aJ'></sub></form><legend id='MI3aJ'></legend><bdo id='MI3aJ'><pre id='MI3aJ'><center id='MI3aJ'></center></pre></bdo></b><th id='MI3aJ'></th></span></q></dt></tr></i><div id='MI3aJ'><tfoot id='MI3aJ'></tfoot><dl id='MI3aJ'><fieldset id='MI3aJ'></fieldset></dl></div>

        <small id='MI3aJ'></small><noframes id='MI3aJ'>

              <tbody id='MI3aJ'></tbody>
            <tfoot id='MI3aJ'></tfoot>

                <bdo id='MI3aJ'></bdo><ul id='MI3aJ'></ul>
                <legend id='MI3aJ'><style id='MI3aJ'><dir id='MI3aJ'><q id='MI3aJ'></q></dir></style></legend>
                本文介绍了使用 JSR303 验证 Spring MVC 日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在使用带有 JSR303 的 Spring MVC 进行输入验证.

                I'm using Spring MVC with JSR303 to do my input validation.

                我创建的表单有几个日期字段,这些字段绑定到支持表单的对象中的 Date 对象.我正在使用 JSR303 使用 @FutureDate 进行验证.我也在使用 @DateTimeFormat(pattern="dd/MM/yyyy"),(我知道这不是验证).

                A form I've created has a couple of date fields that are bound to Date objects within the object backing the form. I'm using JSR303 to do the validation for the Date using @Future. I'm also using @DateTimeFormat(pattern="dd/MM/yyyy"), (I know it's not validation).

                如何验证表单上 String 的日期格式?如果我将其他必填字段留空 (@NotEmpty) 并以dd/MM/yy"形式输入无效日期,它会在重新转换为dd/MM/yyyy"演示文稿(例如 12/03/12 重新呈现为 12/03/0012).这意味着我将在我的系统中获得 duff 数据.如果我输入aaa",我会得到一个转换异常.格式正确的 String 被转换为 Date 对象.

                How do I validate the date format of the String on the form? If I leave the other required fields blank (@NotEmpty) and enter a non-valid date in the form 'dd/MM/yy' it gets converted to 'dd/MM/yyyy' on re-presentation (e.g. 12/03/12 is re-presented as 12/03/0012). Which means I will get duff data in my system. If I enter "aaa" for I get a conversion Exception. Correctly formatted Strings get converted to Date objects.

                Date 字段的必填字段"注释还应该是 @NotNull 还是 @NotEmpty?

                Additionally should the 'required field' annotation for Date fields be @NotNull or @NotEmpty?

                非常感谢您提供的任何建议.

                Many thanks in advance for any advice provided.

                推荐答案

                谢谢拉尔夫.我做了一些进一步的挖掘并想出了这个(在我的表单控制器中):

                Thanks Ralph. I did some further digging around and came up with this (Which goes in my form controller):

                    @InitBinder
                    public void initBinder(WebDataBinder binder) {
                
                
                    String format = "dd/MM/yyyy";
                    SimpleDateFormat dateFormat = new SimpleDateFormat(format);
                    dateFormat.setLenient(false);
                    CustomDateEditor customDateEditor = new CustomDateEditor(dateFormat,true,format.length());
                
                    binder.registerCustomEditor(Date.class, customDateEditor);
                    }
                

                属性文件具有以下键:

                typeMismatch.java.util.Date :一些很好的平静的安慰信息,以帮助所有疏忽的用户

                typeMismatch.java.util.Date : Some nice calm reassuring message to assist all negligent users

                也许还有其他一些方法可以做到这一点,但现在就可以了.

                Maybe there are some other ways to do this but this will do for now.

                这篇关于使用 JSR303 验证 Spring MVC 日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)

                <i id='CFJ8G'><tr id='CFJ8G'><dt id='CFJ8G'><q id='CFJ8G'><span id='CFJ8G'><b id='CFJ8G'><form id='CFJ8G'><ins id='CFJ8G'></ins><ul id='CFJ8G'></ul><sub id='CFJ8G'></sub></form><legend id='CFJ8G'></legend><bdo id='CFJ8G'><pre id='CFJ8G'><center id='CFJ8G'></center></pre></bdo></b><th id='CFJ8G'></th></span></q></dt></tr></i><div id='CFJ8G'><tfoot id='CFJ8G'></tfoot><dl id='CFJ8G'><fieldset id='CFJ8G'></fieldset></dl></div>
                  <tbody id='CFJ8G'></tbody>
                  <tfoot id='CFJ8G'></tfoot>
                  <legend id='CFJ8G'><style id='CFJ8G'><dir id='CFJ8G'><q id='CFJ8G'></q></dir></style></legend>

                  • <small id='CFJ8G'></small><noframes id='CFJ8G'>

                      <bdo id='CFJ8G'></bdo><ul id='CFJ8G'></ul>