Struts Tiles

Tiles in Struts helps to provide an easy and common look for Struts application. Struts Tiles is a user interface framework. The steps for creating a tiles application are explained below.

Eg: – tiles-defs.xml

<!DOCTYPE tiles-definitions PUBLIC
“-//Apache Software Foundation//DTD Tiles Configuration 1.3//EN”
“http://struts.apache.org/dtds/tiles-config_1_3.dtd”>
<tiles-definitions>
<definition name=“company-template” path=“/pages/tiles/common-layout.jsp”>
<put name=“header” value=“/pages/tiles/template-red/header.jsp” />
<put name=“footer” value=“/pages/tiles/template-red/footer.jsp” />
</definition>
</tiles-definitions>

Tiles support the 2 different concepts.

  • Reuse
  • Developers can define a template for the web site and then use this layout to change the content of the web site.

Eg: – Consider we develop a web site having 1000 of pages static content and dynamically generate pages. If we want to change the layout of the web site we use the tiles framework to design template for the web site. This template helps to change the content of our web site. In future we want to change any layout we made a change only in one page and this change will affect whole pages in our web site.

We can specify the template in three different ways.

  1. Define anonymously in a JSP page by using the tiles insert tag.
  2. Define in a JSP using the tiles definition tag.
  3. Define through XML file and loaded from a factory using the tiles insert tag.

Steps used for tiles creation application: –

1)      Add the Tiles Tag Library Descriptor (TLD) file to the web.xml: –

Before using the tiles we need web.xml file.

Syntax: –

<taglib>
<taglib-uri>/tags/struts-tiles</taglib-uri>
<taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
</taglib>

2)      Create layout JSPs: –

The web layouts can be divided into 4 parts.

  1. Banner
  2. Left Navigation Bar
  3. Content Area
  4. Bottom of the page for copy right information.

The same layout can be reused any number of times by specifying different JSP pages.

Syntax: –

<%@ page language=“java” %>
<%@ taglib uri=“/WEB-INF/struts-tiles.tld” prefix=“tiles” %>
<html>

<head>
<title><tiles:getAsString name=“title” ignore=“true”/></title>
</head>
<body>
<table border=“1” cellpadding=“0” cellspacing=“0”
width=“100%” bordercolor=“#000000”
bgcolor=“#E7FDFE”>
<tr>
<td width=“100%” colspan=“2” valign=“top”><tiles:insert attribute=“header”/></td>
</tr>
<tr>
<td width=“23%”><tiles:insert attribute=“menu”/></td>
<td width=“77%” valign=“top” valign=“top”><tiles:insert attribute=“body”/></td>
</tr>
<tr>
<td width=“100%” colspan=“2” valign=“top”><tiles:insert attribute=“bottom”/></td>
</tr>
</table>
</body>
</html>

/WEB-INF/struts-tiles.tld references TLD. The string parameters used to display title using the tiles:getAsString tag. If the attribute ignore=“true” then tiles ignore the missing parameter and the tiles framework will through the exception in case the parameter is missing.

To insert the content JSP, the tiles:insert tag is used, which inserts any page or web resources that framework refers to as a title.

Eg: –

<tiles:insert attribute=“header”/> inserts the header web page.

3)      Develop the web pages using layouts: –

Tiles layout can be used to display the content page. For accomplishing this we use two different jsp file. One file is used to store the content and other file is used for displaying the content.

Eg: – Content.jsp

<p align=“left”><font size=“5”>Welcome to the Title Tutorial</font></p>
<p align=“left”><font size=“5”>This is the content page</font></p>

The content page define the content of the page and this page may be static or dynamic depends on our requirement.

Contentdispaly.jsp

<%@ page language=“java” %>
<%@ taglib uri=“/WEB-INF/struts-tiles.tld” prefix=“tiles” %>
 <tiles:insert page=“/tiles/template.jsp” flush=“true”>
<tiles:put name=“title” type=“string” value=“Welcome” />
<tiles:put name=“header” value=“/tiles/top.jsp” />
<tiles:put name=“menu” value=“/tiles/left.jsp” />
<tiles:put name=“body” value=“/tiles/content.jsp” />
<tiles:put name=“bottom” value=“/tiles/bottom.jsp” />
</tiles:insert>

4)      Repackage, run and test application: –

The below code in the index.jsp helps to test the tile.

Syntax: –

<li>
<html:link page=“/tiles/example.jsp”>Tiles Example</html:link>
<br>
Example of creating first tile application.
</li>

 

Features of struts tiles

1)      Screen definitions: –

  1. Create a screen by assembling tiles like header, footer, menu, body, etc.
  2. Definitions can take place :
    1. i.      in a centralized XML file
    2. ii.      directly in JSP pages
    3. iii.      programmatically in Actions
  3. Definitions provide an inheritance mechanism: Definition can extend another one, and override parameters.

2)      Simplify the creation of similar pages.

3)      Layouts

  1. Define common page layouts and reuse them across your website.
  2. Define menu layouts, and use them by passing lists of items and links.
  3. Define a portal layout; use it by passing a list of tiles (pages) to show.
  4. Reuse existing layouts, or define our own.

4)      Increase flexibility and ease of maintenance compared to <jsp:include…/>

5)      Dynamic page building

  1. Tiles can be gathered dynamically during page reload. It is possible to change any attribute: layout, list of tiles in portal, list of menu items, etc.

6)      Reuse of tiles  Components

  1. If well defined, a tile can be reused across multiple applications.
  2. Dynamic attributes are used to parameterize tiles.
  3. It is possible to define a library of reusable tiles.
  4. Build a page by assembling predefined components, giving them appropriate parameters.
  5. It avoid the repetitions

7)      Internationalization

  1. It is possible to load different tiles according to Locale.
  2. A mechanism similar to Java properties files is used for definitions files: you can have one definition file per Locale. The appropriate definition is loaded according to the current Locale.

8)      Multi-channels

  1. It is possible to load different Tiles according to a key.
  2. For example, the key could represent user privileges, browser type, arbitrary name stored in session, etc.
  3. A mechanism similar to Java properties files is used for definitions files: you can have one definition file per key. The appropriate definition is loaded according to the key.

If we want to use strut framework we should declare the TilesPlugin plug-in class in the strut configuration file.

Eg: – struts-config.xml

<?xml version=“1.0” encoding=“UTF-8”?>
<!DOCTYPE struts-config PUBLIC
“-//Apache Software Foundation//DTD Struts Configuration 1.3//EN”
“http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd”>
<struts-config>
<action-mappings>
<action
path=“/User”
type=“org.apache.struts.actions.ForwardAction”
parameter=“/pages/user/user-form.jsp”/>
</action-mappings>
<plug-in className=“org.apache.struts.tiles.TilesPlugin” >
<set-property property=“definitions-config”
value=“/WEB-INF/tiles-defs.xml”/>
</plug-in>
</struts-config>