<listener>
<listener-class>
org.apache.struts2.tiles.StrutsTilesListener
</listener-class>
</listener>
<context-param>
<param-name>tilesDefinitions</param-name>
<param-value>/WEB-INF/tiles.xml</param-value>
</context-param>
Code language: HTML, XML (xml)
The above code configure Tiles listener in web.xml. An input configuration file /WEB-INF/tiles.xml
is passed as argument. This file contains the Tiles definition for our web application. Create a file tiles.xml in WEB-INF folder and copy following code into it. <?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="" />
<put-attribute name="header" value="/Header.jsp" />
<put-attribute name="menu" value="/Menu.jsp" />
<put-attribute name="body" value="" />
<put-attribute name="footer" value="/Footer.jsp" />
</definition>
<definition name="/welcome.tiles" extends="baseLayout">
<put-attribute name="title" value="Welcome" />
<put-attribute name="body" value="/Welcome.jsp" />
</definition>
<definition name="/customer.tiles" extends="baseLayout">
<put-attribute name="title" value="Customer Form" />
<put-attribute name="body" value="/Customer.jsp" />
</definition>
<definition name="/customer.success.tiles" extends="baseLayout">
<put-attribute name="title" value="Customer Added" />
<put-attribute name="body" value="/SuccessCustomer.jsp" />
</definition>
</tiles-definitions>
Code language: HTML, XML (xml)
Here in tiles.xml we have define a template baseLayout. This layout contains attributes such as Header, Title, Body, Menu and Footer. The layout is then extended and new definitions for Welcome page and Customer page is defined. We have override the default layout and changed the content for Body and Title. <%@ 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>
Code language: HTML, XML (xml)
Header.jsp <%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<h2>Struts2 Example - ViralPatel.net</h2>
Code language: HTML, XML (xml)
Menu.jsp <%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<s:a href="customer-form">Customer</s:a>
Code language: HTML, XML (xml)
Footer.jsp <%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
Copyright © ViralPatel.net
Code language: HTML, XML (xml)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation"
value="false" />
<constant name="struts.devMode" value="false" />
<constant name="struts.custom.i18n.resources"
value="ApplicationResources" />
<package name="default" extends="struts-default" namespace="/">
<result-types>
<result-type name="tiles"
class="org.apache.struts2.views.tiles.TilesResult" />
</result-types>
<action name="login"
class="net.viralpatel.struts2.LoginAction">
<result name="success" type="tiles">/welcome.tiles</result>
<result name="error">Login.jsp</result>
</action>
<action name="customer"
class="net.viralpatel.struts2.CustomerAction">
<result name="success" type="tiles">/customer.success.tiles</result>
<result name="input" type="tiles">/customer.tiles</result>
</action>
<action name="customer-form">
<result name="success" type="tiles">/customer.tiles</result>
</action>
</package>
</struts>
Code language: HTML, XML (xml)
The struts.xml now defines a new Result type for Tiles. This result type is used in <result>
tag for different actions. Also note that we have define a new action customer-form. This is just an empty declaration to redirect user to Customer form page when she clicks Customer link from menu. Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…
Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…
Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…
1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…
GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…
1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…
View Comments
Congratulations on this tutorial, I would like to make two comments:
1) I downloaded the latest version of Struts2 and comes with the library "struts2-core-2.1.8.1.jar". With this library do not work the examples. You have to install the versions "struts2-core-2.0.14.jar" you include the link in part 1 of tutorial that really works well. I have not tested with the version "struts2-core-2.1.6.jar".
2) I have a problem with Part 4 of the tutorial. I do not understand that serving the "customer-form" as well when you click on the link "customer" an error "HTTP Status 404 - / StrutsHelloWorld / Customer-form" ocurs. I have reviewed the signs of the tutorial and I can not see my mistake.
Thanks in advance
@All: To fix error 404, please correct file Menu.jsp. Add href = "customer-form.action".
Customer
Hi Viral Patel ,
Thanks for the tutorial,
It is very helpful ,
Iam having below queries on Tiles
Is it possible to use struts tiles like Iframe/Div ?
ie. can i call different URLs in header and footer in above example?
Kindly reply,
Hi Viral,
Nice tutorials ..!
But in the Tiles tutorial, I'm facing the same problem, which was mentioned above by Pedro.
error:
----------------------------------------------
HTTP Status 404 - /mystuff/customer-form
type Status report
message /mystuff/customer-form
description The requested resource (/mystuff/customer-form) is not available.
----------------------------------------------
And, I'm are eagerly waiting for your "Struts 2 Ajax Tutorial with Example"
Kindly reply viral
Hey Viral
Thanks for the tutorials. Though I was stuck in running the hello world but I managed to get it through. Also is there any forums or any thing in which I can ask you about some concepts & meanings of the syntax?? You understand what I am saying??
Regards
Sadia
Hi Viral,
great tutorial! Thank you very much for your work! BTW, i had to modyfiy the Customer.jsp and Welcome.jsp Files to get them displayed correctly with Tiles - if BaseLayout.jsp contains the complete XHTML framework, the "content tiles" should only contain the content of the body tags, i suppose? But maybe this is due to my usage of a css/div-based layout? In addition, i had to change the "add customer" link in Welcome.jsp so it points to customer-form instead of Customer.jsp - otherwise i loose my Tiles-Layout.
Regards
florian
I am having problems. when I click on the customer or add customer button then it shows the customer.action without the tiles & not in the way its show in the picture. then when I press add customer then it shows in the body tile???what could be the problem?
I got the error
———————————————-
HTTP Status 404 – /mystuff/customer-form
type Status report
message /mystuff/customer-form
description The requested resource (/mystuff/customer-form) is not available.
———————————————-
Can anyone share how to fix it?
NK
Hi,
This is a nice tutorial, I appreciate your help.
It's working fine for me, just don't know why it's not taking the whole page, I only see it as a small square in the top center of the page.
thanks
For those that are still trying to fix the 404 on customer-form
in Welcome.jsp and Menu.jsp the href should now point to "customer-form.action" not "customer-form"
Hope this helps
I have downloaded the example and added the Jar, but when i run it on server, it is giving error "The requested resource (/StrutsHelloWorld/) is not available.", I dont know where i am missing, can u please guide me as i understand everything but i want to chek it out in practical . Thanks in advance,