Tiles Result Example in Struts 2.2.1
Posted on: January 20, 2011 at 12:00 AM
In this tutorial you will learn how to develop a tiles based application in struts

Tiles Result Example

In Struts Tiles is a view layer which allows separate page to be reusable in single page.

To use tiles in your application you need to design a base layout which arranges the different views into single page. The base layout describes where to arrange the page by using <tiles:insertAttribute name="...." /> tag. The base layout for given example is as follows


<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><tiles:insertAttribute name="title" ignore="true" /></title>
<table border="0" cellpadding="2" cellspacing="2" align="center">
<td height="30" colspan="2" BGCOLOR="pink">
<tiles:insertAttribute name="header" />
<td height="350" width="150" BGCOLOR="lightblue">
<tiles:insertAttribute name="menu" />
<td width="450" BGCOLOR="#CCFFCC">
<tiles:insertAttribute name="body" />
<td height="30" colspan="2" BGCOLOR="pink">
<tiles:insertAttribute name="footer" />

Now you need to design the pages which you have described in baseLaout.jsp such as header, footer, menu, body. And the pages which you have designed for your application.


<div align="center" style="font-weight:bold">
<font face="arier" size="6">Your Apps Header Goes Here</font>


<%@taglib uri="/struts-tags" prefix="s"%>

<h3 align="center" color="red">Menu</h3>
<ul><a href="<s:url action="hometilesAction"/>">Home</a><br></ul>
<ul><a href="<s:url action="contacttilesAction"/>">Contact Us</a><br></ul>
<ul><a href="<s:url action="aboutUstilesAction"/>">About Us</a><br></ul>
<ul><a href="<s:url action="tutorialtilesAction"/>">Struts2.2.1 Tutorials</a><br></ul>


<div align="center">
Your Apps Footer Goes Here


<BODY bgcolor="pink">
<p>body/content of the page</p>


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<center><h1>About Us Page</h1></center>


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<h1>Home Page of your Apps</h1>


<h3><center>This is Contact Us Page</center></h3>


<a href="http://www.roseindia.net/struts/struts/struts2.2.1/index.html">Struts2.2.1 torials</a>

Now you need the define the above basic pages(tiles) such as header, menu, footer, body And do mapping for all the pages of your application in a XML called tiles.xml


<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"


<definition name="baseLayout" template="/layouts/baseLayout.jsp">

<put-attribute name="title" value="Template"/>
<put-attribute name="header" value="/layouts/header.jsp"/>
<put-attribute name="menu" value="/layouts/menu.jsp"/>
<put-attribute name="body" value="/layouts/bodyPart.jsp"/>
<put-attribute name="footer" value="/layouts/footer.jsp"/>


<definition name="home" extends="baseLayout">
<put-attribute name="title" value="Home"/>
<put-attribute name="body" value="/pages/home.jsp"/> 

<definition name="aboutUs" extends="baseLayout">
<put-attribute name="title" value="AboutUs"/>
<put-attribute name="body" value="/pages/aboutUs.jsp"/> 

<definition name="tutorial" extends="baseLayout">
<put-attribute name="title" value="StrutsTutorial"/>
<put-attribute name="body" value="/pages/strutstutorial.jsp"/> 

<definition name="contact" extends="baseLayout">
<put-attribute name="title" value="contactUs"/>
<put-attribute name="body" value="/pages/contactUs.jsp"/> 


Then you need to do some mapping in struts.xml as follows


"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

<package name="default" extends="struts-default">

<result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult"/>

<action name="*tilesAction" method="{1}" class="net.roseindia.TilesAction">
<result name="home" type="tiles">home</result>
<result name="aboutUs" type="tiles">aboutUs</result>
<result name="strutstutorial" type="tiles">tutorial</result>
<result name="contactUs" type="tiles">contact</result>


To configure tiles integration you need  to do some mapping for StrutsTilesListener. So you need to mapping as follows


<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>Struts Tiles Example</display-name>






finally you need to write a Action which can handle all these


package net.roseindia;

import com.opensymphony.xwork2.ActionSupport;

public class TilesAction extends ActionSupport {

	private static final long serialVersionUID = -2613425890762568273L;

	public String home()
		return "home";		
	public String aboutUs()
		return "aboutUs";		

	public String tutorial()
		return "strutstutorial";		

	public String contact()
		return "contactUs";		

	public String execute() throws Exception {
		// TODO Auto-generated method stub
		return home();

When you run this application it will display message as shown below:


Download this example code

Related Tags for Tiles Result Example in Struts 2.2.1:




Advertisement null