public class CommandScript extends UICommand
CommandScript is an extension to <h:commandXxx>
which generates a JavaScript
function in the global JavaScript scope which allows the end-user to execute a JSF ajax request by just a function
call functionName()
in the JavaScript context.
The <o:commandScript>
component is required to be enclosed in a UIForm
component. The
name
attribute is required and it represents the JavaScript function name. The execute
and render
attributes work exactly the same as in <f:ajax>
. The onbegin
and oncomplete
attributes must represent (valid!) JavaScript code which will be executed before sending
the ajax request and after processing the ajax response respectively. The action
,
actionListener
and immediate
attributes work exactly the same as in
<h:commandXxx>
.
Basic usage example of <o:commandScript>
which submits the entire form on click of a plain HTML
button:
<h:form> <h:inputText value="#{bean.input1}" ... /> <h:inputText value="#{bean.input2}" ... /> <h:inputText value="#{bean.input3}" ... /> <o:commandScript name="submitForm" action="#{bean.submit}" render="@form" /> </h:form> <input type="button" value="submit" onclick="submitForm()" />
Usage example which uses the <o:commandScript>
as a poll function which updates every 3 seconds:
<h:form> <h:dataTable id="data" value="#{bean.data}" ...>...</h:dataTable> <o:commandScript name="updateData" action="#{bean.reloadData}" render="data" /> </h:form> <h:outputScript target="body">setInterval(updateData, 3000);</h:outputScript>
The component also supports nesting of <f:param>
, <f:actionListener>
and
<f:setPropertyActionListener>
, exactly like as in <h:commandXxx>
. The function
also supports a JS object as argument which will then end up in the HTTP request parameter map:
functionName({ name1: "value1", name2: "value2" });
With the above example, the parameters are in the action method available as follows:
String name1 = Faces.getRequestParameter("name1"); // value1 String name2 = Faces.getRequestParameter("name2"); // value2
Modifier and Type | Field and Description |
---|---|
static String |
COMPONENT_TYPE |
COMPONENT_FAMILY
BEANINFO_KEY, COMPOSITE_COMPONENT_TYPE_KEY, COMPOSITE_FACET_NAME, CURRENT_COMPONENT, CURRENT_COMPOSITE_COMPONENT, FACETS_KEY, VIEW_LOCATION_KEY
Constructor and Description |
---|
CommandScript() |
Modifier and Type | Method and Description |
---|---|
void |
decode(FacesContext context)
If this command script was invoked, queue the
ActionEvent accordingly. |
void |
encodeChildren(FacesContext context)
Write a
<span><script> with therein the script function which allows the end-user to
execute a JSF ajax request by a just script function call functionName() in the JavaScript context. |
String |
getExecute()
Returns a space separated string of client IDs to process on ajax request.
|
String |
getFamily()
Returns
ScriptFamily.COMPONENT_FAMILY . |
String |
getName()
Returns the script function name.
|
String |
getOnbegin()
Returns a script to execute before ajax request is fired.
|
String |
getOncomplete()
Returns a script to execute after ajax response is processed.
|
String |
getRender()
Returns a space separated string of client IDs to update on ajax response.
|
boolean |
getRendersChildren()
Returns
true . |
void |
setExecute(String execute)
Sets a space separated string of client IDs to process on ajax request.
|
void |
setName(String name)
Sets the script function name.
|
void |
setOnbegin(String onbegin)
Sets a script to execute before ajax request is fired.
|
void |
setOncomplete(String oncomplete)
Sets a script to execute after ajax response is processed.
|
void |
setRender(String render)
Sets a space separated string of client IDs to update on ajax response.
|
addActionListener, broadcast, getAction, getActionExpression, getActionListener, getActionListeners, getValue, isImmediate, queueEvent, removeActionListener, setAction, setActionExpression, setActionListener, setImmediate, setValue
addClientBehavior, clearInitialState, encodeBegin, encodeEnd, findComponent, getAttributes, getChildCount, getChildren, getClientBehaviors, getClientId, getDefaultEventName, getEventNames, getFacet, getFacetCount, getFacets, getFacetsAndChildren, getId, getParent, getRendererType, getValueBinding, invokeOnComponent, isRendered, isTransient, markInitialState, processDecodes, processRestoreState, processSaveState, processUpdates, processValidators, restoreAttachedState, restoreState, saveAttachedState, saveState, setId, setParent, setRendered, setRendererType, setTransient, setValueBinding
encodeAll, getClientId, getCompositeComponentParent, getContainerClientId, getCurrentComponent, getCurrentCompositeComponent, getListenersForEventClass, getNamingContainer, getResourceBundleMap, getValueExpression, initialStateMarked, isCompositeComponent, isInView, popComponentFromEL, processEvent, pushComponentToEL, setInView, setValueExpression, subscribeToEvent, unsubscribeFromEvent, visitTree
public static final String COMPONENT_TYPE
public String getFamily()
ScriptFamily.COMPONENT_FAMILY
.public boolean getRendersChildren()
true
.getRendersChildren
in class UIComponentBase
public void decode(FacesContext context)
ActionEvent
accordingly.decode
in class UIComponentBase
public void encodeChildren(FacesContext context) throws IOException
<span><script>
with therein the script function which allows the end-user to
execute a JSF ajax request by a just script function call functionName()
in the JavaScript context.encodeChildren
in class UIComponentBase
IllegalArgumentException
- When there is no parent form, or when the name
attribute is
missing, or when the name
attribute does not represent a valid script function name.IOException
public String getName()
public void setName(String name)
name
- The script function name.public String getExecute()
public void setExecute(String execute)
execute
- A space separated string of client IDs to process on ajax request.public String getRender()
public void setRender(String render)
render
- A space separated string of client IDs to update on ajax response.public String getOnbegin()
public void setOnbegin(String onbegin)
onbegin
- A script to execute before ajax request is fired.public String getOncomplete()
public void setOncomplete(String oncomplete)
oncomplete
- A script to execute after ajax response is processed.