Class ScriptParam

  • All Implemented Interfaces:
    EditableValueHolder, PartialStateHolder, StateHolder, TransientStateHolder, ValueHolder, ComponentSystemEventListener, FacesListener, SystemEventListenerHolder, EventListener

    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>.

         <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}" />

    You can use the render attribute to declare which components should be updated when a script parameter has been set.

         <o:scriptParam script="foo()" value="#{bean.resultOfFoo}" render="fooResult" />
         <h:panelGroup id="fooResult">
             <ui:fragment rendered="#{not empty bean.resultOfFoo}">
                 The result of foo() script is: #{bean.resultOfFoo}

    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:

         <o:scriptParam script="window.navigator" value="#{bean.clientNavigator}" />
    With a clientNavigator being an instance of jakarta.json.JsonObject:
     private JsonObject clientNavigator;
    And this converter:
     package com.example;
     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> {
         public String getAsString(FacesContext context, UIComponent component, JsonObject modelValue) {
             if (modelValue == null) {
                 return "";
             return modelValue.toString();
         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:

     public void initScriptParams() {
         // ...

    This is useful in case you want to preload the model for whatever is rendered by <o:scriptParam render>.

    Bauke Scholtz
    See Also:
    OnloadParam, PostScriptParam, Faces.getScriptParameters()
    • Field Detail


        public static final String COMPONENT_TYPE
        The component type, which is "org.omnifaces.component.input.ScriptParam".
        See Also:
        Constant Field Values

        public static final String EVENT_VALUE
        The omnifaces event value, which is "setScriptParamValues".
        See Also:
        Constant Field Values
    • Constructor Detail

      • ScriptParam

        public ScriptParam()
    • Method Detail

      • getInitScript

        protected String getInitScript​(FacesContext context)
        Description copied from class: OnloadParam
        Returns script which should be executed upon initialization of a new view.
        getInitScript in class OnloadParam
        context - The involved faces context.
        Script which should be executed upon initialization of a new view.
      • getScript

        public String getScript()
        Returns the script to be evaluated.
        The script to be evaluated.
      • setScript

        public void setScript​(String script)
        Sets the script to be evaluated.
        script - The script to be evaluated.
      • isScriptParamRequest

        public static boolean isScriptParamRequest​(FacesContext context)
        Returns 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.