You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Michael Nascimento Santos <mi...@hotmail.com> on 2004/03/03 16:42:17 UTC

JXPath Validator

Hi guys,

I am just wondering: you guys have developed validwhen so we could have a
more powerful validator, right? But, while using just commons-validator in a
GUI (non-web) project, I noticed I could implement a very powerful validator
using
JXPath.

Here is the code for the static method:

   public static boolean validateJXPath(Object bean, Field field) {
      final JXPathContext ctx = JXPathContext.newContext(bean);

      return
!Boolean.FALSE.equals(ctx.getValue(field.getVarValue("jxpath")));
   }

Notice it's just a simple validator with no dependencies on
HttpServletRequest or ActionErrors. I don't know that much about validwhen,
but from what I've seen, XPath expressions can do everything validwhen
expressions can and even more.

Am I missing something? Is there any reason why a JXPath-based validator as
the above wouldn't be suitable for Struts?

Regards,
Michael Nascimento Santos
JSR 207 Expert Group Member
http://today.java.net/pub/au/80
Sun Certified Programmer for the Java 2 Platform 1.4
Sun Certified Web Component Developer for J2EE
Moderador SouJava - http://www.soujava.org.br

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: JXPath Validator

Posted by Niall Pemberton <ni...@blueyonder.co.uk>.
Sounds good to me.

I don't know if the validator guys considered JXPath, but why don't you
submit an enhacement request using bugzilla to commons with your JXPath
validator attached (if you include JUnit tests you'll impress them even
more).

For some reason the 'validwhen' validator is part of struts (rather than
commons validator) - I don't know why this is. From the look of it, it could
have been implemented in commons with a method in struts to call it. From
what I can see the only reason validwhen needs the HttpServletRequest is so
that it can get error messages. Presumably to process error messages in the
same way in a struts environment, yours would need to do a similar thing -
but that could be handled with a validate method in struts which calls your
commons JXPath validator.

Niall

----- Original Message ----- 
From: "Michael Nascimento Santos" <mi...@hotmail.com>
To: "Struts Users Mailing List" <st...@jakarta.apache.org>
Sent: Wednesday, March 03, 2004 3:42 PM
Subject: JXPath Validator


> Hi guys,
>
> I am just wondering: you guys have developed validwhen so we could have a
> more powerful validator, right? But, while using just commons-validator in
a
> GUI (non-web) project, I noticed I could implement a very powerful
validator
> using
> JXPath.
>
> Here is the code for the static method:
>
>    public static boolean validateJXPath(Object bean, Field field) {
>       final JXPathContext ctx = JXPathContext.newContext(bean);
>
>       return
> !Boolean.FALSE.equals(ctx.getValue(field.getVarValue("jxpath")));
>    }
>
> Notice it's just a simple validator with no dependencies on
> HttpServletRequest or ActionErrors. I don't know that much about
validwhen,
> but from what I've seen, XPath expressions can do everything validwhen
> expressions can and even more.
>
> Am I missing something? Is there any reason why a JXPath-based validator
as
> the above wouldn't be suitable for Struts?
>
> Regards,
> Michael Nascimento Santos
> JSR 207 Expert Group Member
> http://today.java.net/pub/au/80
> Sun Certified Programmer for the Java 2 Platform 1.4
> Sun Certified Web Component Developer for J2EE
> Moderador SouJava - http://www.soujava.org.br
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
>
>



---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org