Search code examples
jsffaceletsjsf-2.3

JSF: How to throw an exception from within a facelet


Is there a way to throw an exception from within a facelet? Background: I've a component using a customised converter as parameter. So I call the component as

@Named
@RequestScoped
public class CustomConverter implements Converter<String>
...
...
<ui:param
  name="pConverter"
  value="#{customConverter}" />
...

But when there's a typo in the param value e.g.

...
<ui:param
  name="pConverter"
  value="#{custoMConverter}" /> <!-- custoMConverter instead of customConverter -->
...

the converter is consequently not working but no exception is thrown. Now I would like to throw an exception from within the component if the converter is empty in such a case. How to do that?

Why is a non existent element not throwing an exception at all?

<h:outputText value="#{fooBean.foo}" />

There's no exception if the bean fooBean doesn't exist. Why not?

Thanks in advance.


Solution

  • I've found a solution.

    @Named
    @ApplicationScoped
    public class ApplicationBean {
        public final void checkExistenceAndThrow(final Object o) {
            if (Objects.isNull(o)) {
                throw new NullPointerException();
            }
        }
    }
    

    Then in the component check for the existence of the converter:

    <h:outputText
      value="#{applicationBean.checkExistenceAndThrow(pConverter)}"
      style="display: none;" />
    

    So the throwing of the exception is delegated to the bean.