Struts with Tiles Framework
- Overview of Struts Tiles Framework
- Templates
- Configurie tiles framework in struts-config.xml files
- Struts tiles tag library
- Tiles Definition
- Example for Tiles Framework
Overview of Struts Tiles Framework |
In most of the Web application, we have used three different approaches to assembling Web pages.
The first approach
- sometimes referred to as a straight JSP-based approach.
- The JSP pages contain presentation logic along with HTML layout tags; there is no separation of the two.
- This approach is typically used for smaller, less complicated Web applications.
The second approach
- uses the JSP include directive.
- It’s used by developers for larger Web applications.
- Using the JSP include directive allows for more reuse, which reduces the total development and maintenance costs.
The third approach,
- which is introduced in this session is Tiles (template-based approach),
- describes a way to reduce the amount of redundant code a Web application contains and, at the same time,
- allows you to separate the content from the layout better than the first two approaches.
Overview of Tiles
- Tiles allows you to exploit the concept of JSP includes by providing a framework for defining and dynamically populating page layouts.
- Each page layout is simply a JSP that defines a template frame (or outline) with placeholders for where content should go.
- At run time, Tiles replaces the placeholders with their associated content, creating a complete page ad unique instance of the layout.
- To accomplish this Tiles uses its concepts of definitions and attributes.
- A Tiles definition creates a piece of content that Tiles can insert into a JSP using that definition’s name.
- Each definition consists of a name (or identifier), a layout JSP, and a set of attributes associated with the definition.
- To facilitate the use of definitions and attributes, Tiles uses an XML configuration file (tiles-defs.xml) for storing their definitions.
- Tiles also provides a JSP tag library for defining definitions and attributes.
Tiles framework provides the following features.
- Template capabilities
- Dynamic page construction and loading
- Screen definitions
- Support for tile and layout reuse
- Support for internationalization
- Support for multiple channels
Templates |
- A template is a JSP page that uses a JSP custom tag library to describe the layout of a page.
- The template acts as a definition for what the pages of an application will look like, without specifying the content.
- The content is inserted into the template page at runtime.
- One or more pages may use the same template.
- The template file includes a body-content section.
- This allows us to reuse the template for any page that has this generic format.
- This one file can then control the layout of multiple pages.
- If we need to modify the layout of the site, this is the only file you need to change-that is the real power of using a template-based approach.
The following example illustrates how the template will look like:
<% taglib uri="/WEB-INF/tlds/struts-tiles.tld" prefix="tiles" %>
<html:html>
<head><title> < tiles:getAsString name="title" />
</title>
</head>
<body>
<tiles:insert attribute="header"/>
<tiles:insert attribute="menubar"/>
<tiles:insert attribute="bodycontent"/>
<tiles:insert attribute="footer"/>
</body>
</html:html>
Configuring Struts-config.xml and tiles files |
You must add the Tiles library to the Web application deployment descriptor before wecan use it. Add the following taglib element to the Web.xml file:
<taglib>
<taglib-uri>/WEB-INF/tiles.tld</taglib-uri>
<taglib-location>/WEB-INF/tiles.tld</taglib-location>
</taglib>
Each page that needs to use the Tiles tag library must include the following line at the top:
<%@ taglib uri="/WEB-INF/tiles.tld" prefix="tiles" %>
Configuring tiles with struts
To add the Tiles plug-in to a Struts application, you need to add the following plug-in element to the Struts configuration file
<plug-in ClassName="org.apache.struts.tiles.TilesPlugin">
<set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml"/>
</plug-in>
The definitions-config initialization parameter specifies the XML file or files containing Tile definitions. If multiple filenames are used, they must be comma-separated.
Tiles tag Library |
This section introduces the JSP custom tags used by the Tiles framework. The tags are very similar to the ones provided in any template-based framework, but Tiles contains more functionality.
Tag name | Description |
---|---|
add | Add an element to the surrounding list. |
definition | Create a tile component definition. |
get | Get the content from request scope that was put there by a put tag. |
getAsString | Render the value of the specified tile/component/template attribute to the current JspWriter. |
importAttribute | Import a tile’s attribute into the specified context. |
initComponentDefinitions | Initialize a tile definitions factory. |
insert | Insert a tiles component. |
put | Put an attribute into a tile context. |
putList | Declare a list that will be passed as an attribute. |
useAttribute | Use an attribute value inside a page. |
The insert Tag
- The insert tag is responsible for inserting content into a page.
- In a layout tile, the insert tag prescribes where the content will go using attribute values
Attributes of the insert tag
Attribute name | Description |
---|---|
attribute | The name of an attribute in the current tile/component context. |
beanName | The name of the bean used as a value. |
beanProperty | The name of the bean property. If specified, the value is retrieved from this property. |
beanScope | The context scope the bean can be found within. |
component | A string representing the URI of a tile or template. |
controllerUrl | The URL of a controller called immediately before the page is inserted. |
controllerClass | The class type of a controller called immediately before the page is inserted. |
definition | The name of the definition to insert. Definitions are defined in a centralized file |
flush | true or false. If true, the current page output stream is flushed before tile insertion. |
ignore | If this attribute is set to true and the attribute specified by the name does not exist, simply return without writing anything. runtime exception to be thrown. |
name | The name of an entity to insert. |
page | A string representing the URI of a tile or template. |
role | If the user is in the specified role, the tag is taken into account; otherwise, the tag is skipped and the content is not written out. |
template | A string representing the URI of a tile or template. |
The definition Tag
- The definition tag is used to create a tile (template) definition as a bean.
- The newly created bean will be saved under the specified id, in the requested scope.
- The definition tag has the same syntax as the insert tag.
- The new definition can extend a definition described in the definition factory (XML file) and may overload any previously defined parameters.
Attributes of the definition tag
Attribute name | Description |
---|---|
extends | The name of a parent definition that is used to initialize this new definition. |
id | The name under which the newly created definition bean will be saved. This attribute is required. |
page | URL of the template/component to insert. Same as template. |
role | The role to check before inserting this definition. |
scope | The variable scope in which the newly defined bean will be created. |
template | A string representing the URL of a tile/component/template (a JSP page). |
The put tag
- The put tag is used to pass attributes to a tile component.
- This tag can be used only inside the insert or definition tags.
- The value (or content) of the put tag is specified using the value attribute or the tag body.
- It is also possible to specify the type of the value.
Attribute name | Description |
---|---|
beanName | The name of the bean used to retrieve the value. |
beanProperty | The property name in the bean |
beanScope | The scope used to search for the bean. |
content | Content that’s put into tile scope. |
direct | How the content is handled: true means content is printed directly; false means content is included. |
name | The name of the attribute. |
role | If the user is in the specified role, the tag is taken into account. Otherwise, the tag is skipped and the content is not written out. |
type | The content type. Valid values are "string", "page", "template", or "definition". |
value | The attribute valuecan be a String or an Object. |
Tiles Definition |
- Tiles definitions provide just such functionality.
- A definition allows you to statically specify the attributes that are used by a template, which in turn allows you to specify only the page-specific attributes in your tiles.
- There are two locations in which you can specify definitions: a JSP page or an XML file.
Declaring definition in JSP Page
<tiles:definition id="default" page="DefaultLayout.jsp" scope="request">
<tiles:put name="header" value="/header.jsp" />
<tiles:put name="menubar" value="/menubar.jsp" />
<tiles:put name="footer" value="/footer.jsp" />
</tiles:definition>
The common files that were spread through the various tiles are now located in the definition file. This makes changing their values much easier. For instance, if we wanted to specify a different footer page, the only place to change it would be in the definition file.
Declaring definition in a configuration file
To use the XML approach, create an XML file that follows the syntax of the tiles-config.dtd file. The definition XML file should be placed in the WEB-INF directory.
<definition name="layout" path="/mainLayout.jsp">
<put name="title" value="" />
<put name="header" value="/header.jsp" />
<put name="menu" value="/menubar.jsp" />
<put name="body" value="" />
<put name="footer" value="/footer.jsp" />
</definition>
<definition name= "search.page" extends= "layout">
<put name="title" value="Search Page" />
<put name="body" value="/search.jsp" />
</definition>
Extending tiles definition
- One of the most powerful features of tile definitions is the ability to create new definitions by extending existing ones. All attributes and properties of the parent definition are inherited, and you can override any attribute or property.
- In the preceding example you are extending mainLayout.jsp to search.jsp.so the attribute and properties of the mainLayout.jsp are inherited to search.jsp page.
Using Definitions as Forwards in Struts
- Tiles definitions can also be used as Struts forwards, instead of actual URLs. To use definitions in this manner, you first create the definitions.
- In the Struts configuration file, you need to define the forwards that use these definitions:
<global-forwards>
<forward name="main" path=" layout" />
</global-forwards>
In the preceding example instead of giving the full path of JSP page(/mainLayout.jsp) , we can specify only the definition of that page lyout.
Example for Tiles Framework |
Display the hello.jsp page using tiles telmplate .Use tiles-def.xml, struts-config.xml for this purpose.
header.jsp
<%@ taglib prefix="html" uri="/WEB-INF/struts-html" %>
<table>
<tr>
<td>Javaskool WELCOME TO ALL</td>
</tr>
</table>
footer.jsp
<%@ taglib prefix="html" uri="/WEB-INF/struts-html" %>
<table>
<tr>
<td>©Copyright 2010-12, JavaSkool.com, All Rights Reserved</td>
</tr>
</table>
hello.jsp
<%@ taglib prefix="html" uri="/WEB-INF/struts-html" %>
<html:html>
Welcome to Tiles Framework
</html:html>
Template.jsp
Template page is a JSP page, in which you can insert header.jsp, footer.jsp pages. You can use this template as a layout.
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib prefix="tiles" uri="http://struts.apache.org/tags-tiles" %>
<html:html>
<head>
<title><tiles:getAsString name="title" ignore="true"/></title>
</head>
<body>
<table >
<tr><td><!-- Enter the Header here -->
<tiles:insert attribute="header"/></td></tr>
</table>
<table>
<tr><td><!--Body Section here -->
<tiles:insert attribute="body"/>
</td></tr>
</table>
<!--Footer Section here -->
<tiles:insert attribute="footer"/>
</body>
</html:html>
tiles-def.xml
It is a configuration file to define template page extending template page to other pages.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC "-//Apache Software Foundation//DTD Tiles Configuration
1.1//EN" "http://jakarta.apache.org/struts/dtds/tiles-config_1_3.dtd">
<tiles-definitions>
<!--Master Template -->
<definition name="MasterTemplate" page="/template.jsp">
<put name="title" type="String" value="Example" />
<put name="header" value="/header.jsp" />
<put name="body" value="/blank.jsp" />
<put name="footer" value="/footer.jsp" />
</definition>
<!hello page start-->
<definition name="hello" extends="MasterTemplate">
<put name="title" type="string" value="Hello Page" />
<put name="body" value="/hello.jsp" />
</definition>
struts-config.xml
You need to add tiles plugin in struts-config.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">
<struts-config>
<plug-in className="org.apache.struts.tiles.TilesPlugin">
<set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" />
<set-property property="moduleAware" value="true" />
</plug-in>
</struts-config>
Flow
- template.jsp file contains the template for the application like header, body and footer.The actual data will populated at runtime using tiles tags.
- tiles-def.xml file contains the definition for the template(template.jsp) and definition of the hello.jsp page which extended template.jsp.
- All the jsp files will extend and override the required (body) content.
Recent Comments