Adding external javascript to JSF

I want to insert an external javascript file into my JSF page. So what I did:

Now the JSF file is named start.xhtml and both are in the same folder. However, when I was running, nothing happened [javascript should trigger a warning when I click]. I checked the source of the page and found in.

What did I do wrong to get RES NOT FOUND? I even set the absolute path, but nothing happened :(. I'm sad panda.

Fyi, I use the latest Netbeans.

Thank you, Song.

+4
source share
2 answers

First of all, the absolute path should work. This is not a Netbeans or Glassfih question, or JSF is a browser. And if your browser had an error preventing it from receiving Javascript from valid URLs, you would notice. Therefore, if your Javascript does not load, the probability of 99% is a simple typo, a silly mistake (forgetting the directory name, adding an extra slash or such things) and nothing to do with any of the mentioned technologies.

Another theory (just a theory - I don’t have enough data to prove it) is that you have a standard mapping showing all the face files in the "virtual" faces directory (/ faces / *). Thus, when you put your index.xhtml in the main directory of the Foo project, you see it under: localhost: 8080 / Foo / faces / index.xhtml. The "front" part of the path does not represent any real directory, it is just a display. Therefore, if you have a js file sitting on index.xhtml, you would refer to it like this: '../yourjavascript.js';../- this is a compensation for part of the virtual directory.

In any case, I highly recommend that you give up the downloadable dillemas script and use the official and good way to load resources like javascript to put them in a directory called "resources" (make it under "web pages") node in Netbeans. it will go to the top directory of your .war); to get the path to any file stored in resources, you use EL as: # {resource ['filename.css']}. You load your script with:

<script src='#{resource['script.js']}' ></script> 

If you use a resource directory, you can do many more things, read some details somewhere

+4
source

Try turning on the script this way

 <script src="#{facesContext.externalContext.requestContextPath}/yourPathAfterWebpages/scriptFile.js" type="text/javascript"></script> 
+5
source

Source: https://habr.com/ru/post/1338044/


All Articles