Java Reference
In-Depth Information
Now the button component will fire an Ajax action in case of a
mouseover
event as well
as a mouse click event.
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 func-
tionality in JavaServer Faces applications.
In order to use this resource directly with a component or a bean class, you need to expli-
citly load the resource library. The resource can be loaded in one of the following ways:
• By using the resource API directly in a Facelets page
• By using the
javax.faces.application.ResourceDependency
an-
notation and the resource API in a bean class
Using JavaScript API in a Facelets Application
To use the bundled JavaScript resource API directly in a web application, such as a Face-
lets page, you need to first identify the default JavaScript resource for the page with the
help of the
h:outputScript
tag. For example, consider the following section of a
Facelets page:
<h:form>
<h:outputScript name="jsf.js" library="javax.faces" tar-
get="head"/>
</h:form>
Specifying the target as
head
causes the script resource to be rendered within the
head
element on the HTML page.
In the next step, identify the component to which you would like to attach the Ajax func-
tionality. 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" tar-
get="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,