Search code examples
jsffaceletsresourcebundlejsf-2.2

Different ways of declaring a resource bundle in JSF2.x


I am working on JSF2.2 at my work place. My faces-config.xml has the resource bundle tag which helps me to assign a variable to a property file and use the variable in a EL.

   <resource-bundle>
    <base-name>properties.common</base-name>
    <var>prop</var>
   </resource-bundle>

I also found another way of achieving this by using the f:loadBundle tag like this

<f:loadBundle basename="properties.common" var="prop"/>

But this is a localized solution, meaning I would have to write this in every page.

  1. Would this work if i define this in a template ? If yes, how do i achieve it.

  2. Is there any other way that i can declare the resource globally with a variable to be used in an EL(like in the case of faces-config.xml)


Solution

  • Would this work if i define this in a template ?

    Yes.


    If yes, how do i achieve it.

    Just do exactly as you said. Define it in a template.


    Is there any other way that i can declare the resource globally with a variable to be used in an EL(like in the case of faces-config.xml)

    Put it in request map yourself in (post)constructor of a request scoped bean which is referenced in the view.

    ResourceBundle bundle = ResourceBundle.getBundle("properties.common", facesContext.getViewRoot().getLocale());
    externalContext.getRequestMap().put("prop", bundle);
    

    It can even be referenced as a property of a request scoped bean and this guarantees construction of the bean even if it's not referenced elsewhere in the view.