- java.lang.Object
-
- org.omnifaces.util.Components
-
public final class Components extends Object
Collection of utility methods for the Faces API with respect to working with
UIComponent
. There are several traversal/lookup methods, there are severalUIForm
andUIInput
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.
- Author:
- Bauke Scholtz, Arjan Tijms
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Components.ForEach
Builder class used to collect a number of query parameters for a visit (for each) of components in the Faces component tree.
-
Field Summary
Fields Modifier and Type Field Description static String
LABEL_ATTRIBUTE
The name of the label attribute.static String
VALUE_ATTRIBUTE
The name of the value attribute.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
addFacesScriptResource()
Add the Faces JavaScript resource to current view.static void
addFormIfNecessary()
Add anUIForm
to the current view if absent.static void
addScript(String script)
Add given JavaScript code to the current view which is to be executed as an inline script when the rendering is completed.static void
addScriptResource(String libraryName, String resourceName)
Add given JavaScript resource to the current view.static UIViewRoot
buildView(String viewId)
Creates and builds a local view for the given view ID independently from the current view.static <T> String
convertToString(FacesContext context, ValueHolder holder, T value)
Convert given value of given value holder to string using either the converter attached to the given value holder or the one obtained viaApplication.createConverter(Class)
based on the type of the given value.static MethodExpressionActionListener
createActionListenerMethodExpression(String expression)
Create an action listener method expression based on the given EL expression.static AjaxBehavior
createAjaxBehavior(String expression)
Create an ajax behavior which should invoke an ajax listener method expression based on the given EL expression.static <C extends UIComponent>
CcreateComponent(FacesContext context, String componentType)
Creates a new component.static <C extends UIComponent>
CcreateComponent(String componentType)
Creates a new component.static MethodExpression
createMethodExpression(String expression, Class<?> returnType, Class<?>... parameterTypes)
Create a method expression based on the given EL expression, the given return type and the given parameter types, if any.static ValueExpression
createValueExpression(String expression, Class<?> type)
Create an editable value expression based on the given EL expression and the given type.static MethodExpression
createVoidMethodExpression(String expression, Class<?>... parameterTypes)
Create a void method expression based on the given EL expression and the given parameter types, if any.static void
disableInput(UIInput input)
Disable the passedUIInput
component.static void
disableInput(String clientId)
Disable theUIInput
component matching the given client ID search expression.static String
encodeHtml(UIComponent component)
Encodes the given component locally as HTML, with UTF-8 character encoding, independently from the current view.static <C extends UIComponent>
Optional<C>findClosestParent(UIComponent component, Class<C> parentType)
Finds from the given component the closest parent of the given parent type.static <C extends UIComponent>
CfindComponent(String clientId)
Returns the UI component matching the given client ID search expression.static <C extends UIComponent>
CfindComponentInChildren(UIComponent component, String clientId)
Returns the UI component matching the given client ID search expression relative to the point in the component tree of the given component, searching only in its children.static <C extends UIComponent>
CfindComponentInParents(UIComponent component, String clientId)
Returns the UI component matching the given client ID search expression relative to the point in the component tree of the given component, searching only in its parents.static <C extends UIComponent>
CfindComponentRelatively(UIComponent component, String clientId)
Returns the UI component matching the given client ID search expression relative to the point in the component tree of the given component.static <C extends UIComponent>
List<C>findComponentsInChildren(UIComponent component, Class<C> type)
Returns a list of UI components matching the given type in children of the given component.static <C extends UIComponent>
List<C>findComponentsInCurrentForm(Class<C> type)
Returns a list of UI components matching the given type in children of the currently submitted form.static Components.ForEach
forEachComponent()
Invokes an operation on every component in the component tree.static Components.ForEach
forEachComponent(FacesContext facesContext)
Invokes an operation on every component in the component tree.static List<String>
getActionExpressionsAndListeners(UIComponent component)
Returns a list of all action expressions and listeners associated with given component.static <T> T
getAttribute(UIComponent component, String name)
Returns the attribute of the given component on the given name.static <C extends UIComponent>
CgetClosestParent(UIComponent component, Class<C> parentType)
Returns from the given component the closest parent of the given parent type, ornull
if none is found.static <C extends UIComponent>
CgetCurrentActionSource()
Returns the source of the currently invoked action, ornull
if there is none, which may happen when the current request is not a postback request at all, or when the view has been changed by for example a successful navigation.static UICommand
getCurrentCommand()
Returns the currently invoked UI command component, ornull
if there is none, which may happen when the current request is not a postback request at all, or when the view has been changed by for example a successful navigation.static <C extends UIComponent>
CgetCurrentComponent()
Returns the current UI component from the EL context.static UIForm
getCurrentForm()
Returns the currently submitted UI form component, ornull
if there is none, which may happen when the current request is not a postback request at all, or when the view has been changed by for example a successful navigation.static <T> Class<T>
getExpectedType(ValueExpression valueExpression)
Returns the expected type of the given value expression.static <T> Class<T>
getExpectedValueType(UIComponent component)
Returns the expected type of the "value" attribute of the given component.static <T> T
getImmediateValue(UIInput input)
Returns the value of the given input component whereby any unconverted submitted string value will immediately be converted/validated as this method is called.static String
getLabel(UIComponent component)
Returns the value of thelabel
attribute associated with the given UI component if any, else the client ID.static UIMessage
getMessageComponent(UIInput input)
static UIMessages
getMessagesComponent()
Returns the firstUIMessages
component found in the current view.static String
getOptionalLabel(UIComponent component)
Returns the value of thelabel
attribute associated with the given UI component if any, else null.static <T> List<ParamHolder<T>>
getParams(UIComponent component)
Returns an unmodifiable list with all childUIParameter
components (<f|o:param>
) of the given parent component as a list ofParamHolder
instances.static Map<String,List<String>>
getParams(UIComponent component, boolean includeRequestParams, boolean includeViewParams)
Returns an unmodifiable map with all request query string or view parameters, appended with all childUIParameter
components (<f|o:param>
) of the given parent component.static String
getRenderedValue(FacesContext context, ValueHolder holder)
Get the rendered value of given value holder.static <T> T
getValue(EditableValueHolder component)
Returns the value of the given editable value holder component without the need to know if the given component has already been converted/validated or not.static boolean
hasInvokedSubmit(UIComponent component)
Returns whether the given component has invoked the form submit.static boolean
hasSubmittedValue(EditableValueHolder component)
Returns whether the given editable value holder component has a submitted value.static UIComponent
includeCompositeComponent(UIComponent parent, String libraryName, String tagName, String id)
Create and include the composite component of the given library and resource name as child of the given UI component parent and return the created composite component.static UIComponent
includeCompositeComponent(UIComponent parent, String libraryName, String tagName, String id, Map<String,String> attributes)
Create and include the composite component of the given library and resource name as child of the given UI component parent, set the given attributes on it and return the created composite component.static void
includeFacelet(UIComponent parent, String path)
Include the Facelet file at the given (relative) path as child of the given UI component parent.static void
invalidateInput(String relativeClientId, String message, Object... params)
InvalidateUIInput
component identified by given relative client ID viainvalidateInputs(String...)
and callsMessages.addError(String, String, Object...)
on it with the given message body which is formatted with the given parameters.static void
invalidateInputs(String... relativeClientIds)
InvalidateUIInput
components identified by given relative client IDs.static boolean
isEditable(UIInput input)
Returns whether the given UI input component is editable.static boolean
isRendered(UIComponent component)
Returns whether the given UI component and all of its parents is rendered.static void
resetForm(UIComponent component)
static void
resetInputs(UIComponent component)
Reset all childUIInput
components enclosed in the given parent component.static void
setAttribute(UIComponent component, String name, Object value)
Set the attribute value on the specified component for the specified name.static void
setLabel(UIComponent component, Object label)
Sets thelabel
attribute of the given UI component with the given value.static <C extends UIComponent>
voidvalidateHasChild(UIComponent component, Class<C> childType)
Validate in development stage if the given component has at least a child of given child type.static <C extends UIComponent>
voidvalidateHasDirectParent(UIComponent component, Class<C> parentType)
Validate in development stage if the given component has a direct parent of given parent type.static void
validateHasNoChildren(UIComponent component)
Validate in development stage if the given component has no children.static <C extends UIComponent>
voidvalidateHasNoParent(UIComponent component, Class<C> parentType)
Validate in development stage if the given component has no parent of given parent type.static <C extends UIComponent>
voidvalidateHasOnlyChildren(UIComponent component, Class<C> childType)
Validate in development stage if the given component has only children of given child type.static <C extends UIComponent>
voidvalidateHasParent(UIComponent component, Class<C> parentType)
Validate in development stage if the given component has a parent of given parent type.
-
-
-
Field Detail
-
LABEL_ATTRIBUTE
public static final String LABEL_ATTRIBUTE
The name of the label attribute.- See Also:
- Constant Field Values
-
VALUE_ATTRIBUTE
public static final String VALUE_ATTRIBUTE
The name of the value attribute.- See Also:
- Constant Field Values
-
-
Method Detail
-
getCurrentComponent
public static <C extends UIComponent> C getCurrentComponent()
Returns the current UI component from the EL context.- Type Parameters:
C
- The expected component type.- Returns:
- The current UI component from the EL context.
- Throws:
ClassCastException
- WhenC
is of wrong type.- See Also:
UIComponent.getCurrentComponent(FacesContext)
-
getAttribute
public static <T> T getAttribute(UIComponent component, String name)
Returns the attribute of the given component on the given name.- Type Parameters:
T
- The expected return type.- Parameters:
component
- The component to return the attribute of the given name for.name
- The name of the attribute of the given component to be returned.- Returns:
- The attribute of the given component on the given name.
- Throws:
ClassCastException
- WhenT
is of wrong type.- Since:
- 1.5
-
setAttribute
public static void setAttribute(UIComponent component, String name, Object value)
Set the attribute value on the specified component for the specified name.- Parameters:
component
- The component on which to set the attribute.name
- The name of the attribute.value
- The value of the attribute, can be aValueExpression
ornull
. In case of aValueExpression
,UIComponent.setValueExpression(String, ValueExpression)
will be invoked instead. In case ofnull
,Map.remove(Object)
will be invoked onUIComponent.getAttributes()
.- Since:
- 4.5
-
isRendered
public static boolean isRendered(UIComponent component)
Returns whether the given UI component and all of its parents is rendered. This thus not only checks the component's ownrendered
attribute, but also of all of its parents.- Parameters:
component
- The component to be checked.- Returns:
true
if the given UI component and all of its parents is rendered.- Since:
- 1.8
-
findComponent
public static <C extends UIComponent> C findComponent(String clientId)
Returns the UI component matching the given client ID search expression.- Type Parameters:
C
- The expected component type.- Parameters:
clientId
- The client ID search expression.- Returns:
- The UI component matching the given client ID search expression.
- Throws:
ClassCastException
- WhenC
is of wrong type.- See Also:
UIComponent.findComponent(String)
-
findComponentRelatively
public static <C extends UIComponent> C findComponentRelatively(UIComponent component, String clientId)
Returns the UI component matching the given client ID search expression relative to the point in the component tree of the given component. For this search both parents and children are consulted, increasingly moving further away from the given component. Parents are consulted first, then children.- Type Parameters:
C
- The expected component type.- Parameters:
component
- the component from which the relative search is started.clientId
- The client ID search expression.- Returns:
- The UI component matching the given client ID search expression.
- Throws:
ClassCastException
- WhenC
is of wrong type.- See Also:
UIComponent.findComponent(String)
-
findComponentInParents
public static <C extends UIComponent> C findComponentInParents(UIComponent component, String clientId)
Returns the UI component matching the given client ID search expression relative to the point in the component tree of the given component, searching only in its parents.- Type Parameters:
C
- The expected component type.- Parameters:
component
- the component from which the relative search is started.clientId
- The client ID search expression.- Returns:
- The UI component matching the given client ID search expression.
- Throws:
ClassCastException
- WhenC
is of wrong type.- See Also:
UIComponent.findComponent(String)
-
findComponentInChildren
public static <C extends UIComponent> C findComponentInChildren(UIComponent component, String clientId)
Returns the UI component matching the given client ID search expression relative to the point in the component tree of the given component, searching only in its children.- Type Parameters:
C
- The expected component type.- Parameters:
component
- the component from which the relative search is started.clientId
- The client ID search expression.- Returns:
- The UI component matching the given client ID search expression.
- Throws:
ClassCastException
- WhenC
is of wrong type.- See Also:
UIComponent.findComponent(String)
-
findComponentsInChildren
public static <C extends UIComponent> List<C> findComponentsInChildren(UIComponent component, Class<C> type)
Returns a list of UI components matching the given type in children of the given component.- Type Parameters:
C
- The generic component type.- Parameters:
component
- The component to search in its children for UI components matching the given type.type
- The type of the UI components to be searched in children of the given component.- Returns:
- A list of UI components matching the given type in children of the given component.
-
findComponentsInCurrentForm
public static <C extends UIComponent> List<C> findComponentsInCurrentForm(Class<C> type)
Returns a list of UI components matching the given type in children of the currently submitted form. The currently submitted form is obtained bygetCurrentForm()
.- Type Parameters:
C
- The generic component type.- Parameters:
type
- The type of the UI components to be searched in children of the currently submitted form.- Returns:
- A list of UI components matching the given type in children of the currently submitted form.
- Since:
- 3.1
-
getClosestParent
public static <C extends UIComponent> C getClosestParent(UIComponent component, Class<C> parentType)
Returns from the given component the closest parent of the given parent type, ornull
if none is found.- Type Parameters:
C
- The generic component type.- Parameters:
component
- The component to return the closest parent of the given parent type for.parentType
- The parent type.- Returns:
- From the given component the closest parent of the given parent type, or
null
if none is found.
-
findClosestParent
public static <C extends UIComponent> Optional<C> findClosestParent(UIComponent component, Class<C> parentType)
Finds from the given component the closest parent of the given parent type.- Type Parameters:
C
- The generic component type.- Parameters:
component
- The component to find the closest parent of the given parent type for.parentType
- The parent type.- Returns:
- From the given component the closest parent of the given parent type.
- Since:
- 3.11
-
forEachComponent
public static Components.ForEach forEachComponent()
Invokes an operation on every component in the component tree.This is a simplified version of regular component visiting that uses the builder pattern to provide the various optional parameters. Includes supports for only visiting components of a certain class type and two simplified functional interfaces / lambdas.
- Returns:
- A new instance of
Components.ForEach
. - Since:
- 2.0
-
forEachComponent
public static Components.ForEach forEachComponent(FacesContext facesContext)
Invokes an operation on every component in the component tree.This is a simplified version of regular component visiting that uses the builder pattern to provide the various optional parameters. Includes supports for only visiting components of a certain class type and two simplified functional interfaces / lambdas.
- Parameters:
facesContext
- the faces context used for tree visiting- Returns:
- A new instance of
Components.ForEach
, using the given faces context. - Since:
- 2.0
-
createComponent
public static <C extends UIComponent> C createComponent(String componentType)
Creates a new component.- Type Parameters:
C
- The generic component type.- Parameters:
componentType
- The component type to create.- Returns:
- The newly created component.
- Throws:
ClassCastException
- WhenC
is of wrong type.- Since:
- 4.4
- See Also:
Application.createComponent(String)
-
createComponent
public static <C extends UIComponent> C createComponent(FacesContext context, String componentType)
Creates a new component.- Type Parameters:
C
- The generic component type.- Parameters:
context
- The involved faces context.componentType
- The component type to create.- Returns:
- The newly created component.
- Throws:
ClassCastException
- WhenC
is of wrong type.- Since:
- 4.4
- See Also:
Application.createComponent(String)
-
includeFacelet
public static void includeFacelet(UIComponent parent, String path) throws IOException
Include the Facelet file at the given (relative) path as child of the given UI component parent. This has the same effect as using<ui:include>
. The path is relative to the current view ID and absolute to the webcontent root.- Parameters:
parent
- The parent component to include the Facelet file in.path
- The (relative) path to the Facelet file.- Throws:
IOException
- Whenever given path cannot be read.- Since:
- 1.5
- See Also:
FaceletContext.includeFacelet(UIComponent, String)
-
includeCompositeComponent
public static UIComponent includeCompositeComponent(UIComponent parent, String libraryName, String tagName, String id)
Create and include the composite component of the given library and resource name as child of the given UI component parent and return the created composite component. This has the same effect as usingxmlns:my="http://xmlns.jcp.org/jsf/composite/libraryName
and<my:tagName>
. The given component ID must be unique relative to the current naming container parent and is mandatory for functioning of input components inside the composite, if any.- Parameters:
parent
- The parent component to include the composite component in.libraryName
- The library name of the composite component (path after "http://xmlns.jcp.org/jsf/composite/").tagName
- The tag name of the composite component.id
- The component ID of the composite component.- Returns:
- The created composite component, which can if necessary be used to set more custom attributes on it.
- Since:
- 1.5
-
includeCompositeComponent
public static UIComponent includeCompositeComponent(UIComponent parent, String libraryName, String tagName, String id, Map<String,String> attributes)
Create and include the composite component of the given library and resource name as child of the given UI component parent, set the given attributes on it and return the created composite component. This has the same effect as usingxmlns:my="http://xmlns.jcp.org/jsf/composite/libraryName
and<my:tagName>
. The given component ID must be unique relative to the current naming container parent and is mandatory for functioning of input components inside the composite, if any.The attribute values must represent literal values or literal EL expressions, exactly like as you would declare in the view file. E.g.
attributes.put("foo", "#{bean.foo}"); attributes.put("bar", "true"); attributes.put("baz", "#{bean.baz(" + someId + ")}");
- Parameters:
parent
- The parent component to include the composite component in.libraryName
- The library name of the composite component (path after "http://xmlns.jcp.org/jsf/composite/").tagName
- The tag name of the composite component.id
- The component ID of the composite component.attributes
- The attributes to be set on the composite component.- Returns:
- The created composite component, which can if necessary be used to set more custom attributes on it.
- Since:
- 2.2
-
addScript
public static void addScript(String script)
Add given JavaScript code to the current view which is to be executed as an inline script when the rendering is completed. When the current request isFaces.isAjaxRequestWithPartialRendering()
, then it will delegate toAjax.oncomplete(String...)
, else it will add given JavaScript code as inline script to end of body.- Parameters:
script
- JavaScript code which is to be executed as an inline script.- Since:
- 3.6
-
addScriptResource
public static void addScriptResource(String libraryName, String resourceName)
Add given JavaScript resource to the current view. This will first check if the resource isn't already rendered as perResourceHandler.isResourceRendered(FacesContext, String, String)
. If not, then continue as below:- When the current request is a
Faces.isAjaxRequestWithPartialRendering()
, then it will delegate toAjax.load(String, String)
. - Else when the
<h:head>
has not yet been rendered, then add given JavaScript resource to head. - Else add given JavaScript resource to end of the
<h:body>
.
- Parameters:
libraryName
- Library name of the JavaScript resource.resourceName
- Resource name of the JavaScript resource.- Since:
- 3.6
- When the current request is a
-
addFacesScriptResource
public static void addFacesScriptResource()
Add the Faces JavaScript resource to current view. If Faces 4.0+ is present, then it will add thejakarta.faces:faces.js
resource, else it will add thejakarta.faces:jsf.js
resource.- Since:
- 4.0
-
buildView
public static UIViewRoot buildView(String viewId) throws IOException
Creates and builds a local view for the given view ID independently from the current view.- Parameters:
viewId
- The ID of the view which needs to be created and built.- Returns:
- A fully populated component tree of the given view ID.
- Throws:
IOException
- Whenever something fails at I/O level. This can happen when the given view ID is unavailable or malformed.- Since:
- 2.2
- See Also:
ViewHandler.createView(FacesContext, String)
,ViewDeclarationLanguage.buildView(FacesContext, UIViewRoot)
-
encodeHtml
public static String encodeHtml(UIComponent component)
Encodes the given component locally as HTML, with UTF-8 character encoding, independently from the current view. The current implementation, however, uses the current faces context. The same managed beans as in the current faces context will be available as well, including request scoped ones. But, depending on the nature of the provided component, the state of the faces context may be affected because the attributes of the context, request, view, session and application scope could be (in)directly manipulated during the encode. This may or may not have the desired effect. If the given view does not have any component resources, Faces forms, dynamically added components, component event listeners, then it should mostly be safe. In other words, use this at most for "simple templates" only, e.g. a HTML based mail template, which usually already doesn't have a HTML head nor body.- Parameters:
component
- The component to capture HTML output for.- Returns:
- The encoded HTML output of the given component.
- Throws:
UncheckedIOException
- Whenever something fails at I/O level. This would be quite unexpected as it happens locally.- Since:
- 2.2
- See Also:
UIComponent.encodeAll(FacesContext)
-
getCurrentForm
public static UIForm getCurrentForm()
Returns the currently submitted UI form component, ornull
if there is none, which may happen when the current request is not a postback request at all, or when the view has been changed by for example a successful navigation. If the latter is the case, you'd better invoke this method before navigation.- Returns:
- The currently submitted UI form component.
- See Also:
UIForm.isSubmitted()
-
getCurrentCommand
public static UICommand getCurrentCommand()
Returns the currently invoked UI command component, ornull
if there is none, which may happen when the current request is not a postback request at all, or when the view has been changed by for example a successful navigation. If the latter is the case, you'd better invoke this method before navigation.- Returns:
- The currently invoked UI command component.
- Since:
- 1.6
-
getCurrentActionSource
public static <C extends UIComponent> C getCurrentActionSource()
Returns the source of the currently invoked action, ornull
if there is none, which may happen when the current request is not a postback request at all, or when the view has been changed by for example a successful navigation. If the latter is the case, you'd better invoke this method before navigation.- Type Parameters:
C
- The expected component type.- Returns:
- The source of the currently invoked action.
- Since:
- 2.4
-
isEditable
public static boolean isEditable(UIInput input)
Returns whether the given UI input component is editable. That is when it is rendered, not disabled and not readonly.- Parameters:
input
- The UI input component to be checked.- Returns:
true
if the given UI input component is editable.
-
getLabel
public static String getLabel(UIComponent component)
Returns the value of thelabel
attribute associated with the given UI component if any, else the client ID. It never returns null.- Parameters:
component
- The UI component for which the label is to be retrieved.- Returns:
- The value of the
label
attribute associated with the given UI component if any, else the client ID.
-
getOptionalLabel
public static String getOptionalLabel(UIComponent component)
Returns the value of thelabel
attribute associated with the given UI component if any, else null.- Parameters:
component
- The UI component for which the label is to be retrieved.- Returns:
- The value of the
label
attribute associated with the given UI component if any, else null.
-
setLabel
public static void setLabel(UIComponent component, Object label)
Sets thelabel
attribute of the given UI component with the given value.- Parameters:
component
- The UI component for which the label is to be set.label
- The label to be set on the given UI component.
-
getValue
public static <T> T getValue(EditableValueHolder component)
Returns the value of the given editable value holder component without the need to know if the given component has already been converted/validated or not. Note that it thus returns the unconverted submitted string value when the conversion/validation hasn't been taken place for the given component and it returns the converted object value -if applicable- when conversion/validation has been taken place for the given component.- Type Parameters:
T
- The expected return type.- Parameters:
component
- The editable value holder component to obtain the value for.- Returns:
- The value of the given editable value holder component.
- Throws:
ClassCastException
- WhenT
is of wrong type.
-
getImmediateValue
public static <T> T getImmediateValue(UIInput input)
Returns the value of the given input component whereby any unconverted submitted string value will immediately be converted/validated as this method is called. This method thus always returns the converted/validated value.- Type Parameters:
T
- The expected return type.- Parameters:
input
- The input component to obtain the converted/validated value for.- Returns:
- The converted/validated value of the given input component.
- Throws:
ClassCastException
- WhenT
is of wrong type.- Since:
- 1.2
-
hasSubmittedValue
public static boolean hasSubmittedValue(EditableValueHolder component)
Returns whether the given editable value holder component has a submitted value.- Parameters:
component
- The editable value holder component to be checked.- Returns:
true
if the given editable value holder component has a submitted value, otherwisefalse
.
-
getExpectedValueType
public static <T> Class<T> getExpectedValueType(UIComponent component)
Returns the expected type of the "value" attribute of the given component. This is useful in among others a "generic entity converter".- Type Parameters:
T
- The expected type of the expected type of the "value" attribute of the given component.- Parameters:
component
- The component to obtain the expected type of the "value" attribute for.- Returns:
- The expected type of the "value" attribute of the given component, or
null
when there is no such value. - Throws:
ClassCastException
- WhenT
is of wrong type.- Since:
- 3.8
-
getExpectedType
public static <T> Class<T> getExpectedType(ValueExpression valueExpression)
Returns the expected type of the given value expression. This first inspects if theValueExpression.getExpectedType()
returns a specific type, i.e. notjava.lang.Object
, and then returns it, else it inspects the actual type of the property behind the expression string.- Type Parameters:
T
- The expected type of the expected type of the given value expression.- Parameters:
valueExpression
- The value expression to obtain the expected type for.- Returns:
- The expected type of the given value expression.
- Throws:
ClassCastException
- WhenT
is of wrong type.- Since:
- 3.8
-
hasInvokedSubmit
public static boolean hasInvokedSubmit(UIComponent component)
Returns whether the given component has invoked the form submit. In non-ajax requests, that can only be anUICommand
component. In ajax requests, that can also be among others anUIInput
component.- Parameters:
component
- The component to be checked.- Returns:
true
if the given component has invoked the form submit.- Since:
- 1.3
-
getParams
public static <T> List<ParamHolder<T>> getParams(UIComponent component)
Returns an unmodifiable list with all childUIParameter
components (<f|o:param>
) of the given parent component as a list ofParamHolder
instances. Those withdisabled=true
and an empty name are skipped.- Type Parameters:
T
- The type of the param value.- Parameters:
component
- The parent component to retrieve all childUIParameter
components from.- Returns:
- An unmodifiable list with all child
UIParameter
components having a non-empty name and not disabled. - Since:
- 2.1
-
getParams
public static Map<String,List<String>> getParams(UIComponent component, boolean includeRequestParams, boolean includeViewParams)
Returns an unmodifiable map with all request query string or view parameters, appended with all childUIParameter
components (<f|o:param>
) of the given parent component. Those withdisabled=true
or an empty name or an empty value are skipped. The<f|o:param>
will override any included view or request parameters on the same name.- Parameters:
component
- The parent component to retrieve all childUIParameter
components from.includeRequestParams
- Whether or not to include request query string parameters. When set totrue
, then this overrides theincludeViewParams
.includeViewParams
- Whether or not to include view parameters.- Returns:
- An unmodifiable list with all request query string or view parameters, appended with all child
UIParameter
components having a non-empty name and not disabled. - Since:
- 2.4
-
getMessagesComponent
public static UIMessages getMessagesComponent()
Returns the firstUIMessages
component found in the current view. This returnsnull
if none can be found.- Returns:
- The first
UIMessages
component found in the current view. - Since:
- 2.5
-
resetForm
public static void resetForm(UIComponent component)
Reset all childUIInput
components enclosed in the givenUIForm
component, or the closestUIForm
parent of it.- Parameters:
component
- The component representing theUIForm
itself, or to find the closestUIForm
parent for.- Throws:
IllegalArgumentException
- When given component is not anUIForm
, or does not have aUIForm
parent.- Since:
- 2.5
-
resetInputs
public static void resetInputs(UIComponent component)
Reset all childUIInput
components enclosed in the given parent component.- Parameters:
component
- The parent component to reset all childUIInput
components in.- Since:
- 2.5
-
disableInput
public static void disableInput(UIInput input)
Disable the passedUIInput
component.- Parameters:
input
- TheUIInput
component to disable.- Since:
- 4.5
-
disableInput
public static void disableInput(String clientId)
Disable theUIInput
component matching the given client ID search expression.- Parameters:
clientId
- The client ID search expression.- Since:
- 4.5
-
addFormIfNecessary
public static void addFormIfNecessary()
Add anUIForm
to the current view if absent. This might be needed for scripts which rely on Faces view state identifier and/or on functioning of jsf.ajax.request().- Since:
- 3.6
-
convertToString
public static <T> String convertToString(FacesContext context, ValueHolder holder, T value)
Convert given value of given value holder to string using either the converter attached to the given value holder or the one obtained viaApplication.createConverter(Class)
based on the type of the given value.- Type Parameters:
T
- The generic value type.- Parameters:
context
- The involved faces context.holder
- The value holder.value
- The value to be converted to string.- Returns:
- The conversion result, may be
null
, depending on the value and the converter implementation. - Since:
- 4.1
-
getRenderedValue
public static String getRenderedValue(FacesContext context, ValueHolder holder)
Get the rendered value of given value holder. If the given value holder is an instance ofEditableValueHolder
, and itsEditableValueHolder.getSubmittedValue()
is non-null
, then return it, or if itsEditableValueHolder.isLocalValueSet()
is true, then useValueHolder.getLocalValue()
as base value. Else useValueHolder.getValue()
as base value. Finally return the result ofconvertToString(FacesContext, ValueHolder, Object)
with base value as value argument. The result should be exactly the same as displayed during the render response phase.- Parameters:
context
- The involved faces context.holder
- The value holder.- Returns:
- The rendered value, never
null
. If the final result wasnull
, then an empty string is returned. - Since:
- 4.2
-
invalidateInputs
public static void invalidateInputs(String... relativeClientIds)
InvalidateUIInput
components identified by given relative client IDs. They will first be searched usingfindComponentRelatively(UIComponent, String)
within theUIForm
returned bygetCurrentForm()
with a fallback to theUIViewRoot
. Then theEditableValueHolder.setValid(boolean)
will be set withfalse
.- Parameters:
relativeClientIds
- The relative client IDs ofUIInput
components to be invalidated.- Throws:
IllegalArgumentException
- When a relative client ID does not represent anUIInput
component.- Since:
- 4.2
-
invalidateInput
public static void invalidateInput(String relativeClientId, String message, Object... params)
InvalidateUIInput
component identified by given relative client ID viainvalidateInputs(String...)
and callsMessages.addError(String, String, Object...)
on it with the given message body which is formatted with the given parameters.- Parameters:
relativeClientId
- The relative client ID ofUIInput
component to be invalidated.message
- The message to be added to the invalidatedUIInput
component.params
- The message format parameters, if any.- Throws:
IllegalArgumentException
- When the relative client ID does not represent anUIInput
component.- Since:
- 4.2
-
createValueExpression
public static ValueExpression createValueExpression(String expression, Class<?> type)
Create an editable value expression based on the given EL expression and the given type.- Parameters:
expression
- The EL expression to represent an editable value expression.type
- The type of the property referenced by the value expression.- Returns:
- The created editable value expression, ready to be used as
UIComponent.setValueExpression(String, ValueExpression)
.
-
createMethodExpression
public static MethodExpression createMethodExpression(String expression, Class<?> returnType, Class<?>... parameterTypes)
Create a method expression based on the given EL expression, the given return type and the given parameter types, if any. As an example, the following action method examples,
public void submit1()
public String submit2()
public void submit3(String argument)
public String submit4(String argument)
public void submit5(String argument1, Long argument2)
public String submit6(Long argument1, String argument2)
can be created as follows:
createMethodExpression("#{bean.submit1}", Void.class);
createMethodExpression("#{bean.submit2}", String.class);
createMethodExpression("#{bean.submit3('foo')}", Void.class, String.class);
createMethodExpression("#{bean.submit4('foo')}", String.class, String.class);
createMethodExpression("#{bean.submit5('foo', 0)}", Void.class, String.class, Long.class);
createMethodExpression("#{bean.submit6(0, 'foo')}", String.class, Long.class, String.class);
- Parameters:
expression
- The EL expression to create a method expression for.returnType
- The return type of the method expression. Can benull
if you don't care about the return type (e.g.void
orString
).parameterTypes
- The parameter types of the method expression.- Returns:
- The created method expression, ready to be used as
UICommand.setActionExpression(MethodExpression)
.
-
createVoidMethodExpression
public static MethodExpression createVoidMethodExpression(String expression, Class<?>... parameterTypes)
Create a void method expression based on the given EL expression and the given parameter types, if any. As an example, the following action method examples,
public void submit1()
public void submit3(String argument)
public void submit5(String argument1, Long argument2)
can be created as follows:
createVoidMethodExpression("#{bean.submit1}");
createVoidMethodExpression("#{bean.submit3('foo')}", String.class);
createVoidMethodExpression("#{bean.submit5('foo', 0)}", String.class, Long.class);
- Parameters:
expression
- The EL expression to create a void method expression for.parameterTypes
- The parameter types of the void method expression.- Returns:
- The created void method expression, ready to be used as
UICommand.setActionExpression(MethodExpression)
.
-
createActionListenerMethodExpression
public static MethodExpressionActionListener createActionListenerMethodExpression(String expression)
Create an action listener method expression based on the given EL expression. The target method must take anActionEvent
as argument. As an example, the following action method example,public void actionListener(ActionEvent event)
can be created as follows:
createActionListenerMethodExpression("#{bean.actionListener}");
- Parameters:
expression
- The EL expression to create an action listener method expression for.- Returns:
- The created action listener method expression, ready to be used as
UICommand.addActionListener(jakarta.faces.event.ActionListener)
.
-
createAjaxBehavior
public static AjaxBehavior createAjaxBehavior(String expression)
Create an ajax behavior which should invoke an ajax listener method expression based on the given EL expression. The target method must take anAjaxBehaviorEvent
as argument. As an example, the following ajax listener example,public void ajaxListener(AjaxBehaviorEvent event)
can be created as follows:
createAjaxBehavior("#{bean.ajaxListener}");
Note that this is essentially the programmatic equivalent of
<f:ajax>
. So if you intented to create for example a<p:ajax>
programmatically, then don't use this method.- Parameters:
expression
- The EL expression to be invoked when the created ajax behavior is processed.- Returns:
- The created ajax behavior, ready to be used as
UIComponentBase.addClientBehavior(String, ClientBehavior)
whereby the string argument represents the client event name, such as "action", "valueChange", "click", "blur", etc.
-
getActionExpressionsAndListeners
public static List<String> getActionExpressionsAndListeners(UIComponent component)
Returns a list of all action expressions and listeners associated with given component. This covers expressions inaction
attribute of command components andlistener
attribute of ajax components. Any method expressions are in format#{bean.method}
and any action listeners are added as fully qualified class names. This list is primarily useful for logging postback actions in a phase listener. You can usegetCurrentActionSource()
to obtain the current action source.- Parameters:
component
- The component to retrieve all action expressions and listeners from.- Returns:
- A list of all action expressions and listeners associated with given component.
- Since:
- 2.4
-
validateHasParent
public static <C extends UIComponent> void validateHasParent(UIComponent component, Class<C> parentType)
Validate in development stage if the given component has a parent of given parent type.- Type Parameters:
C
- The generic component type.- Parameters:
component
- The component to be validated.parentType
- The parent type to be checked.- Throws:
IllegalStateException
- When the given component doesn't have any parent of the given type.
-
validateHasDirectParent
public static <C extends UIComponent> void validateHasDirectParent(UIComponent component, Class<C> parentType)
Validate in development stage if the given component has a direct parent of given parent type.- Type Parameters:
C
- The generic component type.- Parameters:
component
- The component to be validated.parentType
- The parent type to be checked.- Throws:
IllegalStateException
- When the given component doesn't have a direct parent of the given type.
-
validateHasNoParent
public static <C extends UIComponent> void validateHasNoParent(UIComponent component, Class<C> parentType)
Validate in development stage if the given component has no parent of given parent type.- Type Parameters:
C
- The generic component type.- Parameters:
component
- The component to be validated.parentType
- The parent type to be checked.- Throws:
IllegalStateException
- When the given component does have a parent of the given type.- Since:
- 2.5
-
validateHasChild
public static <C extends UIComponent> void validateHasChild(UIComponent component, Class<C> childType)
Validate in development stage if the given component has at least a child of given child type.- Type Parameters:
C
- The generic component type.- Parameters:
component
- The component to be validated.childType
- The child type to be checked.- Throws:
IllegalStateException
- When the given component doesn't have any children of the given type.- Since:
- 2.5
-
validateHasOnlyChildren
public static <C extends UIComponent> void validateHasOnlyChildren(UIComponent component, Class<C> childType)
Validate in development stage if the given component has only children of given child type.- Type Parameters:
C
- The generic component type.- Parameters:
component
- The component to be validated.childType
- The child type to be checked.- Throws:
IllegalStateException
- When the given component has children of a different type.- Since:
- 2.5
-
validateHasNoChildren
public static void validateHasNoChildren(UIComponent component)
Validate in development stage if the given component has no children.- Parameters:
component
- The component to be validated.- Throws:
IllegalStateException
- When the given component has any children.
-
-