Loading JavaScript as a Resource
The JavaScript resource file bundled with JavaServer Faces technology is
named jsf.js and is available in the javax.faces library. This
resource library supports Ajax functionality in JavaServer Faces
applications.
If you use the f:ajax tag on a page, the jsf.js resource is
automatically delivered to the client. It is not necessary to use the
h:outputScript tag to specify this resource. You may want to use the
h:outputScript tag to specify other JavaScript libraries.
To use a JavaScript resource directly with a UIComponent, you must
explicitly load the resource as described in either of the following
topics:
-
Using JavaScript API in a Facelets Application – Uses the
h:outputScripttag directly in a Facelets page -
Using the @ResourceDependency Annotation in a Bean Class – Uses the
javax.faces.application.ResourceDependencyannotation on aUIComponentJava class
Using JavaScript API in a Facelets Application
To use the JavaScript resource API directly in a web application, such as a Facelets page:
-
Identify the default JavaScript resource for the page with the help of the
h:outputScripttag.For example, consider the following section of a Facelets page:
<h:form> <h:outputScript name="jsf.js" library="javax.faces" target="head"/> </h:form>Specifying the target as
headcauses the script resource to be rendered within theheadelement on the HTML page. -
Identify the component to which you would like to attach the Ajax functionality.
-
Add the Ajax functionality to the component by using the JavaScript API. For example, consider the following:
<h:form> <h:outputScript name="jsf.js" library="javax.faces" target="head"> <h:inputText id="inputname" value="#{userBean.name}"/> <h:outputText id="outputname" value="#{userBean.name}"/> <h:commandButton id="submit" value="Submit" onclick="jsf.ajax.request(this, event, {execute:'inputname',render:'outputname'}); return false;" /> </h:form>The
jsf.ajax.requestmethod takes up to three parameters that specify source, event, and options. The source parameter identifies the DOM element that triggered the Ajax request, typicallythis. The optional event parameter identifies the DOM event that triggered this request. The optional options parameter contains a set of name/value pairs from Table 13-5.
Table 13-5 Possible Values for the Options Parameter
Name |
Value |
|
A space-delimited list of client identifiers or one of the keywords listed in Table 13-2. The identifiers reference the components that will be processed during the Execute phase of the lifecycle. |
|
A space-delimited list of client identifiers or one of the keywords listed in Table 13-2. The identifiers reference the components that will be processed during the render phase of the lifecycle. |
|
A |
|
A |
|
An object that may include additional parameters to include in the request. |
+
If no identifier is specified, the default assumed keyword for the
execute attribute is @this, and for the render attribute it is
@none.
+ You can also place the JavaScript method in a file and include it as a resource.
Using the @ResourceDependency Annotation in a Bean Class
Use the javax.faces.application.ResourceDependency annotation to cause
the bean class to load the default jsf.js library.
To load the Ajax resource from the server side:
-
Use the
jsf.ajax.requestmethod within the bean class.Note:
This method is usually used when creating a custom component or a custom renderer for a component.
The following example shows how the resource is loaded in a bean class:
@ResourceDependency(name="jsf.js" library="javax.faces" target="head")