How to include JavaScript file in JSF
In JSF 2.0, you can use <h:outputScript /> tag to render a HTML “script” element, link it to a js file in the page. For example,
JSF…
<h:outputScript library="js" name="common.js" />HTML output…
<script type="text/javascript" src="/JavaServerFaces/faces/javax.faces.resource/common.js?ln=js"> </script>
JSF outputScript example
An example to show the use of JSF 2 <h:outputScript /> to render a “common.js” file in the “resources/js” folder, see figure below :

JSF file
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" > <h:head></h:head> <h:body> <h1>JSF 2 outputScript example</h1> <h:outputScript library="js" name="common.js" /> </h:body> </html>
It will generate the following HTML output
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head></head> <body> <h1>JSF 2 outputScript example</h1> <script type="text/javascript" src="/JavaServerFaces/faces/javax.faces.resource/common.js?ln=js"> </script> </body> </html>
The JS file will render in where JSF <h:outputScript /> tag is placed.
In addition, you can use “target” attribute to render js file within the head element like following :
<h:outputScript library="js" name="common.js" target="head" />It will generate the following HTML output
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <script type="text/javascript" src="/JavaServerFaces/faces/javax.faces.resource/common.js?ln=js"> </script> </head> <body> <h1>JSF 2 outputScript example</h1> </body> </html>







[...] JSF 2 outputScript example Include JavaScript file with <h:outputScript> tag. [...]
Thanks, it helped me :)
Just one observation, not all jsf engines generate the same pattern.
In my case it was:
my suggestion for whom is in trouble with this is to create a css file(e.g. css/jsfcrud.css) in a css folder, than use the bellow tag(with is relative)
check the html source page in your browser and you will find out the pattern your JEE/JSF engine generates.
Thanks that should help.
Is this work?
template.xhtml:
my.xhtml:
How to include js&css files using h:outputScipt tag when using facelets template tech?
How to include js&css files not in the resources directory?
For question not related to the article, try post here – Java Q&A forum
What do you do in JSF 2.0 if you want to inject a remote js file path like the Google Maps API url?
Looks like from http://java.net/jira/browse/JAVASERVERFACES_SPEC_PUBLIC-598 that it isn’t currently possible.
Hi! This is pretty obvious, but my HTML output contains extra xhtml extension, how can I disable this?
Your code:
src=”/JavaServerFaces/faces/javax.faces.resource/common.js?ln=js”
My code:
src=”/JavaServerFaces/faces/javax.faces.resource/common.js.xhtml?ln=js”
Cheers,
Lukasz
[...] JSF 2 outputScript example Include JavaScript file with <h:outputScript> tag. [...]