Class UIImportConstants

java.lang.Object
All Implemented Interfaces:
PartialStateHolder, StateHolder, TransientStateHolder, ComponentSystemEventListener, FacesListener, SystemEventListenerHolder, EventListener

public class UIImportConstants extends UIComponentBase

UIImportConstants imports a mapping of all constant field values of the given type in the current view.

The ViewDeclarationLanguage implementation must cause an instance of this component to be placed in the view for each occurrence of an <f:importConstants /> element placed inside of an <f:metadata /> element. The user must place <f:metadata /> as a direct child of the UIViewRoot. The ViewMetadata.createMetadataView(jakarta.faces.context.FacesContext) must take care of actual task of importing the constants.

Instances of this class participate in the regular Jakarta Faces lifecycle, including on Ajax requests.

The purpose of this component is to provide a mapping of all constant field values of the given type in the current view. Constant field values are all public static final fields of the given type. The map key represents the constant field name as String. The map value represents the actual constant field value. This works for classes, interfaces and enums.

Usage

The below constant fields:

 package com.example;

 public class Foo {
     public static final String FOO1 = "foo1";
     public static final String FOO2 = "foo2";
 }
 
 package com.example;

 public interface Bar {
     public static final String BAR1 = "bar1";
     public static final String BAR2 = "bar2";
 }
 
 package com.example;

 public enum Baz {
     BAZ1, BAZ2;
 }
 

Can be imported as below:

 <f:metadata>
     <f:importConstants type="com.example.Foo" />
     <f:importConstants type="com.example.Bar" var="Barrr" />
     <f:importConstants type="com.example.Baz" />
 </f:metadata>
 

And can be referenced as below:

 #{Foo.FOO1}, #{Foo.FOO2}, #{Barrr.BAR1}, #{Barrr.BAR2}, #{Baz.BAZ1}, #{Baz.BAZ2}
 
 <h:selectOneMenu value="#{bean.baz}" >
     <f:selectItems value="#{Baz}" />
 </h:selectOneMenu>
 
Since:
2.3