-
Available since OmniFaces 1.0
Collection of utility methods for the Faces API with respect to working with UIComponent
. There are several traversal/lookup methods, there are several UIForm
and UIInput
related methods which makes it easier to deal with forms and inputs.
Usage
Here are some examples:
// Get closest parent of given type.
UIForm form = Components.getClosestParent(someUIInputComponent, UIForm.class);
// Get currently submitted form.
UIForm form = Components.getCurrentForm();
// Get currently invoked command, useful for logging actions in a phase listener.
UICommand command = Components.getCurrentCommand();
// Get the label of the given UIInput component as Faces uses for validation messages.
String label = Components.getLabel(someUIInputComponent);
// Inside decode() and/or encode() of some custom component, validate if it has no children.
Components.validateHasNoChildren(this);
// Programmatically include composite component.
Components.includeCompositeComponent(someParentComponent, libraryName, tagName, id);
// Programmatically create value and action expressions.
UICommand command = new HtmlCommandButton();
command.setId("foo");
command.setValue(Components.createValueExpression("#{bundle['button.foo']}", String.class));
command.addClientBehavior("action", Components.createAjaxBehavior("#{bean.ajaxListener}"));
command.addActionListener(Components.createActionListenerMethodExpression("#{bean.actionListener}"));
command.setActionExpression(Components.createVoidMethodExpression("#{bean.action}"));
// Programmatically capture HTML output of a given view.
String mailHtml = Components.encodeHtml(Components.buildView("/WEB-INF/mail-template.xhtml"));
// Collecting all queued actions and action listeners as method expression strings in a logging phase listener.
List<String> actions = Components.getActionExpressionsAndListeners(Components.getCurrentActionSource());
For a full list, check the method summary.
Feature request
If you know more useful methods/functions which you think should be added to this OmniFaces utility class so that everyone can benefit from a "standard" Faces utility library, feel free to post a feature request.
The following shows a utility method for easily invoking an operation on each component in a component tree using the builder pattern.
Count of all components on this page:
708
The following is a rendering of a panel group with several nested components, which will be used for the examples below.
first textfirst nested textsecond text
All IDs of the components in the panel group rendered above:
[mainGroup, firstText, nestedGroup, nestedText, secondText]
All IDs of outputText components in the panel group rendered above:
[firstText, nestedText, secondText]
<p>
The following shows a utility method for easily invoking an operation on each
component in a component tree using the builder pattern.
</p>
<p>
Count of all components on this page:
<br/>
#{treeWalkingBean.pageComponentCount}
</p>
<p>
The following is a rendering of a panel group with several nested components, which will
be used for the examples below.
</p>
<hr/>
<h:panelGroup id="mainGroup" binding="#{treeWalkingBean.component}">
<h:outputText id="firstText" value="first text" />
<h:panelGroup id="nestedGroup">
<h:outputText id="nestedText" value="first nested text" />
</h:panelGroup>
<h:outputText id="secondText" value="second text" />
</h:panelGroup>
<hr/>
<p>
All IDs of the components in the panel group rendered above:
<br/>
#{treeWalkingBean.allIdsInGroup}
</p>
<p>
All IDs of outputText components in the panel group rendered above:
<br/>
#{treeWalkingBean.textIdsInGroup}
</p>
package org.omnifaces.showcase.utils;
import static org.omnifaces.util.Components.forEachComponent;
import java.util.ArrayList;
import java.util.List;
import jakarta.enterprise.context.RequestScoped;
import jakarta.faces.component.UIComponent;
import jakarta.faces.component.UIOutput;
import jakarta.inject.Named;
@Named
@RequestScoped
public class TreeWalkingBean {
private UIComponent component;
public int getPageComponentCount() {
final int[] count = new int[1];
forEachComponent().invoke(component -> count[0]++);
return count[0];
}
public List<String> getAllIdsInGroup() {
final List<String> textIds = new ArrayList<>();
forEachComponent()
.fromRoot(component)
.invoke(component -> textIds.add(component.getId()));
return textIds;
}
public List<String> getTextIdsInGroup() {
final List<String> textIds = new ArrayList<>();
forEachComponent()
.fromRoot(component)
.ofTypes(UIOutput.class)
.invoke(component -> textIds.add(component.getId()));
return textIds;
}
public UIComponent getComponent() {
return component;
}
public void setComponent(UIComponent component) {
this.component = component;
}
}