Struts tiles

Struts tiles is widely used to provide a easy framework for developing UI applications.Tiles in Struts helps to provide an easy and common look for Struts application. Struts Tiles is a user interface framework.
For mapping a request URL to a particular Action class, the ActionServlet class is to be provided with suitable information using ActionMapping interface. Struts provides a declarative way in the form of XML configuration file to pass this information to the controller servlet. The controller servlet parses this file and gets the required information to handle the incoming request. The configuration file usually named as struts-config.xml file should be defined and placed in the WEB-INF folder of the application. The controller servlet knows the configuration file from the web.xml where it is declared as follows :

<init-param >
<param-name >
config
</param-name >
<param-value >
/WEB-INF/struts-config.xml
</param-value >
</init-param >

Following are the important elements of struts-config.xml file :

<!DOCTYPE struts-config PUBLIC -//Apache Software Foundation//DTD Struts Configuration 1.2//EN” “http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd” >
<struts-config >
<form-beans >
……………………….
<form-bean type=”com.strutstutorial.colors.ItemColorForm” name=”itemcolorform”/ >
</form-beans >
<global-exceptions >
………………………………
</global-exceptions >
<global-forwards >
…………………………
</global-forwards >
<action-mappings >
……………………………..
</action-mappings >
</struts-config >

<form-beans> :

Contains definition of the beans used by the application. A <form-bean> describes an instance of ActionForm class that will be used to pass the form data to the action class. Important attributes of <form-bean> are as follows:

name : A unique identifier for the bean that will be used as reference in action mappings.
type : Fully qualified class name of the ActionForm subclass used to defined this bean. <global-forwards>:

This elements define the forwards that can be used by all action mappings. A Forward is an instance of ActionForward class that maps a resource name to the actual resource in the application. These forward names are used in application to forward the control to the next view based on the outcome of execution of a request. Important attributes of a <forward> element are as follows :

name : The logical name of the forward. This will be used in the application to forward the request to the resource referenced by this forward.
path : The relative path to the resource (typically jsp page).
redirect : true or false. If true, a redirect is carried out to the resource instead of forward.

Creating Tile Layout

So far everything is familliar and its same as what we had done for any struts application. To create the view component whcih uses Tile Framework, we need to create Tile Layout. We will be doing following steps to create a tile layout“generalLayout.jsp”.

  • Import the tiles taglib into the JSP and any other taglibs you need with the taglib directive.
  • Use string parameters to display things like the page title using the tiles:getAsString tag.
  • Insert the tiles in the correct regions of the layout using the tiles:insert tag.
  • Pass any needed parameters to the internal tiles using tiles:put — a subtag of tiles:insert.

Step 1

<%@ taglib uri=”http://struts.apache.org/tags-html” prefix=”html” %>
<%@ taglib uri=”http://struts.apache.org/tags-bean” prefix=”bean” %>
<%@ taglib uri=”http://struts.apache.org/tags-tiles” prefix=”tiles” %>

Step 2

<html>
<head>
<title>
<tiles:getAsString name=”title” ignore=”true”/>
</title>
</head>

Note :
1.we will be defining Title for the webpage in attribute “title” in tile layout (as every page has title so we will be defining this variable in tile layout), depending on the page.
2. ignore=”true” means if the variable not found ignore. otherwise if ignore set to false, and the attribure not found then the tile framework throw exception

Step 3

To insert another tile or component we will be using <tile:insert> tag, like :

<tiles:insert attribute=”header”/>
<tiles:insert attribute=”body”/>
<tiles:insert attribute=”footer”/>

Step 4

As mentioned above we have defined a variable “title” , and this variable is defined like this
<tiles:put name=”title” type=”string” value=”Item Selection” />

Complete Tile Layout (generalLayout.jsp)

<%@ taglib uri=”http://struts.apache.org/tags-html” prefix=”html” %>
<%@ taglib uri=”http://struts.apache.org/tags-bean” prefix=”bean” %>
<%@ taglib uri=”http://struts.apache.org/tags-tiles” prefix=”tiles” %>
<html>
<head>
<title>
<tiles:getAsString name=”title” ignore=”true”/>
</title>
</head>
<body>
<table width=”500″ border=”0″ cellspacing=”0″ cellpadding=”0″>
<tr>
<td height=”68″ width=”2000″>
<tiles:insert attribute=”header” ignore=”true”/>
</td>
</tr>
<tr>
<td>
<div align=”center”>
<tiles:insert attribute=”body”/>
</div>
</td>
</tr>
<tr>
<td>
<tiles:insert attribute=”footer” ignore=”true”/>
</td>
</tr>
</table>
</body>
</html>

Copy the this jsp file as generalLayout.jsp under web application folder folder(web-apps/struts-blank)

Writing tiles

We have done with creating required components for struts application, lets create few jsp which will invoke this component to achieve funtionality.

We will be creating four tiles.

  1. header.jsp – header for the page
  2. footer.jsp – footer for the page
  3. itemselect.jsp – having form to select the item color
  4. itemlist.jsp- list all the items

header.jsp:

Sample XYZ Product Co.

footer.jsp:

footer

itemselect.jsp:

<%@ taglib uri=”http://struts.apache.org/tags-html” prefix=”html”% >
<%@ taglib uri=”http://struts.apache.org/tags-bean” prefix=”bean”% >
<html:html >
<head >
<title >Select Item </title >
</head >
<body >
<%
java.util.ArrayList list= new java.util.ArrayList();
list.add(“Red”);
list.add(“Blue”);
list.add(“Green”);
request.setAttribute(“colors”,list);
%><html:form action=”/selectitem” method=”post” >
<table>
<tr>
<td>Select the color :</td>
<td>
<html:select property=”color”>
<html:options name=”colors” />
</html:select>
</td>
<td><html:submit value=”Get Items”/></td>
</table>
</html:form >
</body >
</html:html >

itemlist.jsp:

<%@ taglib uri=”http://struts.apache.org/tags-html” prefix=”html”% >
<%@ taglib uri=”http://struts.apache.org/tags-bean” prefix=”bean”% >
<%@ taglib uri=”http://struts.apache.org/tags-logic” prefix=”logic”% >
<html:html >
<head >
<title >Item List </title >
</head >
<body >
<logic:present name=”itemsList”>
Items available for the selected color <b><bean:write name=”selectedColor”/></b> :<br/>
<logic:iterate id=”item” name=”itemsList”>
<b><bean:write name=”item”/></b><br/>
</logic:iterate>
</logic:present>
<logic:notPresent name=”itemsList”>
No Items available for selected color <bean:write name=”selectedColor”/>
</logic:notPresent>
</body >
</html:html >

Writing JSP pages

We are done with creating tile layout (generalLayout.jsp) and tiles (header.jsp.footer.jsp, itemselect.jsp, itemlist.jsp). Now we have to start integrating these individual tile into tile layout to generate the final web page. we will be having two web page

  1. itemselectpage.jsp – having form to select the item color
  2. itemlistpage.jsp- list all the items

itemselectpage.jsp:

<%@ taglib uri=”/WEB-INF/struts-tiles.tld” prefix=”tiles” %>
<tiles:insert page=”/generalLayout.jsp” flush=”true”>
<tiles:put name=”title” type=”string” value=”Item Selection” />
<tiles:put name=”header” value=”/header.jsp” />
<tiles:put name=”footer” value=”/footer.jsp” />
<tiles:put name=”content” value=”/itemselect.jsp”/>
</tiles:insert>

itemlistpage.jsp:

<%@ taglib uri=”/WEB-INF/struts-tiles.tld” prefix=”tiles” %>
<tiles:insert page=”/generalLayout.jsp” flush=”true”>
<tiles:put name=”title” type=”string” value=”Item Listing” />
<tiles:put name=”header” value=”/header.jsp” />
<tiles:put name=”footer” value=”/footer.jsp” />
<tiles:put name=”content” value=”/itemlist.jsp”/>
</tiles:insert>

Action Mapping Configuration

Following are the important elements of struts-config.xml file :

<!DOCTYPE struts-config PUBLIC -//Apache Software Foundation//DTD Struts Configuration 1.2//EN” “http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd” >
<struts-config >
<form-beans >
……………………….
<form-bean type=”com.strutstutorial.colors.ItemColorForm” name=”itemcolorform”/ >
</form-beans >
<global-exceptions >
………………………………
</global-exceptions >
<global-forwards >
…………………………
</global-forwards >
<action-mappings >
……………………………..
<action input=”/itemselectpage.jsp” type=”com.strutstutorial.colors.ItemColorAction” validate=”false” scope=”request” path=”/selectitem” name=”itemcolorform” >
<forward name=”success” path=”/itemlistpage.jsp” / >
</action >
</action-mappings >
</struts-config >

<action-mappings>:This element is used for defining mapping for various actions using <action> tags. An <action> tag is used to map a request URL to a particular action and pass associated information. Here are important attributes of <action> element :

path : The application context-relative path to the action e.g. /login
type : Fully qualified name of Java class that will be used to execute this action.
name : The logical name of form bean used to pass the form data to action class.
parameter : The name of parameter whose values is used to call a particular method in action class.
validate : Set to true if the validate() method of the action associated with this mapping should be called.
forward : The request URI to which the control should be passed for this action mapping.