Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
425 views
in Technique[技术] by (71.8m points)

java - Datepicker with time validation in Struts 2

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 Struts2 jquery datepicker passing 0 or null value to action class

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

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

  ...
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...