S02L03 – Adding Fragments, header, footers to web pages

Adding Fragments, Headers, and Footers in Spring Boot

Table of Contents:

  1. Introduction
  2. Understanding Fragments in Spring Boot
  3. Adding Headers and Footers in Web Pages
  4. Code Implementation and Example
  5. Conclusion

Introduction:

In modern web development, reusing components such as headers and footers across multiple pages helps maintain consistency and reduces repetitive code. In this article, we’ll explore how to add fragments (headers and footers) to your web pages using Spring Boot. You’ll learn how to structure your application, write code to achieve reusable layouts, and enhance your web pages effectively.

Understanding Fragments in Spring Boot:

Fragments in web development refer to reusable components like headers, footers, or any part of a page that you wish to include across multiple pages. Spring Boot, paired with Thymeleaf, offers an easy way to implement such fragments by allowing you to define these sections in separate files and include them as needed.

When to Use Fragments:

  • Maintainability: Fragments ensure that any change to the header or footer is automatically applied to all pages.
  • Reusability: Allows you to reuse code across different web pages.
  • Consistency: Ensures a uniform look across your website.

Adding Headers and Footers in Web Pages:

Header and Footer Structure:

We’ll create a header, footer, and main content sections for your web pages. These sections will reside in separate files, and we’ll use Thymeleaf to include them dynamically in the main HTML file.

Code Implementation and Example:

Main Application Code:

This is a basic Spring Boot starter class, and it initiates the application using the SpringApplication.run() method.

Home Controller:

This controller defines three routes:

  • /home: Directs to the home page.
  • /about: Displays the about page.
  • /book: Navigates to a book page.

Fragment Inclusion Example:

In Thymeleaf, fragments are included using the th:replace or th:include directive.

This is an example of a basic HTML page where the header and footer are included as fragments from separate files.

Detailed Explanation:

1. Header and Footer Fragment Files:

You need to create two fragment files header.html and footer.html inside the resources/templates/fragments/ directory.

Header (header.html):

Website Header

Footer (footer.html):

© 2024 Your Company

These files represent the header and footer, which can be included in various pages.

2. Main Content Page:

In the example above, the home.html file includes the header and footer using th:replace. The th:replace directive dynamically inserts the content of the specified fragment into the location where it’s called.

Conclusion:

By using Thymeleaf fragments in Spring Boot, you can create a modular and maintainable web page structure, reusing code such as headers and footers across multiple pages. This not only improves maintainability but also ensures consistency across your site.