I have a question here:
Scenario: I have a JSF-2, Spring (Beans wiring) Application. I have written a custom validator, which I want to execute.
@FacesValidator("com.test.vali")
@Named("com.test.vali")
public class TestValidator implements Validator {
@Override
public void validate(FacesContext arg0, UIComponent arg1, Object arg2) throws ValidatorException {
System.out.println("dhkdfkbhdfbkdfksdfdfk");
}
}
I was trying to inject the validator using the following ways:
Way#1:
<h:inputText value="#{helloWorld.name}">
<f:validator binding="#{com.test.vali}" />
</h:inputText>
Output
When tried to render the page, it threw an exception.
javax.servlet.ServletException: /testRichFaces.xhtml @17,48 <f:validator> A validator id was not specified. Typically the validator id is set in the constructor ValidateHandler(ValidatorConfig)
Searched a lot on this, and verified few ways like:
- Java file is in a package.
Way#2
<f:validator validatorId="com.test.vali" />
Output
javax.servlet.ServletException: Expression Error: Named Object: com.test.vali not found.
So from way#1 and way#2, I could interpret that none of the annotations were working for me.
Then, I tried to move to the last approach:
Way#3: Adding validator in faces-config.xml, just to show I am using 2.0 compliance:
<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">
And validator config is:
<validator>
<validator-id>com.test.vali</validator-id>
<validator-class>teet.TestValidator</validator-class>
</validator>
Output
Works
Now the question arises, even using JSF-2.0, I had to resort to faces-config.xml.
What might be the mistake am doing?
Let know if any more configurations are required.