JSF Tutorial - JSF OutputScript Example








The h:outputScript tag renders an HTML element of the type "script" with type "text/javascript".

This tag adds external javascript file to JSF page.

The following JSF tag

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

is rendered into the following HTML code.

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

Example

The following code is from common.js.

document.write('java2s.com');

The following code is from UserBean.java.

package com.java2s.common;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
 
@ManagedBean(name="user")
@SessionScoped
public class UserBean{
 
  
}

The following code is from demo.xhtml.

<?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>
      
      <h:outputScript library="js" name="common.js" target="head" />
      
    </h:body>

</html>


Download OutputScript.zip





To RUN

Copy the generated WAR file from the target folder to Tomcat deployment folder and run Tomcat-Install-folder/bin/startup.bat.

After Tomcat finish starting, type the following URL in the browser address bar.

http://localhost:8080/simple-webapp/demo.xhtml