12:03 pm - Wednesday January 18, 2017

Struts 2 Tiles Layout Example and Tutorial

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

BACK

Struts 2 Tiles Layout Example and Tutorial

 

Screenshot-4Screenshot-5

LinkAction.java


package com.candid.action;

import com.opensymphony.xwork2.ActionSupport;

public class LinkAction extends ActionSupport {

private static final long serialVersionUID = -2613425890762568273L;

public String home()
{

return "home";
}

public String training()
{
return "training";
}

public String contact()
{
return "contact";
}
public String service()
{
return "service";
}
}

struts.xml


<struts>
<package name="default" extends="struts-default">
<result-types>
<result-type name="tiles" />
</result-types>
<action name="*Link" method="{1}">
<result name="home" type="tiles">home</result>
<result name="training" type="tiles">training</result>
<result name="contact" type="tiles">contact</result>
<result name="service" type="tiles">service</result>
</action>

</package>
</struts>

Tiles.xml


<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
"http://tiles.apache.org/dtds/tiles-config_2_0.dtd">

<tiles-definitions>

<definition name="baseLayout" template="/layout.jsp">
<put-attribute name="title"  value="Template"/>
<put-attribute name="header" value="/header.jsp"/>
<put-attribute name="menu"   value="/menu.jsp"/>
<put-attribute name="body"   value="/body.jsp"/>
<put-attribute name="footer"   value="/footer.jsp"/>
</definition>

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

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

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

<definition name="service" extends="baseLayout">
<put-attribute name="title"  value="service"/>
<put-attribute name="body"   value="/service.jsp"/>
</definition>
</tiles-definitions>

web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Struts2Example15</display-name>

<context-param>
<param-name>org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG</param-name>
<param-value>/WEB-INF/tiles.xml</param-value>
</context-param>

<listener>
<listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
</listener>

<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

home.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Welcome Guest.
</body>
</html>

layout.jsp


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

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<%-- <title><tiles:insertAttribute name="title" ignore="true" /></title> --%>
</head>
<body>
<table border="1" cellpadding="2" cellspacing="2" align="center">
<tr>
<td height="30" colspan="2">
<tiles:insertAttribute name="header" />
</td>
</tr>
<tr>
<td height="250">
<tiles:insertAttribute name="menu" />
</td>
<td width="350">
<tiles:insertAttribute name="body" />
</td>
</tr>
<tr>
<td height="30" colspan="2">
<tiles:insertAttribute name="footer" />
</td>
</tr>
</table>
</body>
</html>

header.jsp


<div align="center" style="font-weight:bold">Candid industrial training</div>

footer.jsp


<div align="center">&copy; candid.com</div>

menu.jsp


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

<a href="<s:url action="trainingLink"/>" >training</a><br>
<a href="<s:url action="contactLink"/>" >contact</a><br>
<a href="<s:url action="serviceLink"/>" >service</a><br>

Output ScreenshotsScreenshot-1

 

Screenshot-2

Filed in: Struts 2 Tutorial

Comments are closed.