简体   繁体   中英

Struts 2 validation

任何人都可以让我知道如何检查Struts 2 validation.xml中的开始日期>结束日期

If your using java.util.Date objects, this has worked for me.

    <validator type="expression">
        <param name="expression"><![CDATA[
                startDate.time < endDate.time
        ]]></param>
        <message>Start Date must be after End Date</message>
</validator>

I think you can do this with an OGNL expression validator . Something like:

   <validator type="expression">
     <param name="expression">startDate.before(endDate)</param>
     <message>Start Date must be after End Date</message>
   </validator>

Since you're performing validation on two fields, one approach is to create a custom validator that operates on an entire object (eg User) that contains start and end date, as opposed to validating the fields independently. Then you could just do some simple date-comparison arithmetic in there.

The general term they use is non-field validation (or domain validation), which is covered here

There's a nice example in the source code for the Manning book Struts 2 in Action , chapter 10.

Do you know Hibernate Validator Framework ? This is a great validation framework, based on annotations.

And there is a Plugin for Struts2:

Full hibernate Plugin : http://cwiki.apache.org/S2PLUGINS/full-hibernate-plugin.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM