Is there a way to avoid JSF catching Exceptions thrown by w810 validation methods and handle them the same way as Exceptions raised in other Bean methods?

In a JSF project I'm working on, Exceptions thrown by Beans are usually handled by JSF, redirecting the user to the error page, but when they occur inside the Bean's validation method, JSF handles them by displaying them in relative <h:message>

instead of the Exception tag.

I would like the Exceptions raised in the validator methods to be handled like other Exceptions from Beans. Is there a way to achieve this?

The kind of validation I use is the bean authentication method, for example in the JSF page:

<h:inputText value="#{Bean.field}" validator="#{Bean.validate}" />

      

and, backed by Bean code:

public void validate(FacesContext context, UIComponent component, Object value){
   // validation logic here
}

      

Thanks, Andrea

+3


source to share


1 answer


This is by design.

Convert this validator to a fully fledged class that implements Validator

. Any exceptions other than ValidatorException

thrown there will result in an HTTP 500 error.

eg.

public class MyValidator implements Validator {

    public void validate(FacesContext context, UIComponent component, Object value) {
        // validation logic here
    }

}

      

which you register as <validator>

infaces-config.xml



<validator>
    <validator-id>myValidator</validator-id>
    <validator-class>com.example.MyValidator</validator-class>
</validator>

      

and use the following inputs

<h:inputText ... validator="myValidator" />

      

or

<h:inputText ...>
    <f:validator validatorId="myValidator" />
</h:inputText>

      

+1


source







All Articles