Struts 2.x : [A Framework from ASF] :: Using Tiles Framework
Struts 2.x with Tiles |
Struts 2.x with Tiles Framework Example
Example for Struts 2.x with Tiles |
Project Structure with required Lib
Project Structure with All JSP pages
MenuAction.java
package com.javaskool.struts;
import com.opensymphony.xwork2.ActionSupport;
public class MenuAction extends ActionSupport {
public String execute()
{
return SUCCESS;
}
}
tiles.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!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="/baseLayout.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="baseLayout1" template="/baseLayout1.jsp" >
<put-attribute name="title" value="Template"/>
<put-attribute name="header" value="/header.jsp"/>
<put-attribute name="body" value="/body.jsp"/>
<put-attribute name="footer" value="/footer.jsp"/>
</definition>
<definition name="login.def" extends="baseLayout1">
<put-attribute name="title" value="Login form"/>
<put-attribute name="body" value="/login.jsp"/>
</definition>
<definition name="register.def" extends="baseLayout">
<put-attribute name="title" value="Register"/>
<put-attribute name="body" value="/register.jsp"/>
</definition>
<definition name="contact.def" extends="baseLayout">
<put-attribute name="title" value="Contact"/>
<put-attribute name="body" value="/contact.jsp"/>
</definition>
<definition name="h1.def" extends="baseLayout">
<put-attribute name="title" value="Hello"/>
<put-attribute name="body" value="/h11.jsp"/>
</definition>
<definition name="success.def" extends="baseLayout">
<put-attribute name="title" value="success"/>
<put-attribute name="body" value="/success1.jsp"/>
</definition>
</tiles-definitions>
struts.xml
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="myPack1" extends="struts-default">
<result-types>
<result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" />
</result-types>
<action name="loginaction" method="execute" class="com.javaskool.struts.MenuAction">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
index.jsp
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<tiles:insertTemplate template="/baseLayout.jsp">
<tiles:putAttribute name="title" value="This is Tiles Framework" type="string"/>
<tiles:putAttribute name="header" value="/header.jsp" />
<tiles:putAttribute name="menu" value="/menu.jsp"/>
<tiles:putAttribute name="body" value="/body.jsp" />
<tiles:putAttribute name="footer" value="/footer.jsp" />
</tiles:insertTemplate>
baseLayout.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 width="900" cellspacing="0" align="center">
<tr>
<td colspan="2" bgcolor="#1D1D1B">
<tiles:insertAttribute name="header" />
</td>
</tr>
<tr height="300">
<td width="150" valign="top" bgcolor="#988131">
<tiles:insertAttribute name="menu" />
</td>
<td width="750" valign="middle" align="center">
<tiles:insertAttribute name="body" />
</td>
</tr>
<tr>
<td colspan="2" width="900" height="70" bgcolor="#9B1B56">
<tiles:insertAttribute name="footer" /><br/>
</td>
</tr>
</table>
</body>
</html>
baseLayout1.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 width="900" cellspacing="0" align="center">
<tr>
<td colspan="2" bgcolor="#1D1D1B">
<tiles:insertAttribute name="header" />
</td>
</tr>
<tr height="300">
<td width="900" valign="middle" align="center">
<tiles:insertAttribute name="body" />
</td>
</tr>
<tr>
<td colspan="2" width="900" height="70" bgcolor="#9B1B56">
<tiles:insertAttribute name="footer" /><br/>
</td>
</tr>
</table>
</body>
</html>
header.jsp
<h2 style="color:gold;">"Brand Logo"</h2>
<h2 align="right">
<a href="index.jsp">HOME</a> |<a href="contacting.jsp">CONTACT</a> |<a
href="loging.jsp">LOGIN</a>
</h2>
footer.jsp
<h2 align="center"> © Copyrigth Reserved to Javaskool Ltd.</h2>
menu.jsp
<a href="loging.jsp">Login</a><br>
<a href="registering.jsp">Register</a><br>
<a href="contacting.jsp">Contact</a><br>
<a href="h1.jsp">Hello</a><br>
body.jsp
This is your Body section
contact.jsp
Contact US....
Code here.......
contacting.jsp
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<tiles:insertDefinition name="contact.def"/>
login.jsp
<!DOCTYPE html>
Login Here
<br>
<form action="loginaction" method="post">
User:<br/><input type="text" name="user"/><br/>
Password:<br/><input type="password" name="password"/><br/>
<input type="submit" value="Login"/>
</form>
loging.jsp
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<tiles:insertDefinition name="login.def"/>
success.jsp
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<tiles:insertDefinition name="success.def"/>
success1.jsp
successs
register.jsp
Register Here
//Code for Registration form
registering.jsp
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<tiles:insertDefinition name="register.def"/>
h1.jsp
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<tiles:insertDefinition name="h1.def"/>
h11.jsp
Hello, I am james Bond!!
jar files required in lib folder
Note: List of jar files differ as per project
commons-fileupload-1.2.2
commons-io-2.0.1
commons-lang-2.4
commons-lang3-3.1
commons-logging-1.1.1
commons-logging-api-1.1
freemarker-2.3.19
javassist-3.11.0.GA
ognl-3.0.5
struts2-core-2.3.4
xwork-core-2.3.4
Click Here to download all jar files required for basic Struts 2.x Application
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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>Tiles Framework</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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Output
Click here to download complete code with jar files [4.1 MB]
Recent Comments