Struts 2 中具有时间验证的日期选择器 [英] Datepicker with time validation in Struts 2

查看:40
本文介绍了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 中具有时间验证的日期选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆