commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From S├ębastien GALLET <sebast...@gallet.info>
Subject [commons-validator] Validator outside of Struts
Date Wed, 16 Feb 2005 17:28:07 GMT
Hello,
I try to use commons validator oustide of struts.
I've create a class ValidatorTest with the 2 main following methods :

    /** The default construtor
    *
    */
    public ValidatorTest() {

        InputStream in = null;
        System.out.println("Try to init validator");
       
        try {
            in = this.getClass().getResourceAsStream("validatortest.xml");
            resources = new ValidatorResources(in);
        } catch (Exception e) {
            System.out.println("Exception when initialising validator");
            System.out.println(e.getMessage());
            e.printStackTrace(System.out);
        } finally {
            // Make sure we close the input stream.
            if (in != null) {
                try { in.close();} catch (Exception f) {};
            }
        }
    }
   
    /** Validate the POJO.
    *
    */
    public boolean validate(Object obj) {
        boolean result = true;
        System.out.println("Try to validate bean 
"+obj.getClass().getName());
        try {
            Validator _validator = new Validator(resources, 
obj.getClass().getName());
            _validator.setParameter(Validator.BEAN_PARAM, obj);
//            _validator.setOnlyReturnErrors(true);
           
            results = null;         
            results = _validator.validate();
        } catch (Exception e) {
            result=false;
            System.out.println("Exception when validating bean 
"+obj.getClass().getName());
            System.out.println(e.getMessage());
            e.printStackTrace(System.out);
        }
        return result;
    }

And a test code which follows :

           ValidatorTest   hvalidate = new ValidatorTest();
            out.print("<p>Try to validate person (titandb-test 
name11,titandb-test name12) : ...");
            out.print("<p>Validation must be ok");
            try {
                Person person = new Person();
                person.setName1("titandb-test name11");
                person.setName2("titandb-test name12");
                boolean res=hvalidate.validate(person);
                out.println("......"+res+"</p>");
            }
            catch (Exception e) {
                out.println("</p><h3>Failed.</h3>");
                out.println("<p>Exception during tests :</p>");
                out.println(e.getMessage());
            }         

The validator configuration is ok as I can create validator instance.
But when I try to validate the data, I've got the following exception
2005-02-16 17:36:37,766 INFO  [STDOUT] Exception when validating bean 
info.gallet.titandb.person.Person
2005-02-16 17:36:37,766 INFO  [STDOUT] No ClassLoaders found for: 
org.apache.commons.validator.TestValidator

What's wrong in my code ???
TIA

Sebastien
which tries to speak English

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


Mime
View raw message