In this tutorial you will see how to create a Tiles layout in Struts.
Tiles is used to create reusable presentation components. With Tiles, we first define a base layout with different sections after that we define which jsp page should fill in the corresponding regions in an exteranl configuration file. The same layout can be reused any number of times by specifying different jsp pages.
To use Tiles in the Struts application, we need to add the following <plug-in> definition to the struts-config.xml file.
Lets first design the base layout page using tiles. The base layout page is a normal jsp page, which defines different sections. A region is defined using the <tiles:insert> tag. The attribute value hold the name of the region.
The layout shown above can be created using the following code.
The name attribute of the put tag specifies the region in the baseLayout in which the corresponding page specified by the value attribute should be displayed. In our example the header region is occupied by the header.jsp page, the menu part is occupied by the menu.jsp page, the body part by the body.jsp page and the footer by the footer.jsp page. The only section that will be changing when the user request a different page is the body part.
On executing the application the following home page is displayed. The table border is set to 1 inorder to give a clear seperation between the regions.
On clicking the links in the left menu, only the body part of the page should change. So instead of forwarding each link to a jsp page, we forward it to a Tile definition.
tiles-defs.xml file contains all the Tile definitions. A Tile definition can be added in the following way.
The name of the Tile definition is "baseLayout" and it contains one jsp page for each region. Since the title region is specified using getAsString tag, we provide a String variable instead of a jsp page. When an action is forwarded to the Tile definition baseLayout, then the baseLayout.jsp page will be displayed with corresponding jsp pages in the Tile definition
The powerful and useful feature of the Tile definition is the ability to extend an other Tile definition. In our example only the tilte and the body regions are going to change for each link in the left menu. So it is a good practice to create an new Tile definition which extends the baseLayout, with different values for title and body regions.