- All Implemented Interfaces:
EditableValueHolder,PartialStateHolder,StateHolder,TransientStateHolder,ValueHolder,ComponentSystemEventListener,FacesListener,SystemEventListenerHolder,EventListener
The <o:hashParam> is a component that extends the standard <f:viewParam>
with support for setting hash query parameter values in bean and automatically reflecting updated model values in
hash query string.
The "hash query string" is the part in URL after the # which could be formatted in the same format
as a regular request query string (the part in URL after the ?). An example:
https://example.com/page.xhtml#foo=baz&bar=kaz
This specific part of the URL (also called hash fragment identifier) is by default not sent to the server. This
component will on page load and on every window.onhashchange event send it anyway so that the Faces model
gets updated, and on every Faces ajax request update the hash query string in client side when the Faces model value has
changed.
Usage
It's very similar to the <o:viewParam>.
<f:metadata>
<o:hashParam name="foo" value="#{bean.foo}" />
<o:hashParam name="bar" value="#{bean.bar}" />
</f:metadata>
You can use the render attribute to declare which components should be updated when a hash parameter
value is present.
<f:metadata>
<o:hashParam name="foo" value="#{bean.foo}" render="fooResult" />
<o:hashParam name="bar" value="#{bean.bar}" />
</f:metadata>
...
<h:body>
...
<h:panelGroup id="fooResult">
...
</h:panelGroup>
...
</h:body>
In case you need to invoke a bean method before rendering, e.g. to preload the rendered contents based on new hash
param values, then you can observe the HashChangeEvent. See the "Events" section for an usage example.
You can use the default attribute to declare a non-null value which should be interpreted as the default
value. In other words, when the current model value matches the default value, then the hash parameter will be
removed.
<f:metadata>
<o:hashParam name="foo" value="#{bean.foo}" />
<o:hashParam name="bar" value="#{bean.bar}" default="kaz" />
</f:metadata>
When #{bean.foo} is "baz" and #{bean.bar} is "kaz" or empty,
then the reflected hash query string will become https://example.com/page.xhtml#foo=baz.
If #{bean.bar} is any other value, then it will appear in the hash query string.
Note that as it extends from the standard <f:viewParam>, its built-in conversion and validation
functionality is also supported on this component.
Events
When the hash query string is changed by the client side, e.g. by following a #foo=baz&bar=kaz link,
or by manually manipulating the URL, then a CDI HashChangeEvent will be fired which can be observed in any
CDI managed bean as below:
public void onHashChange(@Observes HashChangeEvent event) {
String oldHashString = event.getOldValue();
String newHashString = event.getNewValue();
// ...
}
This is useful in case you want to preload the model for whatever is rendered by
<o:hashParam render>.
- Since:
- 3.2
- Author:
- Bauke Scholtz
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class jakarta.faces.component.UIViewParameter
UIViewParameter.Reference -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe component type, which is "org.omnifaces.component.input.HashParam".static final StringThe omnifaces event value, which is "setHashParamValues".Fields inherited from class org.omnifaces.component.input.OnloadParam
stateFields inherited from class jakarta.faces.component.UIViewParameter
COMPONENT_FAMILYFields inherited from class jakarta.faces.component.UIInput
ALWAYS_PERFORM_VALIDATION_WHEN_REQUIRED_IS_TRUE, CONVERSION_MESSAGE_ID, EMPTY_STRING_AS_NULL_PARAM_NAME, REQUIRED_MESSAGE_ID, UPDATE_MESSAGE_ID, VALIDATE_EMPTY_FIELDS_PARAM_NAMEFields inherited from class jakarta.faces.component.UIComponent
ATTRS_WITH_DECLARED_DEFAULT_VALUES, BEANINFO_KEY, bindings, COMPOSITE_COMPONENT_TYPE_KEY, COMPOSITE_FACET_NAME, FACETS_KEY, VIEW_LOCATION_KEY -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voiddecodeAll(FacesContext context) Decode all relevantOnloadParamcomponents at once.Returns the default value in case the actual hash parameter isnullor empty.protected StringgetEventValue(FacesContext context) Returns the value of theOmniFaces.OMNIFACES_EVENT_PARAM_NAMEassociated with the current component.protected StringgetInitScript(FacesContext context) Returns script which should be executed upon initialization of a new view.getRenderedValue(FacesContext context) Convert the value to string using any converter and ensure that an empty string is returned when the component is invalid or the resulting string is null or represents the default value.protected StringgetUpdateScript(FacesContext context) Returns script which should be exeucted upon ajax update of the current view.static booleanisHashParamRequest(FacesContext context) Returnstrueif the current request is triggered by a hash param request.voidsetDefault(String defaultValue) Sets the default value in case the actual hash parameter isnullor empty.Methods inherited from class org.omnifaces.component.input.OnloadParam
decodeImmediately, getRender, isOnloadParamRequest, isOnloadParamRequest, processDecodes, processUpdates, processValidators, setRenderMethods inherited from class jakarta.faces.component.UIViewParameter
decode, encodeAll, getConvertedValue, getFacesContext, getFamily, getName, getStringValue, getStringValueFromModel, isImmediate, isRendered, setName, updateModelMethods inherited from class jakarta.faces.component.UIInput
addValidator, addValueChangeListener, broadcast, clearInitialState, compareValues, getConverterMessage, getRequiredMessage, getSubmittedValue, getValidatorMessage, getValidators, getValue, getValueChangeListeners, isEmpty, isLocalValueSet, isRequired, isValid, markInitialState, removeValidator, removeValueChangeListener, resetValue, restoreState, saveState, setConverterMessage, setImmediate, setLocalValueSet, setRequired, setRequiredMessage, setSubmittedValue, setValid, setValidatorMessage, setValue, validate, validateValueMethods inherited from class jakarta.faces.component.UIOutput
getConverter, getLocalValue, setConverterMethods inherited from class jakarta.faces.component.UIComponentBase
addClientBehavior, addFacesListener, encodeBegin, encodeChildren, encodeEnd, findComponent, getAttributes, getChildCount, getChildren, getClientBehaviors, getClientId, getDefaultEventName, getEventNames, getFacesListeners, getFacet, getFacetCount, getFacets, getFacetsAndChildren, getId, getParent, getPassThroughAttributes, getRenderer, getRendererType, getRendersChildren, invokeOnComponent, isTransient, processRestoreState, processSaveState, queueEvent, removeFacesListener, restoreAttachedState, saveAttachedState, setId, setParent, setRendered, setRendererType, setTransient, visitTreeMethods inherited from class jakarta.faces.component.UIComponent
getClientId, getCompositeComponentParent, getContainerClientId, getCurrentComponent, getCurrentCompositeComponent, getListenersForEventClass, getNamingContainer, getPassThroughAttributes, getResourceBundleMap, getStateHelper, getStateHelper, getTransientStateHelper, getTransientStateHelper, getValueExpression, initialStateMarked, isCompositeComponent, isInView, isVisitable, popComponentFromEL, processEvent, pushComponentToEL, restoreTransientState, saveTransientState, setInView, setValueExpression, subscribeToEvent, unsubscribeFromEventMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface jakarta.faces.component.ValueHolder
getConverter, getLocalValue, setConverter
-
Field Details
-
COMPONENT_TYPE
The component type, which is "org.omnifaces.component.input.HashParam".- See Also:
-
EVENT_VALUE
The omnifaces event value, which is "setHashParamValues".- See Also:
-
-
Constructor Details
-
HashParam
public HashParam()
-
-
Method Details
-
getInitScript
Description copied from class:OnloadParamReturns script which should be executed upon initialization of a new view.- Overrides:
getInitScriptin classOnloadParam- Parameters:
context- The involved faces context.- Returns:
- Script which should be executed upon initialization of a new view.
-
getUpdateScript
Description copied from class:OnloadParamReturns script which should be exeucted upon ajax update of the current view.- Overrides:
getUpdateScriptin classOnloadParam- Parameters:
context- The involved faces context.- Returns:
- Script which should be exeucted upon ajax update of the current view.
-
getEventValue
Description copied from class:OnloadParamReturns the value of theOmniFaces.OMNIFACES_EVENT_PARAM_NAMEassociated with the current component.- Specified by:
getEventValuein classOnloadParam- Parameters:
context- The involved faces context.- Returns:
- The value of the
OmniFaces.OMNIFACES_EVENT_PARAM_NAMEassociated with the current component.
-
decodeAll
Description copied from class:OnloadParamDecode all relevantOnloadParamcomponents at once.- Specified by:
decodeAllin classOnloadParam- Parameters:
context- The involved faces context.
-
getRenderedValue
Convert the value to string using any converter and ensure that an empty string is returned when the component is invalid or the resulting string is null or represents the default value.- Parameters:
context- The involved faces context.- Returns:
- The rendered value.
-
getDefault
Returns the default value in case the actual hash parameter isnullor empty.- Returns:
- The default value in case the actual hash parameter is
nullor empty.
-
setDefault
Sets the default value in case the actual hash parameter isnullor empty.- Parameters:
defaultValue- The default value in case the actual hash parameter isnullor empty.
-
isHashParamRequest
Returnstrueif the current request is triggered by a hash param request. I.e. if it is initiated byOmniFaces.HashParam.setHashParamValues()script which runs on page load when thewindow.location.hashis present, and on everywindow.onhashchangeevent.- Parameters:
context- The involved faces context.- Returns:
trueif the current request is triggered by a hash param request.
-