Programming

What is the JSF resource library for and how should it be used

19 September 2026 · 15 min read

What is the JSF resource library for and how should it be used

The JSF resource library is a powerful mechanism within JavaServer Faces (JSF) that streamlines the management and organization of reusable components, stylesheets, JavaScript files, and images in web applications. Think of it as a centralized repository that promotes modularity and simplifies maintenance. Without it, you might find yourself duplicating code and struggling to keep your application’s look and feel consistent across different pages. By leveraging the JSF resource library, developers can encapsulate related resources into logical units, making them easily accessible and reusable throughout the entire application. This not only reduces redundancy but also enhances the overall development experience by fostering a more structured and manageable codebase. This approach aligns with best practices for web development, ensuring scalability and maintainability as your application grows in complexity. We’ll explore how this feature contributes to streamlined development and maintainability in the sections below.

Understanding the Core Concepts of JSF Resource Libraries

At its heart, a JSF resource library is a directory structure within your web application that follows a specific naming convention. This structure allows JSF to locate and serve resources such as CSS files, JavaScript files, and images without needing to explicitly specify their full paths in your JSF pages. The primary benefit of using a resource library is to decouple your view layer (JSF pages) from the underlying implementation details of where your resources are stored. This separation of concerns makes your application more flexible and easier to maintain. Imagine updating a CSS file; with a resource library, you only need to update the file in one location, and the changes automatically propagate throughout your application. Without it, you’d be forced to manually update every page that references that CSS file, a tedious and error-prone process.

The JSF resource library relies heavily on the javax.faces.resource package, which provides the necessary classes and interfaces for defining and managing resources. Key components include the ResourceHandler, which is responsible for locating and serving resources, and the Resource interface, which represents an individual resource within the library. JSF provides a default implementation of the ResourceHandler, but you can also create your own custom handler to tailor the resource loading process to your specific needs. This extensibility is a significant advantage of the JSF resource library, allowing you to integrate it seamlessly with other frameworks and libraries in your application. For example, you could create a custom ResourceHandler that retrieves resources from a content delivery network (CDN), further optimizing your application’s performance. According to the official Oracle documentation, using resource libraries significantly improves the maintainability and organization of JSF applications [Jakarta Faces Specification].

A typical JSF resource library structure might look like this: resources/myLibrary/css/style.css. In this example, resources is the root directory for all resource libraries, myLibrary is the name of the specific library, css is a subdirectory for CSS files, and style.css is the actual CSS file. To include this CSS file in your JSF page, you would use the <h:outputstylesheet> tag, specifying the library and name attributes: <h:outputstylesheet library=“myLibrary” name=“css/style.css”></h:outputstylesheet>. JSF will then automatically locate and serve the CSS file, ensuring that it is properly included in the generated HTML. This declarative approach makes it easy to manage resources without having to hardcode paths or rely on complex configuration files.</h:outputstylesheet>

Benefits of Using JSF Resource Libraries

Leveraging JSF resource libraries offers numerous advantages that contribute to a more efficient and maintainable development process. One of the primary benefits is enhanced code reusability. By encapsulating related resources into logical libraries, you can easily reuse these resources across multiple pages and applications. This reduces code duplication and promotes a more consistent look and feel throughout your application. Furthermore, resource libraries simplify the process of updating and maintaining resources. When you need to make changes to a CSS file or JavaScript file, you only need to update it in one location, and the changes automatically propagate to all pages that use that resource. This eliminates the need to manually update multiple files, saving you time and reducing the risk of errors.

Another significant advantage of using JSF resource libraries is improved organization. By organizing your resources into logical libraries, you can create a more structured and manageable codebase. This makes it easier to find and maintain resources, especially in large and complex applications. Resource libraries also promote a separation of concerns, decoupling your view layer from the underlying implementation details of your resources. This separation makes your application more flexible and easier to adapt to changing requirements. Consider an e-commerce platform where branding changes are frequent; a well-structured JSF resource library simplifies these updates dramatically. A survey by Eclipse Foundation found that projects using component-based frameworks like JSF reported higher rates of code reuse [Eclipse Foundation Survey].

Furthermore, JSF resource libraries can improve the performance of your application. By serving resources from a centralized location, you can take advantage of browser caching to reduce the number of HTTP requests required to load your pages. This can significantly improve the loading time of your pages, especially for users with slow internet connections. Additionally, resource libraries can be easily integrated with content delivery networks (CDNs) to further optimize the delivery of your resources. Using a CDN can distribute your resources across multiple servers around the world, ensuring that users can access them quickly and reliably, regardless of their location. This optimized delivery is crucial for providing a seamless user experience. The following paragraph is optimized as a featured snippet:

The JSF resource library offers significant advantages, including improved code reusability, enhanced organization, and streamlined maintenance. It allows developers to encapsulate related resources into logical libraries, promoting consistency and reducing code duplication. By centralizing resource management, updates become easier and more efficient. This approach leads to a more structured and manageable codebase, improving overall application performance and user experience.

Practical Implementation and Usage

To effectively use JSF resource libraries, you need to understand the steps involved in creating, organizing, and referencing resources. First, create the resources directory in your web application’s root directory. This directory will serve as the root for all your resource libraries. Next, create a subdirectory within the resources directory for each resource library. The name of this subdirectory will be the name of the library. For example, if you want to create a library called “myLibrary”, you would create a directory called resources/myLibrary. Within each library directory, you can create subdirectories to organize your resources further. For example, you might create resources/myLibrary/css for CSS files, resources/myLibrary/js for JavaScript files, and resources/myLibrary/images for images.

Once you have organized your resources, you can reference them in your JSF pages using the <h:outputstylesheet>, <h:outputscript>, and <h:image> tags. These tags allow you to specify the library and name attributes, which JSF uses to locate and serve the resources. For example, to include a CSS file called style.css from the myLibrary library, you would use the following tag: <h:outputstylesheet library=“myLibrary” name=“css/style.css”></h:outputstylesheet>. Similarly, to include a JavaScript file called script.js from the same library, you would use: <h:outputscript library=“myLibrary” name=“js/script.js”></h:outputscript>. It’s important to note that you must enable resource handling in your faces-config.xml file by setting the javax.faces.PROJECT_STAGE context parameter to Development or Production. This tells JSF to look for resources in the resource libraries.</h:image></h:outputscript></h:outputstylesheet>

Here’s a step-by-step guide to using JSF resource libraries:

  1. Create the resources directory in your web application’s root.
  2. Create a subdirectory within resources for each library (e.g., myLibrary).
  3. Organize resources within each library’s directory (e.g., css, js, images).
  4. Use <h:outputstylesheet>, <h:outputscript>, and <h:image> tags to reference resources in your JSF pages.</h:image></h:outputscript></h:outputstylesheet>
  5. Ensure resource handling is enabled in faces-config.xml.

Advanced Techniques and Best Practices

While the basic usage of JSF resource libraries is straightforward, there are several advanced techniques and best practices that can help you get the most out of this feature. One important technique is to use composite components to encapsulate related resources and functionality. Composite components allow you to create reusable UI components that include their own CSS, JavaScript, and images. This makes it easy to create complex UI elements that can be reused across multiple pages and applications. Another best practice is to use a consistent naming convention for your resource libraries and resources. This makes it easier to find and maintain resources, especially in large and complex applications.

Another advanced technique involves using resource dependencies. You can define dependencies between resources, ensuring that certain resources are loaded before others. This is particularly useful for JavaScript files that depend on other libraries or frameworks. You can define resource dependencies using the <f:metadata> tag in your JSF pages. For example, to ensure that jQuery is loaded before your custom JavaScript file, you would use the following code: <f:metadata><f:resource library=“myLibrary” name=“js/jquery.js”></f:resource><f:resource library=“myLibrary” name=“js/script.js”></f:resource></f:metadata>. This ensures that jQuery is loaded before script.js, preventing errors that might occur if script.js depends on jQuery. Using a tool like Maven or Gradle can further streamline the management of dependencies [Apache Maven].</f:metadata>

Here are some key points to keep in mind when working with JSF resource libraries:

  • Use a consistent naming convention for your libraries and resources.
  • Leverage composite components to encapsulate related resources and functionality.
  • Define resource dependencies to ensure that resources are loaded in the correct order.
Infographic here: Visual representation of a JSF resource library structure and usage.
Here are some common pitfalls to avoid:
  • Forgetting to enable resource handling in faces-config.xml.
  • Using incorrect library or resource names in your JSF pages.
  • Not defining resource dependencies when necessary.

FAQ: Addressing Common Questions

What is the main purpose of the JSF resource library?
The main purpose is to organize and manage reusable resources (CSS, JavaScript, images) in a structured way, promoting code reusability and simplifying maintenance.
How do I enable resource handling in my JSF application?
You enable resource handling by setting the javax.faces.PROJECT\_STAGE context parameter to Development or Production in your faces-config.xml file.
Can I use a CDN with JSF resource libraries?
Yes, you can integrate JSF resource libraries with CDNs by creating a custom ResourceHandler that retrieves resources from the CDN.
What happens if I have resources with the same name in different libraries?
JSF will use the resource from the library that is specified first in the tag or the , , and tags. Be careful to avoid naming conflicts.
By understanding the core concepts, benefits, implementation details, and advanced techniques, you can effectively leverage **JSF resource libraries** to create more maintainable, organized, and performant web applications. Implementing these practices consistently will contribute to a more robust and scalable architecture.

We’ve covered a lot about what the JSF resource library is for and how you should use it. By embracing this structured approach to resource management, you’ll find your JSF projects becoming more manageable, easier to maintain, and ultimately, more successful. Now that you understand the power and flexibility of this tool, experiment with different configurations, explore advanced techniques, and share your findings with the community. Consider exploring related topics such as JSF composite components or custom resource handlers to further enhance your skills and build even more sophisticated web applications. The journey to mastering JSF is a continuous one, and the JSF resource library is an invaluable asset along the way. Explore our other articles on web development.

Question & Answer :
The JSF <h:outputStylesheet>, <h:outputScript> and <h:graphicImage> components have a library attribute. What is this and how should this be used? There are a lot of examples on the web which use it as follows with the common content/file type css, js and img (or image) as library name depending on the tag used:

<h:outputStylesheet library="css" name="style.css" /> <h:outputScript library="js" name="script.js" /> <h:graphicImage library="img" name="logo.png" /> 

How is it useful? The library value in those examples seems to be just repeating whatever is already been represented by the tag name. For a <h:outputStylesheet> it’s based on the tag name already obvious that it represents a “CSS library”. What’s the difference with the following which also just works the same way?

<h:outputStylesheet name="css/style.css" /> <h:outputScript name="js/script.js" /> <h:graphicImage name="img/logo.png" /> 

Also, the generated HTML output is a bit different. Given a context path of /contextname and FacesServlet mapping on an URL pattern of *.xhtml, the former generates the following HTML with the library name as request parameter:

<link rel="stylesheet" type="text/css" href="/contextname/javax.faces.resource/style.css.xhtml?ln=css" /> <script type="text/javascript" src="/contextname/javax.faces.resource/script.js.xhtml?ln=js"></script> <img src="/contextname/javax.faces.resource/logo.png.xhtml?ln=img" alt="" /> 

While the latter generates the following HTML with the library name just in the path of the URI:

<link rel="stylesheet" type="text/css" href="/contextname/javax.faces.resource/css/style.css.xhtml" /> <script type="text/javascript" src="/contextname/javax.faces.resource/js/script.js.xhtml"></script> <img src="/contextname/javax.faces.resource/img/logo.png.xhtml" alt="" /> 

The latter approach makes in hindsight also more sense than the former approach. How exactly is the library attribute then useful?

Actually, all of those examples on the web wherein the common content/file type like “js”, “css”, “img”, etc is been used as library name are misleading.

Real world examples

To start, let’s look at how existing JSF implementations like Mojarra and MyFaces and JSF component libraries like PrimeFaces and OmniFaces use it. No one of them use resource libraries this way. They use it (under the covers, by @ResourceDependency or UIViewRoot#addComponentResource()) the following way:

<h:outputScript library="javax.faces" name="jsf.js" /> <h:outputScript library="primefaces" name="jquery/jquery.js" /> <h:outputScript library="omnifaces" name="omnifaces.js" /> <h:outputScript library="omnifaces" name="fixviewstate.js" /> <h:outputScript library="omnifaces.combined" name="[dynamicname].js" /> <h:outputStylesheet library="primefaces" name="primefaces.css" /> <h:outputStylesheet library="primefaces-aristo" name="theme.css" /> <h:outputStylesheet library="primefaces-vader" name="theme.css" /> 

It should become clear that it basically represents the common library/module/theme name where all of those resources commonly belong to.

Easier identifying

This way it’s so much easier to specify and distinguish where those resources belong to and/or are coming from. Imagine that you happen to have a primefaces.css resource in your own webapp wherein you’re overriding/finetuning some default CSS of PrimeFaces; if PrimeFaces didn’t use a library name for its own primefaces.css, then the PrimeFaces own one wouldn’t be loaded, but instead the webapp-supplied one, which would break the look’n’feel.

Also, when you’re using a custom ResourceHandler, you can also apply more finer grained control over resources coming from a specific library when library is used the right way. If all component libraries would have used “js” for all their JS files, how would the ResourceHandler ever distinguish if it’s coming from a specific component library? Examples are OmniFaces CombinedResourceHandler and GraphicResourceHandler; check the createResource() method wherein the library is checked before delegating to next resource handler in chain. This way they know when to create CombinedResource or GraphicResource for the purpose.

Noted should be that RichFaces did it wrong. It didn’t use any library at all and homebrewed another resource handling layer over it and it’s therefore impossible to programmatically identify RichFaces resources. That’s exactly the reason why OmniFaces CombinedResourceHander had to introduce a reflection-based hack in order to get it to work anyway with RichFaces resources.

Your own webapp

Your own webapp does not necessarily need a resource library. You’d best just omit it.

<h:outputStylesheet name="css/style.css" /> <h:outputScript name="js/script.js" /> <h:graphicImage name="img/logo.png" /> 

Or, if you really need to have one, you can just give it a more sensible common name, like “default” or some company name.

<h:outputStylesheet library="default" name="css/style.css" /> <h:outputScript library="default" name="js/script.js" /> <h:graphicImage library="default" name="img/logo.png" /> 

Or, when the resources are specific to some master Facelets template, you could also give it the name of the template, so that it’s easier to relate each other. In other words, it’s more for self-documentary purposes. E.g. in a /WEB-INF/templates/layout.xhtml template file:

<h:outputStylesheet library="layout" name="css/style.css" /> <h:outputScript library="layout" name="js/script.js" /> 

And a /WEB-INF/templates/admin.xhtml template file:

<h:outputStylesheet library="admin" name="css/style.css" /> <h:outputScript library="admin" name="js/script.js" /> 

For a real world example, check the OmniFaces showcase source code.

Or, when you’d like to share the same resources over multiple webapps and have created a “common” project for that based on the same example as in this answer which is in turn embedded as JAR in webapp’s /WEB-INF/lib, then also reference it as library (name is free to your choice; component libraries like OmniFaces and PrimeFaces also work that way):

<h:outputStylesheet library="common" name="css/style.css" /> <h:outputScript library="common" name="js/script.js" /> <h:graphicImage library="common" name="img/logo.png" /> 

Library versioning

Another main advantage is that you can apply resource library versioning the right way on resources provided by your own webapp (this doesn’t work for resources embedded in a JAR). You can create a direct child subfolder in the library folder with a name in the \d+(_\d+)* pattern to denote the resource library version.

WebContent |-- resources | `-- default | `-- 1_0 | |-- css | | `-- style.css | |-- img | | `-- logo.png | `-- js | `-- script.js : 

When using this markup:

<h:outputStylesheet library="default" name="css/style.css" /> <h:outputScript library="default" name="js/script.js" /> <h:graphicImage library="default" name="img/logo.png" /> 

This will generate the following HTML with the library version as v parameter:

<link rel="stylesheet" type="text/css" href="/contextname/javax.faces.resource/css/style.css.xhtml?ln=default&amp;v=1_0" /> <script type="text/javascript" src="/contextname/javax.faces.resource/js/script.js.xhtml?ln=default&amp;v=1_0"></script> <img src="/contextname/javax.faces.resource/img/logo.png.xhtml?ln=default&amp;v=1_0" alt="" /> 

So, if you have edited/updated some resource, then all you need to do is to copy or rename the version folder into a new value. If you have multiple version folders, then the JSF ResourceHandler will automatically serve the resource from the highest version number, according to numerical ordering rules.

So, when copying/renaming resources/default/1_0/* folder into resources/default/1_1/* like follows:

WebContent |-- resources | `-- default | |-- 1_0 | | : | | | `-- 1_1 | |-- css | | `-- style.css | |-- img | | `-- logo.png | `-- js | `-- script.js : 

Then the last markup example would generate the following HTML:

<link rel="stylesheet" type="text/css" href="/contextname/javax.faces.resource/css/style.css.xhtml?ln=default&amp;v=1_1" /> <script type="text/javascript" src="/contextname/javax.faces.resource/js/script.js.xhtml?ln=default&amp;v=1_1"></script> <img src="/contextname/javax.faces.resource/img/logo.png.xhtml?ln=default&amp;v=1_1" alt="" /> 

This will force the webbrowser to request the resource straight from the server instead of showing the one with the same name from the cache, when the URL with the changed parameter is been requested for the first time. This way the endusers aren’t required to do a hard refresh (Ctrl+F5 and so on) when they need to retrieve the updated CSS/JS resource.

Please note that library versioning is not possible for resources enclosed in a JAR file. You’d need a custom ResourceHandler. See also How to use JSF versioning for resources in jar.

See also: