Skip to main content

Command Palette

Search for a command to run...

Struts Tiles Plugin in Eclipse Example

Updated
Struts Tiles Plugin in Eclipse Example
Y

Tech Lead & Architect | 13+ Years in Cloud, Backend, and AI - Experienced software engineer with expertise in Java, Spring Boot, Microservices, Angular, React, Kafka, DevOps, Python, PySpark, Databricks, and Generative AI. Certified in TOGAF, AWS, and Google Cloud. Passionate about building scalable, secure, and high-performance systems. Enthusiast in Data Engineering & Agentic AI. Author of 1,200+ technical articles sharing insights across diverse tech stacks.

Date: 2017-09-22

The Struts Tiles framework simplifies web application development by providing a structured approach to creating consistent layouts. Imagine building a website with a common header, footer, and menu across all pages. Manually adding these elements to every page is tedious and prone to errors. If you need to update the header, you'd have to modify every single page. This is where Struts Tiles shines. It allows developers to define these reusable components once and then apply them across the entire application. This framework is not only efficient for managing consistent page layouts but also for componentizing the presentation layer, meaning breaking down the visual elements into smaller, manageable parts.

Traditionally, there are two primary methods for handling consistent website layouts. The first involves directly embedding the header and footer code within each page's source code. While seemingly straightforward, this method is extremely inefficient. Any change to the header or footer necessitates modification of every single page, creating significant maintenance overhead and increasing the risk of introducing inconsistencies. The second method is more elegant, employing JSP's include tag to insert the header and footer from separate files into each page. This approach improves maintenance, as changes only need to be made in the header and footer files themselves. However, this method still requires explicit inclusion for each component on every page.

The Struts Tiles framework offers a superior solution. It uses a template-based approach, storing the overall layout structure in a separate file. This layout acts as a container, holding placeholders for reusable components like the header, footer, and menu. Instead of embedding the components directly, each page specifies which components to include in the layout's placeholders. This makes updating the layout incredibly easy. Changing the header only requires modifying the header component and the main layout file; all other pages automatically reflect the update. Adding a new component, such as a menu, is equally straightforward; simply define the menu component and add it to the layout, and all pages will automatically incorporate it.

The integration of Struts Tiles with the Struts 2 framework enhances this efficiency. Through configuration files (typically struts.xml and tiles.xml), developers specify how the Tiles framework should work within their Struts 2 application. The struts.xml file manages the mapping between URLs and actions within the Struts 2 framework. The tiles.xml file is crucial to Struts Tiles; it defines the layout templates, individual components (like headers, footers, and menus), and how these components fit together to form complete pages. These files act as blueprints, instructing the framework on how to assemble the web pages dynamically.

Consider a typical web application layout. You have a header (containing branding and navigation), a body (showing the page-specific content), and a footer (with copyright and contact information). Using Struts Tiles, you would define these sections as individual components. Then, your main layout template would define where these components should appear. A page displaying product details would then just specify which component should populate the body section of the layout, using the header and footer components automatically.

Setting up a Struts Tiles application usually involves creating a Maven project (a project management tool). Maven simplifies the process of managing dependencies, automatically downloading the necessary libraries (such as Struts 2 Core, Struts 2 Tiles, and related tag libraries). The project structure is key. You'll have folders for your Java source code, resources (like configuration files), and web application files (JSP pages, static content). Java classes, called Actions, handle the application's logic, interacting with models (data) and ultimately returning data to be displayed on a view (JSP pages). The struts.xml file maps URLs to these Actions. The tiles.xml file defines the Tiles layout and its components. Finally, JSP files define the actual visual components like headers, footers, and page-specific content.

The JSP files are crucial. A base layout JSP file acts as the main template, with placeholders where components like headers, footers, and the page's main content will be inserted. Separate JSP files are created for each component, allowing for independent maintenance and modification. The tiles.xml file uses these JSPs as definitions for building the final page. For instance, a tiles.xml definition might specify that the baseLayout.jsp should be used, with header.jsp for the header and body.jsp for the body content.

Deploying the application involves using a servlet container such as Tomcat. Once deployed, the application's pages can be accessed through URLs, with the Struts 2 framework and the Tiles plugin working together to dynamically render the pages based on the configuration files and the defined layout components. When a user requests a page, the Struts 2 framework routes the request, the Tiles plugin assembles the page according to the layout definitions, and the result is presented to the user.

The error message "HTTP Status [404] – [Not Found] /Struts2Tiles/welcomeLink.action" suggests a configuration problem. This error means the server cannot find the requested resource. This is commonly caused by incorrect URL mapping in the struts.xml file, problems in the tiles.xml configuration (preventing the correct page assembly), or incorrect deployment of the web application to the server. Troubleshooting this error would involve carefully checking the URL mapping in struts.xml to ensure it correctly points to the appropriate action class, verifying the paths and filenames within tiles.xml are accurate, and making sure the application is deployed correctly to the Tomcat server, including the placement of JSP files and configuration files in their respective directories within the deployed web application.

Read more

More from this blog

The Engineering Orbit

1174 posts

The Engineering Orbit shares expert insights, tutorials, and articles on the latest in engineering and tech to empower professionals and enthusiasts in their journey towards innovation.