- All Implemented Interfaces:
EditableValueHolder,PartialStateHolder,StateHolder,TransientStateHolder,ValueHolder,ComponentSystemEventListener,FacesListener,SystemEventListenerHolder,EventListener
The <o:scriptParam> is a component that extends the standard <f:viewParam>
with support for setting results of client-side evaluated JavaScript code in bean.
Usage
It's similar to the <f:viewParam>.
<f:metadata>
<o:scriptParam script="new Date().getTimezoneOffset()" value="#{bean.clientTimeZoneOffset}" />
<o:scriptParam script="window.screen.width" value="#{bean.clientScreenWidth}" />
<o:scriptParam script="someFunctionName()" value="#{bean.resultOfSomeFunctionName}" />
</f:metadata>
You can use the render attribute to declare which components should be updated when a script parameter
has been set.
<f:metadata>
<o:scriptParam script="foo()" value="#{bean.resultOfFoo}" render="fooResult" />
</f:metadata>
...
<h:body>
...
<h:panelGroup id="fooResult">
<ui:fragment rendered="#{not empty bean.resultOfFoo}">
The result of foo() script is: #{bean.resultOfFoo}
</ui:fragment>
</h:panelGroup>
...
</h:body>
Note that as it extends from the standard <f:viewParam>, its built-in conversion and validation
functionality is also supported on this component. So, the following is also possible:
<f:metadata>
<o:scriptParam script="window.navigator" value="#{bean.clientNavigator}" />
</f:metadata>
With a clientNavigator being an instance of jakarta.json.JsonObject:
private JsonObject clientNavigator;And this converter:
package com.example;
import java.io.StringReader;
import jakarta.faces.component.UIComponent;
import jakarta.faces.context.FacesContext;
import jakarta.faces.convert.Converter;
import jakarta.faces.convert.ConverterException;
import jakarta.faces.convert.FacesConverter;
import jakarta.json.Json;
import jakarta.json.JsonObject;
@FacesConverter(forClass = JsonObject.class)
public class JsobObjectConverter implements Converter<JsonObject> {
@Override
public String getAsString(FacesContext context, UIComponent component, JsonObject modelValue) {
if (modelValue == null) {
return "";
}
return modelValue.toString();
}
@Override
public JsonObject getAsObject(FacesContext context, UIComponent component, String submittedValue) {
if (submittedValue == null || submittedValue.isEmpty()) {
return null;
}
try {
return Json.createReader(new StringReader(submittedValue)).readObject();
}
catch (Exception e) {
throw new ConverterException("Not a valid JSON object", e);
}
}
}
Events
When the script params have been set, then any method with the PostScriptParam annotation will be fired:
@PostScriptParam
public void initScriptParams() {
// ...
}
This is useful in case you want to preload the model for whatever is rendered by
<o:scriptParam render>.
- Since:
- 3.6
- 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.ScriptParam".static final StringThe omnifaces event value, which is "setScriptParamValues".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.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.Returns the script to be evaluated.static booleanisScriptParamRequest(FacesContext context) Returnstrueif the current request is triggered by a script param request.voidSets the script to be evaluated.Methods inherited from class org.omnifaces.component.input.OnloadParam
decodeImmediately, getRender, getUpdateScript, 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.ScriptParam".- See Also:
-
EVENT_VALUE
The omnifaces event value, which is "setScriptParamValues".- See Also:
-
-
Constructor Details
-
ScriptParam
public ScriptParam()
-
-
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.
-
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.
-
getScript
Returns the script to be evaluated.- Returns:
- The script to be evaluated.
-
setScript
Sets the script to be evaluated.- Parameters:
script- The script to be evaluated.
-
isScriptParamRequest
Returnstrueif the current request is triggered by a script param request. I.e. if it is initiated byOmniFaces.ScriptParam.setScriptParamValues()script which runs on page load.- Parameters:
context- The involved faces context.- Returns:
trueif the current request is triggered by a script param request.
-