Wednesday, July 23, 2014

JSF 2 - Import javascript file

1. Import JS file as a local resource

★  In JSF2 you can use the <h:outputScript/> tag to import local javascript and css files in to your xhtml file. However if you are using this tag your resource file should be located in the WebContent/resources folder. Please follow the following steps.

★ Create a new folder inside your WebContent fodler and name it as 'resources'.

 Put your JS file directly in to resources folder or create a sub folder in resources folder and put the file in to it. (I will create a subfolder and name it as js and add my js file myJs.js in to it.)

 Now in your xhtml page add this line in between the <h:head></h:head> tags to import the file.
<h:outputScript name="js/myJs.js" />

2. Import an external JS file

★   You can't use the <h:outputScript/> tag to import external js files. Just use plain <script/> tag as below.
<script src="http://<your-site>/yourJs.js"/>