- cdi
- components
- contexts
- converters
- el
- eventlisteners
- exceptionhandlers
- facesviews
- filters
- functions
- managedbeans
- push
- resourcehandlers
- search
- servlets
- taghandlers
- utils
- validators
- viewhandlers
- - Ajax
- Beans
- BeansLocal
- Components
- Events
- Exceptions
- Faces
- FacesConfigXml
- FacesLocal
- JNDI
- JNDIObjectLocator
- Messages
- Servlets
- WebXml
- cdi
- components
- contexts
- converters
- el
- eventlisteners
- exceptionhandlers
- facesviews
- filters
- functions
- managedbeans
- push
- resourcehandlers
- search
- servlets
- taghandlers
- utils
- validators
- viewhandlers
- Ajax
- Beans
- BeansLocal
- Components
- Events
- Exceptions
- Faces
- FacesConfigXml
- FacesLocal
- JNDI
- JNDIObjectLocator
- Messages
- Servlets
- WebXml
Collection of utility methods for the JSF 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
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 JSF 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());
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" JSF 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:
660
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 javax.enterprise.context.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.component.UIOutput;
import javax.inject.Named;
import org.omnifaces.util.Callback.WithArgument;
@Named
@RequestScoped
public class TreeWalkingBean {
private UIComponent component;
public int getPageComponentCount() {
final int[] count = new int[1];
forEachComponent()
.invoke(new WithArgument<UIComponent>() { @Override public void invoke(UIComponent component) {
count[0]++;
}
});
return count[0];
}
public List<String> getAllIdsInGroup() {
final List<String> textIds = new ArrayList<>();
forEachComponent()
.fromRoot(component)
.invoke(new WithArgument<UIComponent>() { @Override public void invoke(UIComponent component) {
textIds.add(component.getId());
}
});
return textIds;
}
public List<String> getTextIdsInGroup() {
final List<String> textIds = new ArrayList<>();
forEachComponent()
.fromRoot(component)
.ofTypes(UIOutput.class)
.invoke(new WithArgument<UIComponent>() { @Override public void invoke(UIComponent component) {
textIds.add(component.getId());
}
});
return textIds;
}
public UIComponent getComponent() {
return component;
}
public void setComponent(UIComponent component) {
this.component = component;
}
}