public class ScriptParam extends OnloadParam
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.
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 javax.json.JsonObject
:
private JsonObject clientNavigator;And this converter:
package com.example; import java.io.StringReader; import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; import javax.faces.convert.Converter; import javax.faces.convert.ConverterException; import javax.faces.convert.FacesConverter; import javax.json.Json; import javax.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); } } }
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>
.
Faces.getScriptParameters()
,
PostScriptParam
UIViewParameter.Reference
Modifier and Type | Field and Description |
---|---|
static String |
COMPONENT_TYPE
The component type, which is "org.omnifaces.component.input.ScriptParam".
|
static String |
EVENT_VALUE
The omnifaces event value, which is "setScriptParamValues".
|
state
COMPONENT_FAMILY
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_NAME
ATTRS_WITH_DECLARED_DEFAULT_VALUES, BEANINFO_KEY, bindings, COMPOSITE_COMPONENT_TYPE_KEY, COMPOSITE_FACET_NAME, CURRENT_COMPONENT, CURRENT_COMPOSITE_COMPONENT, FACETS_KEY, HONOR_CURRENT_COMPONENT_ATTRIBUTES_PARAM_NAME, VIEW_LOCATION_KEY
Constructor and Description |
---|
ScriptParam() |
Modifier and Type | Method and Description |
---|---|
protected void |
decodeAll(FacesContext context)
Decode all relevant
OnloadParam components at once. |
protected String |
getEventValue(FacesContext context)
Returns the value of the
OmniFaces.OMNIFACES_EVENT_PARAM_NAME associated with the current component. |
protected String |
getInitScript(FacesContext context)
Returns script which should be executed upon initialization of a new view.
|
String |
getScript()
Returns the script to be evaluated.
|
static boolean |
isScriptParamRequest(FacesContext context)
Returns
true if the current request is triggered by a script param request. |
void |
setScript(String script)
Sets the script to be evaluated.
|
decodeImmediately, getRender, getUpdateScript, isOnloadParamRequest, isOnloadParamRequest, processDecodes, processValidators, setRender, updateModel
decode, encodeAll, getConvertedValue, getFamily, getName, getStringValue, getStringValueFromModel, getSubmittedValue, isImmediate, setName, setSubmittedValue
addValidator, addValueChangeListener, clearInitialState, compareValues, getConverterMessage, getRequiredMessage, getValidator, getValidatorMessage, getValidators, getValue, getValueChangeListener, getValueChangeListeners, isEmpty, isLocalValueSet, isRequired, isValid, markInitialState, processUpdates, removeValidator, removeValueChangeListener, resetValue, restoreState, saveState, setConverterMessage, setImmediate, setLocalValueSet, setRequired, setRequiredMessage, setValid, setValidator, setValidatorMessage, setValue, setValueChangeListener, validate, validateValue
getConverter, getLocalValue, setConverter
addClientBehavior, addFacesListener, broadcast, encodeBegin, encodeChildren, encodeEnd, findComponent, getAttributes, getChildCount, getChildren, getClientBehaviors, getClientId, getDefaultEventName, getEventNames, getFacesContext, getFacesListeners, getFacet, getFacetCount, getFacets, getFacetsAndChildren, getId, getListenersForEventClass, getParent, getPassThroughAttributes, getRenderer, getRendererType, getRendersChildren, getValueBinding, invokeOnComponent, isRendered, isTransient, processRestoreState, processSaveState, queueEvent, removeFacesListener, restoreAttachedState, saveAttachedState, setId, setParent, setRendered, setRendererType, setTransient, setValueBinding, subscribeToEvent, unsubscribeFromEvent
getClientId, getCompositeComponentParent, getContainerClientId, getCurrentComponent, getCurrentCompositeComponent, getNamingContainer, getPassThroughAttributes, getResourceBundleMap, getStateHelper, getStateHelper, getTransientStateHelper, getTransientStateHelper, getValueExpression, initialStateMarked, isCompositeComponent, isInView, isVisitable, popComponentFromEL, processEvent, pushComponentToEL, restoreTransientState, saveTransientState, setInView, setValueExpression, visitTree
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
getConverter, getLocalValue, setConverter
public static final String COMPONENT_TYPE
public static final String EVENT_VALUE
protected String getInitScript(FacesContext context)
OnloadParam
getInitScript
in class OnloadParam
context
- The involved faces context.protected String getEventValue(FacesContext context)
OnloadParam
OmniFaces.OMNIFACES_EVENT_PARAM_NAME
associated with the current component.getEventValue
in class OnloadParam
context
- The involved faces context.OmniFaces.OMNIFACES_EVENT_PARAM_NAME
associated with the current component.protected void decodeAll(FacesContext context)
OnloadParam
OnloadParam
components at once.decodeAll
in class OnloadParam
context
- The involved faces context.public String getScript()
public void setScript(String script)
script
- The script to be evaluated.public static boolean isScriptParamRequest(FacesContext context)
true
if the current request is triggered by a script param request.
I.e. if it is initiated by OmniFaces.ScriptParam.setScriptParamValues()
script which runs on page load.context
- The involved faces context.true
if the current request is triggered by a script param request.Copyright © 2012–2020 OmniFaces. All rights reserved.