Introduction📄
A tile designates an area of the entire page. A website is typically the integration of various elements or parts. Most of these elements are the same on each page, and only one is modified (body section). The complexity and code duplication will increase if we employ these common elements on every page. In the future, if the website's common area needs to be changed, it must be done on every page.
We employ the idea of a template to make this simpler. Please include a header, footer, menu, and content on our website. In a template, these are referred to as tiles. The content of a portion can be static or dynamic. The Tiles Framework uses the tiles.xml file to manage all tiles.
Advantages of Tiles Framework📄
The following are the benefits of a tile framework:
⭐Customization by centralized page: The layout of every page can only be changed on a single page (centralized page).
⭐Code reusability: One component, such as a header or footer, may appear on multiple pages. It, therefore, saves on coding.
⭐Easy to modify: We don't have to replace several pages if any tile changes.
⭐Easy to remove: If any part (tile) of the page is removed, we don't need to remove the code from all the pages. We can remove the tile from our layout manager page.
Prerequisites
A templating system called Apache Tiles was created to make creating user interfaces for web applications easier. The Apache Tiles website's tiles.jar files must first be downloaded. The following jar files must be added to the project's class directory.
- tiles-API-x.y.z.jar
- tiles-compat-x.y.z.jar
- tiles-core-x.y.z.jar
- tiles-JSP-x.y.z.jar
- tiles-servlet-x.y.z.jar
In addition to those mentioned above, we also need to copy the following jar files into your WEB-INF/lib directory from the struts2 download.
- commons-beanutils-x.y.zjar
- commons-digester-x.y.jar
- struts2-tiles-plugin-x.y.z.jar
Related Article Apache Server
Steps To Create Tiles Application📄
The Steps are as follows:
Add tiles library in your application.
Define Struts2TilesListener in the web.xml file.
Create the input page (index. JSP).
Create the Action class.
Extend the tiles-default package in your package and define all the result types
as tiles in the struts.xml file.
Create the tiles.xml file and define all the tile's definitions.
Create the LayoutManager page.
Create the View components.
Now we will see each step in detail:
⭐Add Tiles Library to your Application
If you're using the MyEclipse IDE, you may add the tiles library by selecting project -> Build Path -> Add Library -> Add Myeclipse Library -> Select the Struts 2 tiles library -> ok.
If you're using Netbeans or Eclipse IDE, the necessary tiles library must be included in your project.
⭐Define Struts2 TilesListener in the web.xml file
Add a listener class entry for Struts2TilesListener to the web.xml file.
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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>
<listener>
<listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
</listener>
</web-app>
⭐Create the Input Page(index.jsp)
<%@ taglib uri="/struts-tags" prefix="s" %>
<s:form action="login">
<s:textfield name="name" label="User Name"></s:textfield>
<s:password name="password" label="Enter Password"></s:password>
<s:submit value="login"></s:submit>
</s:form>
⭐Create the Action Class
This action class specifies the execute method and has one field name.
package com. javatpoint;
public class Login {
private String user_name,user_password;
//getters and setters
public String execute(){
if(password.equals("admin")){
return "success";
}
else{
return "error";
}
}
}
⭐Inherit the tiles-default Package and Define all the Result Types as Tiles in struts.xml
One package is defined in this XML file, along with one action and two results.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="abc" extends="tiles-default" >
<action name="login" class="com.javatpoint.Login">
<result name="success" type="tiles">login-success</result>
<result name="error" type="tiles">login-error</result>
</action>
</package>
</struts>
⭐Create the tiles.xml file and Define all the Tiles Definitions
The WEB-INF directory must contain the tiles.xml file.
<?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="login-success" template="/layoutmanager.jsp">
<put-attribute name="title" value="Welcome Page"/>
<put-attribute name="body" value="/login-success.jsp"/>
</definition>
<definition name="login-error" template="/layoutmanager.jsp">
<put-attribute name="title" value="Login Error"/>
<put-attribute name="body" value="/login-error.jsp"/>
</definition>
</tiles-definitions>
⭐Create the Layout Manager Page
This page is the layout manager. The string and page resources were included using the insertAttribute and getAsString tiles tags, respectively.
<%@ 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>
<title><tiles:getAsString name="title" /></title>
</head>
<body>
<%@ include file="header.jsp" %>
<tiles:insertAttribute name="body" />
<%@ include file="footer.jsp" %>
</body>
</html>
⭐Create View Components
There are numerous view components, including welcome.jsp, footer.jsp, and header.jsp.
⭐header.jsp⭐
<h2 style="background-color:red;text-align:center;">Header Tile</h2>
<hr/>
⭐footer.jsp⭐
<hr>
<h2 style="background-color:red;text-align:center;">Footer Tile</h2>
⭐login-success.jsp⭐
<%@ taglib uri="/struts-tags" prefix="s" %>
WELCOME!!, <s:property value="name"/>
</textarea></div>
<hr/>
<strong>login-error.jsp</strong>
<div class="codeblock"><textarea name="code" class="XML" >
SORRY!!!! INCORRECT USERNAME OR PASSWORD.
<jsp:include page="index.JSP"></JSP:include>
⭐Defining Multiple Files In Struts2 Application⭐
You must add the following entry to your web.xml file to define numerous tiles.
<context-param id="struts_tiles">
<param-name>org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG</param-name>
<param-value>/WEB-INF/tiles1.xml,/WEB-INF/tiles2.xml</param-value>
</context-param>
Frequently Asked Questions
What function do tiles provide in Struts?
A template system called Tiles makes it simple to create web application pages with a unified aesthetic. It can be utilized for componentization as well as page decoration. Actions can return Tiles pages thanks to the Tiles plugin.
Is Struts 2 thread safe?
Yes, it is thread-safe.
What internal processes does Struts framework have?
In particular, the Struts framework automatically creates a LoginForm object and populates it with the form data given in the request when an incoming request matches /login. Validate's default value is true by default. This instructs the framework to call the form bean's validate function.
What is the vulnerability in Apache Struts?
A flaw in Apache Struts has been found that could lead to remote code execution. An open-source framework called Apache Struts is used to create Java web applications. Remote code execution can be possible if this vulnerability is successfully exploited.
Describe the tiles framework.
A framework for template composition is Apache Tiles. Originally designed to make the creation of web application user interfaces simpler, Tiles is now not only limited to the JavaEE web environment. Authors can create page fragments with Tiles that can be put together into a full page at runtime.