91

After the JSF 2 big support for annotations, I'm wondering what I would use the faces-config.xml for. What is its importance now?

In other words, what are the configurations that can only be done through faces-config.xml and not via annotations?

Right now all what I am using it for is to declare Spring's EL resolver.

<?xml version="1.0" encoding="UTF-8"?>
<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">

    <application>
        <el-resolver>
            org.springframework.web.jsf.el.SpringBeanFacesELResolver
        </el-resolver>
    </application> 
</faces-config>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Mahmoud Saleh
  • 33,303
  • 119
  • 337
  • 498

1 Answers1

142

It's still to be used for many things which can't be annotated. E.g. custom JSF validation messages:

<application>
    <message-bundle>com.example.i18n.messages</message-bundle>
</application>

A global i18n bundle (so that you don't need to declare <f:loadBundle> in every view):

<application>
    <resource-bundle>
        <base-name>com.example.i18n.Text</base-name>
        <var>text</var>
    </resource-bundle>
</application>

Explicitly supported i18n locales (so that the not-declared ones will be ignored even though there's a message bundle or resource bundle for it):

<application>
    <locale-config>
        <default-locale>en</default-locale>
        <supported-locale>nl</supported-locale>
        <supported-locale>es</supported-locale>         
        <supported-locale>de</supported-locale>         
    </locale-config>
</application>

Custom view handlers:

<application>
    <view-handler>com.example.SomeViewHandler</view-handler>
</application>

Phase listeners (there's still no annotation for that):

<lifecycle>
    <phase-listener>com.example.SomePhaseListener</phase-listener>
</lifecycle>

Managed beans which can't be annotated (the below one gives current Date on #{now}):

<managed-bean>
    <description>Current date and time</description>
    <managed-bean-name>now</managed-bean-name>
    <managed-bean-class>java.util.Date</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
</managed-bean>

Custom factories, such as custom exception handler factory (it also allows factories for FacesContext, ExternalContext, LifeCycle and many more so that you can provide your custom implementation):

<factory>
    <exception-handler-factory>com.example.SomeExceptionHandlerFactory</exception-handler-factory>
</factory>

To name only the commonly used ones. If you have faces-config.xml tag autocompletion in your IDE, you can find them all out. Only the managed beans, validators, converters, components, renderers and point-to-point navigation cases are not needed anymore thanks to the new annotations and implicit navigation.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 7
    @Matt: I've had a project where a `java.util.HashMap` as `#{components}` is stored in request scope to have a better declarative overview of all component bindings. E.g. `binding="#{components.foo}"` so that it can be referenced as `#{components.foo}` which is more self-documenting and less risky (due to potential name clashes) than `binding="#{foo}"` and `#{foo}`. – BalusC Sep 28 '11 at 18:36
  • 2
    I was also looking for some annotation for specifying el-resolver. Now i think there is no way to specify these application properties via annotation..right..?? – Rup Majumder Dec 06 '13 at 11:56
  • @RupMajumder Where do you want to put such an annotation, I mean on what class? This property is spread on the whole application. – iozee Oct 10 '14 at 07:35
  • 1
    FWIW, when using Apache Deltaspike, the JsfPhaseListener annotation can be used for using phase listeners without the need for configuring them in faces-config.xml. – jpangamarca Jan 27 '16 at 22:38