JSF - h:outputScript


The h:outputScript tag renders an HTML element of the type "script" with type "text/javascript". This tag is used to add an external javascript file to JSF page.


<h:outputScript library = "js" name = "help.js" /> 

Rendered Output

<script type = "text/javascript"  
   src = "/helloworld/javax.faces.resource/help.js.jsf?ln = js"></script> 

Example Application

Let us create a test JSF application to test the above tag.

Step Description
1 Create a project with a name helloworld under a package com.tutorialspoint.test as explained in the JSF - First Application chapter.
2 Create resources folder under src → main folder.
3 Create js folder under src → main → resources folder.
4 Create help.js file under src → main → resources → js folder.
5 Modify help.js file as explained below.
6 Modify pom.xml as explained below.
7 Modify home.xhtml as explained below. Keep rest of the files unchanged.
8 Compile and run the application to make sure business logic is working as per the requirements.
9 Finally, build the application in the form of war file and deploy it in Apache Tomcat Webserver.
10 Launch your web application using appropriate URL as explained below in the last step.


function showMessage(){
   alert("Hello World!");	


<project xmlns = "http://maven.apache.org/POM/4.0.0" 
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0
   <name>helloworld Maven Webapp</name>


<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 

<html xmlns = "http://www.w3.org/1999/xhtml"
   xmlns:f = "http://java.sun.com/jsf/core"    
   xmlns:h = "http://java.sun.com/jsf/html">
      <title>JSF Tutorial!</title>
      <h:outputScript library = "js" name = "help.js"  /> 
      <h2>h:outputScript example</h2>
      <hr />
         <h:commandButton onclick = "showMessage();" />

Once you are ready with all the changes done, let us compile and run the application as we did in JSF - First Application chapter. If everything is fine with your application, this will produce the following result.

JSF h:outputScript