My JSF Books/Videos My JSF Tutorials OmniFaces/JSF PPTs
JSF 2.3 Tutorial
JSF Caching Tutorial
JSF Navigation Tutorial
JSF Scopes Tutorial
JSF Page Author Beginner's Guide
OmniFaces 2.3 Tutorial Examples
OmniFaces 2.2 Tutorial Examples
JSF Events Tutorial
OmniFaces Callbacks Usages
JSF State Tutorial
JSF and Design Patterns
JSF 2.3 New Features (2.3-m04)
Introduction to OmniFaces
25+ Reasons to use OmniFaces in JSF
OmniFaces Validators
OmniFaces Converters
JSF Design Patterns
Mastering OmniFaces
Reusable and less-verbose JSF code

My JSF Resources ...

Java EE Guardian
Member of JCG Program
Member MVB DZone
Blog curated on ZEEF
OmniFaces is an utility library for JSF, including PrimeFaces, RichFaces, ICEfaces ...

[OmniFaces Utilities] - Find the right JSF OmniFaces 2 utilities methods/functions

Search on blog

Petition by Java EE Guardians

Twitter

luni, 29 iunie 2015

[OmniFaces utilities 2.1] Get all application resource bundles registered as <resource-bundle> in faces-config.xml


[OmniFaces utilities] The getResourceBundles() method returns all application resource bundles registered as <resource-bundle> in faces-config.xml. If there are no resource bundles registered, then this method just returns an empty map.

Method:
Usage:

Let's suppose that we have a resource bundle with "short" messages and a resource bundle with "full" messages:

Is quite simple, just check the content of the msgs.ShortMessages:

English and French (msgs.ShortMessages_en.properties and msgs.ShortMessages_fr.properties):


English and French (msgs.FullMessages_en.properties and msgs.FullMessages_fr.properties):


Now, in faces-config.xml, we declare both resource bundles, as below (for the "short" messages we have attached the var named, short, and for the "full" messages, the var named, full):

<application>
 <locale-config>
  <default-locale>en</default-locale>
  <supported-locale>en</supported-locale>
  <supported-locale>fr</supported-locale>
 </locale-config>
 <resource-bundle>
  <base-name>msgs.ShortMessages</base-name>
  <var>short</var>
 </resource-bundle>
 <resource-bundle>
  <base-name>msgs.FullMessages</base-name>
  <var>full</var>
  </resource-bundle>
 </application>

Now, you can use both resource bundles in the application views by indicating the proper var. For example, a dummy usage is below:

<h:outputText value="#{short['NAME']}"/>
<h:outputText value="#{short['SURNAME']}"/>
<h:outputText value="#{full['NAME']}"/>
<h:outputText value="#{full['SURNAME']}"/>

Now, programmatically speaking, we can access these resource bundles via Faces#getResourceBundles(), as below:

Map<String, ResourceBundle> resourcebundles = Faces.getResourceBundles();
for (Map.Entry<String, ResourceBundle> entry : resourcebundles.entrySet()) {
     // entry.getKey() - this is the var name (short, full)
     // entry.getValue().getString("NAME")); - for short var
     // entry.getValue().getString("SURNAME")); - for full var
}

Niciun comentariu :

Trimiteți un comentariu

JSF BOOKS COLLECTION

Postări populare

OmniFaces/JSF Fans

Follow by Email

Visitors Starting 4 September 2015

Locations of Site Visitors