在Struts 2中具有时间验证的Datepicker [英] Datepicker with time validation in Struts 2

查看:34
本文介绍了在Struts 2中具有时间验证的Datepicker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 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表单

代码与我之前的问题 动作类:

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()来验证您的操作代码是一种好习惯.这种验证类型称为程序化验证,并在Struts文档的 验证拦截器.您可以配置此拦截器以执行所有类型的验证,这是默认设置.因此,通过覆盖validate方法与声明性验证来使用程序验证是一种可以合法地采取行动的方法.当然,您可以通过同一个拦截器进行两种类型的验证.该框架支持核心程序包使用的许多基本验证器,所有这些验证器均在捆绑的验证器.您还可以通过提供自定义验证器来扩展框架.自定义验证程序经典示例,用于验证两个字段.

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.

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

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

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