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

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

        • <bdo id='Aj8SS'></bdo><ul id='Aj8SS'></ul>
      1. <legend id='Aj8SS'><style id='Aj8SS'><dir id='Aj8SS'><q id='Aj8SS'></q></dir></style></legend>

        Struts 2 中具有时间验证的日期选择器

        Datepicker with time validation in Struts 2(Struts 2 中具有时间验证的日期选择器)
      2. <legend id='2bePz'><style id='2bePz'><dir id='2bePz'><q id='2bePz'></q></dir></style></legend>
          <tbody id='2bePz'></tbody>
        <tfoot id='2bePz'></tfoot>
        <i id='2bePz'><tr id='2bePz'><dt id='2bePz'><q id='2bePz'><span id='2bePz'><b id='2bePz'><form id='2bePz'><ins id='2bePz'></ins><ul id='2bePz'></ul><sub id='2bePz'></sub></form><legend id='2bePz'></legend><bdo id='2bePz'><pre id='2bePz'><center id='2bePz'></center></pre></bdo></b><th id='2bePz'></th></span></q></dt></tr></i><div id='2bePz'><tfoot id='2bePz'></tfoot><dl id='2bePz'><fieldset id='2bePz'></fieldset></dl></div>
          <bdo id='2bePz'></bdo><ul id='2bePz'></ul>

          <small id='2bePz'></small><noframes id='2bePz'>

                  本文介绍了Struts 2 中具有时间验证的日期选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在使用 Struts2 JQuery datepicker 标记.

                  但我无法验证它.在我的 validation.xml 文件中,我将其作为 date,但它删除了时间部分.有什么解决办法吗?我应该使用正则表达式来验证我的日期时间吗?

                  But I'm not able to validate it. In my validation.xml file, i am taking it as date, but it removes the time part. Any solution to it? Should i use regex to validate my date time?

                  JSP 表单

                  代码与我之前的问题中的代码有些相同Struts2 jquery datepicker 将 0 或 null 值传递给动作类

                  code is somewhat same as it was is in my earlier question Struts2 jquery datepicker passing 0 or null value to action class

                  动作类:

                  public Date getDateAndTimeOfOccurance() {
                      return dateAndTimeOfOccurance;
                  }
                  
                  public void setDateAndTimeOfOccurance(Date dateAndTimeOfOccurance) {
                      System.out.println(dateAndTimeOfOccurance);
                      this.dateAndTimeOfOccurance = dateAndTimeOfOccurance;
                  }
                  
                  public String execute() throws Exception {
                    .
                    .
                       
                    ps.setTimestamp(13, new java.sql.Timestamp(getDateAndTimeOfOccurance().getTime())); 
                    /*    i have modified my database because the time values in java.sql.Date have been depreciated  */
                        
                    .
                    .
                  }
                  

                  是否有任何现有的解决方案?我应该使用 regex 验证器吗?

                  is there any existing solution for it? Should i use regex validator?

                  更新

                  为了实验,我去掉了验证部分,但动作中收到的时间仍然是00:00:00.

                  For experiment, I removed the validation part but the time received in the action is still 00:00:00.

                  更新 2我找到了一种方法来解决这个问题,方法是收到这个问题的评论中提到的 Struts2 JQuery日期选择器无法正常工作.我也删除了validation.xml.现在我正在使用注释.现在的问题是如何验证它?它应该是 regex 还是有一种将字符串验证为日期的 struts 验证方式(例如使用 validate() 方法,我不确定它是否好用练习).

                  UPDATE 2 i found a way to solve this problem by recieving as mentioned in comments of of this question Struts2 JQuery Datepicker not working properly. Also i have dropped validation.xml. Now i'm using annotation. Now the problem is how to validate it? should it be a regex or is there a struts validation way of validating a string as a date (like using validate() method, i'm not sure is it a good practice).

                  更新代码(更新 2)

                  动作类

                  @RequiredStringValidator(message = "Please enter the date and time of occurance")
                  public String getDateAndTimeOfOccurance() {
                      return dateAndTimeOfOccurance;
                  }
                  
                  public void setDateAndTimeOfOccurance(String dateAndTimeOfOccurance) {
                      this.dateAndTimeOfOccurance = dateAndTimeOfOccurance;
                  }
                  
                  public void execute(){
                    ....
                    Date d = null;
                    try {
                        d = new SimpleDateFormat("dd-MMM-yyyy hh:mm", Locale.getDefault()).parse(getDateAndTimeOfOccurance());
                    } catch (java.text.ParseException e) {
                        e.printStackTrace();
                        addFieldError(dateAndTimeOfOccurance, "Please enter a valid date");
                        return INPUT;
                    }
                    Timestamp t = new java.sql.Timestamp(d.getTime());
                    ps.setTimestamp(13, t);
                  
                    ...
                  }
                  

                  推荐答案

                  使用 validate() 来验证您的操作代码是一个很好的做法.这种类型的验证称为 programmatic 验证,并在 验证拦截器.您可以配置此拦截器以执行默认设置的各种验证.因此,通过覆盖 validate 方法与 declarative 验证来使用编程验证是您可以在您的操作中合法执行的一种方式.当然,您可以通过同一个拦截器进行两种类型的验证.该框架支持核心包使用的许多基本验证器,所有这些都在 捆绑的验证器.您还可以通过提供自定义验证器来扩展框架.用于验证两个字段的自定义验证器的经典示例.

                  Using validate() to validate your action code is a good practice. This type of validation is known as programmatic validation and is described in he Struts documentation under the validation interceptor. You can configure this interceptor to perform all kinds of validations which is the default setting. So, to use a programmatic validation by overriding a validate method vs declarative validation is a way you can do legally in your action. Of course you can do both types of validations via the same interceptor. The framework has support for many basic validators used by the core package, all of them are described under the section of bundled validators. You can also extend the framework by providing your custom validators. The classic example of the custom validator for validation of two fields.

                  无论您选择哪种类型的验证,都不应在操作方法中进行(除非您有理由这样做,并且您知道自己在做什么),因为您可以使用框架功能和验证框架将验证逻辑与控制器逻辑分离(假设业务逻辑在服务层执行).

                  Regardless of whichever type of validation you choose, you shouldn't do it in the action method (unless you have a reason to do it, and you know what are you doing) because you can use the framework features and the validation framework to separate the validation logic from the controller logic (assumed that business logic is performed on the service layer).

                  您应该修复的代码:

                  protected static SimpleDateFormat getDateFormat(){
                    return new SimpleDateFormat("dd-MMM-yyyy hh:mm");
                  }
                  
                  @Override
                  public void validate(){
                    try {
                      SimpleDateFormat df = getDateFormat();
                      df.parse(dateAndTimeOfOccurance);
                    } catch (java.text.ParseException e) {
                      e.printStackTrace();
                      addFieldError(dateAndTimeOfOccurance, "Please enter a valid date"); 
                    }
                  }
                  
                  
                  @Override
                  public void execute() throws Exception {
                    SimpleDateFormat df = getDateFormat();
                    Date d = df.parse(dateAndTimeOfOccurance);
                    Timestamp t = new java.sql.Timestamp(d.getTime());
                    ps.setTimestamp(13, t);
                    ...
                    return SUCCESS;
                  }
                  

                  这段代码有点笨拙,因为它需要解析一个字符串字段两次(如果您想从字符串字段中获取 Date 值,则可以多次解析).此外,无论上下文的语言环境如何,它都使用固定格式模式.

                  This code a bit clumsy because it needs to parse a string field twice (and as many times as needed if you want to get a Date value from the string field). Also, It used a fixed format pattern regardless of the context's locale.

                  这篇关于Struts 2 中具有时间验证的日期选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Java Bytecode Manipulation Library Suggestions(Java 字节码操作库建议)
                  Java CLI UI-design: frameworks or libraries?(Java CLI UI 设计:框架还是库?)
                  About the use of Beans.xml configuration file in Spring Framework application(关于Spring Framework应用中Beans.xml配置文件的使用)
                  What is the difference between Spring, Struts, Hibernate, JavaServer Faces, Tapestry?(Spring、Struts、Hibernate、JavaServer Faces、Tapestry 有什么区别?)
                  Are there any android application framework like spring?(有没有像spring这样的android应用程序框架?)
                  Java Swing based game framework. Any advice?(基于 Java Swing 的游戏框架.有什么建议吗?)

                    1. <tfoot id='RR4Om'></tfoot>

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

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