JEE7: Using Resources

×

Note:

  • Resource Bundles must be created under the resource directory

Create a new Resource Bundel with IntelliJ

Click on resources -> Right Click -> NEW -> Resource Bundle -> Provide a Bundle Name -> Add All Resources
Image resource_img1.jpg  NOT Found“;[/insert_php]

Verifiy Resource Bundle Creation

Directory Layout after resource Bundle Creation
Image resource_img2.jpg  NOT Found“;[/insert_php]

Configuring faces-config.xml under WEB-INF folder


<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
    http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">


        <application>

        <locale-config>
            <default-locale>de</default-locale>
        </locale-config>

        <resource-bundle>
            <base-name>backend</base-name>
            <var>backend</var>
        </resource-bundle>
    </application>
</faces-config>

Java Code testing Resource

@ManagedBean(name = "editor")
@SessionScoped
public class EditorBean {
..
    static  {
        System.out.println("Testing Resource Setup: ");
        try {
            Locale locale = Locale.getDefault();
            System.out.println("Testing Resource Setup - Current Locale: " + locale);
            ResourceBundle backend = ResourceBundle.getBundle("backend",locale);
            Enumeration  keys = backend.getKeys();
            while (keys.hasMoreElements()) {
                String key = keys.nextElement();
                String value = backend.getString(key);
                System.out.println("Testing Resource Setup:" + key + ": " + value);
            }
        } catch (Exception e ){ e.printStackTrace();}

    }
..

Logs:

  Testing Resource Setup: 
  Testing Resource Setup - Current Locale: en_US
  Testing Resource Setup:MALE: Herr
  Testing Resource Setup:FEMALE: Frau