I\'ve done tutorial about Facelets templating.
Now I\'ve tried to create a page that isn\'t in same directory as the template. I\'ve got problems with page style, b
The proper JSF 2.x way is using name
referring the path relative to webapp's /resources
folder. This way you don't need to worry about the context path as you would do in JSF 1.x. See also How to include CSS relative to context path in JSF 1.x?
Drop the CSS/JS/image files in /resources
folder of the public webcontent as below (just create one if not already exist at the same level as /WEB-INF
and /META-INF
).
WebContent
|-- resources
| |-- css
| | |-- other.css
| | `-- style.css
| |-- js
| | `-- script.js
| `-- images
| |-- background.png
| |-- favicon.ico
| `-- logo.png
|-- META-INF
| `-- MANIFEST.MF
|-- WEB-INF
| |-- faces-config.xml
| `-- web.xml
|-- page.xhtml
:
In case of Maven, it should be in /main/webapp/resources
and thus not /main/resources
(those are for Java resources (properties/xml/text/config files) which must end up in runtime classpath, not in webcontent). See also Maven and JSF webapp structure, where exactly to put JSF resources.
Ultimately, those resources are available as below everywhere without the need to fiddle with relative paths:
...
...
...
The name
attribute must represent the full path relative to the /resources
folder. It does not need to start with /
. You do not need the library
attribute as long as you aren't developing a component library like PrimeFaces or a common module JAR file which is shared by multiple webapps.
You can reference the
anywhere, also in
of template clients without the need for an additional
. It will via the
component of master template automatically end up in generated .
...
You can reference
also anywhere, but it will by default end up in the HTML exactly there where you declared it. If you want it to end up in via
, then add target="head"
attribute.
...
Or, if you want it to end up at the end of (right before
, so that e.g.
window.onload
and $(document).ready()
etc isn't necessary) via
, then add target="body"
attribute.
...
HeadRenderer
In case you're using PrimeFaces, its HeadRenderer
will messup the default
script ordering as described above. You're basically forced to force the order via PrimeFaces-specific
, which may end up in messy and "untemplateable" code. You may want to turn off it as described in this answer.
You can even package the resources in a JAR file. See also Structure for multiple JSF projects with shared code.
You can in EL use the #{resource}
mapping to let JSF basically print a resource URL like /context/javax.faces.resource/folder/file.ext.xhtml?ln=library
so that you could use it as e.g. CSS background image or favicon. Only requirement is that the CSS file itself should also be served as a JSF resource, otherwise EL expressions won't evaluate. See also How to reference JSF image resource as CSS background image url.
.some {
background-image: url("#{resource['images/background.png']}");
}
Here's the @import
example.
@import url("#{resource['css/other.css']}");
Here's the favicon example. See also Add favicon to JSF project and reference it in .
In case you're using a SCSS compiler (e.g. Sass Compiler Plugin for Maven), keep in mind that the SCSS processor might interpret #
as a special character. In that case you would need to escape it with \
.
.some {
background-image: url("\#{resource['images/background.png']}");
}
Third party CSS files loaded via
which in turn reference fonts and/or images may need to be altered to use #{resource}
expressions as described in previous section, otherwise an UnmappedResourceHandler
needs to be installed in order to be able to serve those using JSF. See also a.o. Bootsfaces page shows up in browser without any styling and How to use Font Awesome 4.x CSS file with JSF? Browser can't find font files.
If you intend to hide the resources from public access by moving the whole /resources
folder into /WEB-INF
, then you can since JSF 2.2 optionally change the webcontent-relative path via a new web.xml
context parameter as follows:
javax.faces.WEBAPP_RESOURCES_DIRECTORY
/WEB-INF/resources
In older JSF versions this is not possible.