【问题标题】:How to validate fromDate , toDate using <sx:datetimepicker> in struts2?如何在 struts2 中使用 <sx:datetimepicker> 验证 fromDate , toDate ?
【发布时间】:2012-02-22 05:33:34
【问题描述】:

我在 Jsp(struts2 应用程序)中为 fromDate 和 toDate 使用以下代码。

<s:label value="valid From* "/>
<sx:datetimepicker required="true" name="validFrom" displayFormat="dd/MM/yyyy"  />
<s:label value="Valid To * :" />
<sx:datetimepicker required="true" name="validTo" displayFormat="dd/MM/yyyy" />

它正在以指定格式生成日期。我想检查选定的日期值,如果 Todate 小于 Fromdate 必须显示错误消息。

谁能帮我解决这个问题。提前致谢。

【问题讨论】:

    标签: validation struts2 datetimepicker


    【解决方案1】:

    可能最直接的方法是实现 Validateable,这可能是通过扩展 ActionSupport 最容易做到的。请注意,我在下面将“validFrom”更改为“from”,将“validTo”更改为“to”。

    那么在你的行动中你会写...

    //Not tested
    public void Validate(){
       //it is obvious that if one of these conditions is true the other will be as well
       //this is just to show the typical case of building up multiple field errors
       if (to.before(from)){
          addFieldError("to", getText("To date must be later than from date."));
       }
       if (from.after(to)){
          addFieldError("from", getText("From date must be before to date."));
       }
    }
    

    由于我喜欢选择简单的主题,您会发现您可以使用http://struts.apache.org/2.2.1.1/docs/fielderror.html 获取字段错误,以防您想更好地控制错误消息的放置位置。

    有关验证的更多信息,请参阅:http://struts.apache.org/2.x/docs/validation.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-04
      相关资源
      最近更新 更多