Struts 2 datepicker与时间验证 [英] Struts 2 datepicker with time validation

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

问题描述

我刚刚来到Struts2。我正在使用 Struts2 JQuery datepicker标签。但是我无法验证它。在我的 validation.xml 文件中,我把它当作 date ,但它会删除时间部分。任何解决方案吗?我应该使用正则表达式验证我的日期时间吗?



JSP表单



与我的早期版本相同问题



动作类

  public Date getDateAndTimeOfOccurance(){
return dateAndTimeOfOccurance;
}

public void setDateAndTimeOfOccurance(Date dateAndTimeOfOccurance){
System.out.println(dateAndTimeOfOccurance);
this.dateAndTimeOfOccurance = dateAndTimeOfOccurance;
}

public String execute()throws异常{



ps.setTimestamp(13,new java.sql.Timestamp(getDateAndTimeOfOccurance()。getTime()));
/ *我修改了我的数据库,因为java.sql.Date中的时间值已经被折旧* /



}

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



更新



对于实验,我删除了验证部分,但在操作中收到的时间仍然是 00:00:00



更新2
i找到一种方法来解决这个问题,如在正则表达式的注释中所提及的那样,或者有一个struts验证方式来验证一个字符串作为日期(如使用 validate()方法,我不知道这是否是一个好习惯)。



更新代码(更新2)



动作类

  @RequiredStringValidator(message =请输入发生日期和时间)
public String getDateAndTimeOfOccurance(){
return dateAndTimeOfOccurance;
}

public void setDateAndTimeOfOccurance(String dateAndTimeOfOccurance){
this.dateAndTimeOfOccurance = dateAndTimeOfOccurance;
}

public void execute(){
....
日期d = null;
try {
d = new SimpleDateFormat(dd-MMM-yyyy hh:mm,Locale.getDefault())。parse(getDateAndTimeOfOccurance());
} catch(java.text.ParseException e){
e.printStackTrace();
addFieldError(dateAndTimeOfOccurance,请输入有效的日期);
return INPUT;
}
Timestamp t = new java.sql.Timestamp(d.getTime());
ps.setTimestamp(13,t);

...
}


解决方案

使用 validate()验证您的操作代码是一个好习惯。这种类型的验证被称为程序化验证,并在他的Struts文档中描述了验证拦截器。您可以配置此拦截器来执行默认设置的各种验证。所以,通过覆盖一个验证方法vs 声明式使用编程验证是一种可以在您的操作中合法执行的方法。当然,您可以通过同一个拦截器进行两种类型的验证。该框架支持核心包使用的许多基本验证器,它们都在捆绑验证器。您还可以通过提供自定义验证器来扩展框架。用于验证两个字段的自定义验证器经典示例。



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



您应该修复的代码:

  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,请输入有效的日期);
}
}


@Override
public void execute()throws异常{
SimpleDateFormat df = getDateFormat();
日期d = df.parse(dateAndTimeOfOccurance);
Timestamp t = new java.sql.Timestamp(d.getTime());
ps.setTimestamp(13,t);
...
return SUCCESS;
}

这段代码有点笨拙,因为它需要解析一个字符串字段两次如果要从字符串字段获取 Date 值,则需要多次。此外,它使用固定的格式模式,而不管上下文的区域设置。


I'm new to Struts2. I'm using Struts2 JQuery datepicker tag. 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 Form

code is somewhat same as it was is in my earlier question

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  */

  .
  .
}

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

UPDATE

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

UPDATE 2 i found a way to solve this problem by recieving as mentioned in comments of 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).

UPDATED Code (with update 2)

Action class

@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);

  ...
}

解决方案

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).

The code you should fix:

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;
}

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天全站免登陆