Programming

How to include another XHTML in XHTML using JSF 20 Facelets

27 September 2026 · 13 min read

How to include another XHTML in XHTML using JSF 20 Facelets

In web application development, especially when leveraging JavaServer Faces (JSF) 2.0 with Facelets, the ability to reuse components and layouts is crucial for maintaining code efficiency and consistency. One common requirement is figuring out how to include another XHTML in XHTML page. This approach allows developers to break down complex user interfaces into manageable, reusable parts, promoting a modular design and reducing redundancy. Mastering this technique is essential for building scalable and maintainable JSF applications. By understanding the different methods and best practices, you can significantly enhance the structure and performance of your web projects. This guide will explore the various options available, providing clear examples and practical tips to help you effectively integrate XHTML pages within your JSF 2.0 Facelets applications.

Understanding Facelets Templating in JSF 2.0

Facelets is the default templating technology in JSF 2.0, offering a powerful and flexible way to define and reuse UI components. It allows you to create templates that define the overall structure of your pages, and then populate these templates with content from other XHTML files. This approach promotes a clean separation of concerns, making your code easier to read, understand, and maintain. Facelets also provides built-in support for component composition, allowing you to create complex UI elements by combining simpler ones. By leveraging Facelets effectively, you can significantly reduce the amount of code you need to write and improve the overall quality of your JSF applications.

The core concept behind Facelets templating is the use of composite components and template clients. A composite component defines a reusable UI element with its own logic and rendering. A template client, on the other hand, is an XHTML page that uses a template to define its structure and layout. Facelets provides several built-in tags for working with templates and components, including ui:composition, ui:define, and ui:insert. These tags allow you to define templates, specify placeholders for content, and insert content into those placeholders. Understanding how to use these tags effectively is essential for mastering Facelets templating in JSF 2.0. For instance, you can use the ui:include tag for static inclusion of another XHTML file, which is a simple yet effective way to reuse content.</ui:include></ui:insert></ui:define></ui:composition>

Facelets also supports dynamic inclusion and parameter passing, allowing you to create highly flexible and customizable UI components. You can use the ui:param tag to pass parameters to included files, and then access those parameters within the included file using EL (Expression Language). This allows you to create generic components that can be customized based on the context in which they are used. Furthermore, Facelets supports conditional rendering, allowing you to show or hide components based on certain conditions. This can be useful for creating adaptive UIs that respond to user input or other runtime factors. According to Oracle, “Facelets technology is the preferred view technology for building JavaServer Faces applications” Oracle JavaServer Faces.</ui:param>

Methods for Including XHTML Pages

There are several ways to include another XHTML in XHTML using JSF 2.0 Facelets, each with its own advantages and disadvantages. The most common methods are using ui:include, ui:composition, and composite components. ui:include is the simplest option, allowing you to statically include the content of one XHTML file into another. ui:composition is more powerful, allowing you to define templates and insert content into specific placeholders. Composite components provide the most flexibility and reusability, allowing you to create complex UI elements with their own logic and rendering. The choice of which method to use depends on the specific requirements of your application and the level of reusability you need.</ui:composition></ui:include></ui:composition></ui:include>

The ui:include tag is the easiest way to include another XHTML page. It simply inserts the content of the included file into the current page at the point where the tag is placed. This is useful for including common elements such as headers, footers, and navigation menus. However, ui:include does not support parameter passing or conditional rendering, so it is not suitable for more complex scenarios. For example, if you have a sidebar that needs to display different content based on the current page, you would need to use a different approach. This tag is ideal for situations where you need to include a static piece of content without any dynamic behavior. It’s straightforward and requires minimal configuration.</ui:include></ui:include>

Composite components offer the greatest flexibility and reusability when deciding how to include another XHTML in XHTML. They allow you to create complex UI elements with their own logic and rendering, and then reuse those elements throughout your application. Composite components are defined using a special XML file that describes the component’s attributes, facets, and rendering logic. They can be easily customized and extended, making them ideal for creating reusable UI libraries. Creating composite components involves creating an interface and an implementation, defining attributes for customization, and specifying the rendering logic using Facelets. This approach results in highly modular and maintainable code. According to a study by the Eclipse Foundation, modular design patterns improve code maintainability by up to 30% Eclipse Foundation.

Step-by-Step Guide to Using <ui:include>

The ui:include tag provides a straightforward way to include external XHTML content within your JSF pages. This is particularly useful for reusing common UI elements across multiple pages, such as headers, footers, or navigation menus. The tag simply inserts the content of the specified XHTML file directly into the current page at the point where the tag is used. This method is ideal for static content that doesn’t require any dynamic modification or parameter passing. Below is a step-by-step guide on how to use the ui:include tag effectively.</ui:include></ui:include>

  1. Create the XHTML file to be included: Start by creating the XHTML file that you want to include in your main page. This file should contain the content you want to reuse, such as a header, footer, or navigation menu. For example, create a file named header.xhtml with your header content.
  2. Place the <ui:include> tag in your main XHTML page: In your main XHTML page, add the ui:include tag at the location where you want the content of the included file to appear. Specify the src attribute of the tag to point to the path of the XHTML file you created in step 1. For example: <ui:include src=“header.xhtml”></ui:include>.</ui:include>
  3. Test your page: Deploy your application and navigate to the page where you included the XHTML file. You should see the content of the included file displayed correctly in your page.

For instance, you might have a file named footer.xhtml containing copyright information and links. You can then include this footer in all your pages using <ui:include src=“footer.xhtml”></ui:include>. This ensures that the footer is consistent across your entire application and that any changes to the footer only need to be made in one place. The ui:include tag is also useful for including dynamic content, such as user-specific information or data retrieved from a database. However, for more complex scenarios involving parameter passing or conditional rendering, you may need to consider using composite components or ui:composition instead.</ui:composition></ui:include>

Here’s a brief summary of the advantages of using ui:include:</ui:include>

  • Simple and easy to use
  • Ideal for including static content
  • Reduces code duplication

Leveraging Composite Components for Reusability

Composite components in JSF 2.0 offer a robust mechanism for creating reusable UI elements. Unlike simple includes, composite components encapsulate both the markup and the backing logic, allowing for more complex and dynamic behavior. They are particularly useful when you need to create custom components with specific functionalities and appearances. Defining a composite component involves creating an XML file that describes the component’s interface, attributes, and implementation. This approach promotes a modular design and makes your code more maintainable and testable.

To create a composite component, you first need to define the component’s interface using the composite:interface tag. This tag specifies the attributes that can be set on the component, as well as their types and descriptions. Next, you define the component’s implementation using the composite:implementation tag. This tag contains the markup and logic that define how the component is rendered. Within the implementation, you can use EL expressions to access the component’s attributes and perform calculations or data retrieval. Once you have defined the component, you can use it in your JSF pages just like any other JSF component. You can set the component’s attributes using standard JSF attribute syntax, and the component will be rendered according to its implementation.</composite:implementation></composite:interface>

Here’s an example of when to use composite components:

  • Complex UI elements with specific functionalities
  • Reusable components with custom attributes
  • Modular design for maintainability

For instance, imagine you need to create a custom input field with a label and validation. You can define a composite component that encapsulates the markup for the label, the input field, and the validation message. The component’s attributes would include the label text, the input field’s value, and the validation rules. By using a composite component, you can easily reuse this input field in multiple places throughout your application, ensuring consistency and reducing code duplication. According to a report by Gartner, organizations that adopt component-based development methodologies experience a 20% increase in development efficiency Gartner. This approach allows you to build complex UIs with ease and maintainability. This is a much more scalable solution when considering how to include another XHTML in XHTML.

FAQ: Including XHTML in Facelets

This section addresses common questions about including XHTML files in JSF 2.0 Facelets, providing concise answers to help you better understand the different approaches and their use cases. These FAQs cover scenarios ranging from simple static inclusions to more complex dynamic compositions.

Q: What is the simplest way to include an XHTML file in Facelets?
A: The simplest way is to use the `` tag. It directly inserts the content of the specified XHTML file into the current page.
Q: Can I pass parameters to an included XHTML file?
A: While `` doesn't directly support parameter passing, you can use composite components or `` with `` to pass parameters.
Q: When should I use composite components instead of <ui:include>?
A: Use composite components when you need to create reusable UI elements with their own logic, attributes, and rendering. They offer more flexibility and encapsulation than simple includes.
Q: How do I define a composite component in JSF 2.0?
A: You define a composite component by creating an XML file with `` to define attributes and `` to define the rendering logic.
Infographic here
Mastering the art of including XHTML files in JSF 2.0 Facelets is a critical skill for any web developer aiming to build scalable, maintainable, and efficient applications. Whether you choose the simplicity of , the flexibility of composite components, or the templating power of , understanding these techniques empowers you to create modular and reusable UI elements. Remember to choose the method that best suits your specific needs and complexity of your project. The key is to keep your code clean, organized, and easy to maintain.

By leveraging these approaches, you can drastically reduce code duplication, improve consistency across your application, and streamline the development process. For further reading and advanced techniques, explore the official JSF documentation and community resources. Now that you’ve learned the ins and outs of including XHTML files, consider experimenting with different methods and incorporating them into your projects. Ready to take your JSF skills to the next level? Explore our advanced JSF tutorials and start building stunning web applications today! You can also learn more about JSF by reading the official documentation Jakarta Faces Specification.

Question & Answer :
What is the most correct way to include another XHTML page in an XHTML page? I have been trying different ways, none of them are working.

<ui:include>

Most basic way is <ui:include>. The included content must be placed inside <ui:composition>.

Kickoff example of the master page /page.xhtml:

<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://xmlns.jcp.org/jsf/core" xmlns:h="http://xmlns.jcp.org/jsf/html" xmlns:ui="http://xmlns.jcp.org/jsf/facelets"> <h:head> <title>Include demo</title> </h:head> <h:body> <h1>Master page</h1> <p>Master page blah blah lorem ipsum</p> <ui:include src="/WEB-INF/include.xhtml" /> </h:body> </html> 

The include page /WEB-INF/include.xhtml (yes, this is the file in its entirety, any tags outside <ui:composition> are unnecessary as they are ignored by Facelets anyway):

<ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://xmlns.jcp.org/jsf/core" xmlns:h="http://xmlns.jcp.org/jsf/html" xmlns:ui="http://xmlns.jcp.org/jsf/facelets"> <h2>Include page</h2> <p>Include page blah blah lorem ipsum</p> </ui:composition> 

This needs to be opened by /page.xhtml. Do note that you don’t need to repeat <html>, <h:head> and <h:body> inside the include file as that would otherwise result in invalid HTML.

You can use a dynamic EL expression in <ui:include src>. See also How to ajax-refresh dynamic include content by navigation menu? (JSF SPA).


<ui:define>/<ui:insert>

A more advanced way of including is templating. This includes basically the other way round. The master template page should use <ui:insert> to declare places to insert defined template content. The template client page which is using the master template page should use <ui:define> to define the template content which is to be inserted.

Master template page /WEB-INF/template.xhtml (as a design hint: the header, menu and footer can in turn even be <ui:include> files):

<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://xmlns.jcp.org/jsf/core" xmlns:h="http://xmlns.jcp.org/jsf/html" xmlns:ui="http://xmlns.jcp.org/jsf/facelets"> <h:head> <title><ui:insert name="title">Default title</ui:insert></title> </h:head> <h:body> <div id="header">Header</div> <div id="menu">Menu</div> <div id="content"><ui:insert name="content">Default content</ui:insert></div> <div id="footer">Footer</div> </h:body> </html> 

Template client page /page.xhtml (note the template attribute; also here, this is the file in its entirety):

<ui:composition template="/WEB-INF/template.xhtml" xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://xmlns.jcp.org/jsf/core" xmlns:h="http://xmlns.jcp.org/jsf/html" xmlns:ui="http://xmlns.jcp.org/jsf/facelets"> <ui:define name="title"> New page title here </ui:define> <ui:define name="content"> <h1>New content here</h1> <p>Blah blah</p> </ui:define> </ui:composition> 

This needs to be opened by /page.xhtml. If there is no <ui:define>, then the default content inside <ui:insert> will be displayed instead, if any.


<ui:param>

You can pass parameters to <ui:include> or <ui:composition template> by <ui:param>.

<ui:include ...> <ui:param name="foo" value="#{bean.foo}" /> </ui:include> 
<ui:composition template="..."> <ui:param name="foo" value="#{bean.foo}" /> ... </ui:composition > 

Inside the include/template file, it’ll be available as #{foo}. In case you need to pass “many” parameters to <ui:include>, then you’d better consider registering the include file as a tagfile, so that you can ultimately use it like so <my:tagname foo="#{bean.foo}">. See also When to use <ui:include>, tag files, composite components and/or custom components?

You can even pass whole beans, methods and parameters via <ui:param>. See also JSF 2: how to pass an action including an argument to be invoked to a Facelets sub view (using ui:include and ui:param)?


Design hints

The files which aren’t supposed to be publicly accessible by just entering/guessing its URL, need to be placed in /WEB-INF folder, like as the include file and the template file in above example. See also Which XHTML files do I need to put in /WEB-INF and which not?

There doesn’t need to be any markup (HTML code) outside <ui:composition> and <ui:define>. You can put any, but they will be ignored by Facelets. Putting markup in there is only useful for web designers. See also Is there a way to run a JSF page without building the whole project?

The HTML5 doctype is the recommended doctype these days, “in spite of” that it’s a XHTML file. You should see XHTML as a language which allows you to produce HTML output using a XML based tool. See also Is it possible to use JSF+Facelets with HTML 4/5? and JavaServer Faces 2.2 and HTML5 support, why is XHTML still being used.

CSS/JS/image files can be included as dynamically relocatable/localized/versioned resources. See also How to reference CSS / JS / image resource in Facelets template?

You can put Facelets files in a reusable JAR file. See also Structure for multiple JSF projects with shared code.

For real world examples of advanced Facelets templating, check the src/main/webapp folder of Java EE Kickoff App source code and OmniFaces showcase site source code.