Struts Tiles Integration

Hi All,

This post will be focused on integrating Tiles framework with Struts 1.x.

Following are the code snippets.

1) Add a plug in for Tiles in struts-config.xml.
Here is the struts-config.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">

<struts-config>

<!-- Data Sources -->
<data-sources>
</data-sources>

<!-- Form Beans -->
<form-beans>
</form-beans>

<!-- Global Exceptions -->
<global-exceptions>
</global-exceptions>

<!-- Global Forwards -->
<global-forwards>
</global-forwards>

<!-- Action Mappings -->
<action-mappings>
<action path="/HomePage" forward="homepage.page" />
</action-mappings>

<!-- Message Resources -->

<!-- Plug Ins -->

<plug-in className="org.apache.struts.tiles.TilesPlugin">
<set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" />
<set-property property="moduleAware" value="true" />
</plug-in>

</struts-config>

2) Write a Layout.jsp page. This page will serve as a template. We create attributes in this jsp page that will be substituted by actual jsp pages defined in tiles-defs.xml.

Here is Layout.jsp page

<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>

<html>
<body>
<table border="1" cellpadding="2" cellspacing="2" align="center">

<tr>
<td height="20%" colspan="2">
<tiles:insert attribute="header" />
</td>
</tr>

<tr>
<td height="60%" colspan="2">
<tiles:insert attribute="body" />
</td>
</tr>

<tr>
<td height="20%" colspan="2">
<tiles:insert attribute="footer" />
</td>
</tr>

</table>
</body>
</html>

3) Write a tiles-defs.xml file. We write definitions of all the web pages that should obey the template created in step 2. We declare the name (attribute name in jsp page) value (actual jsp page to be substituted) pair. This xml file (as all xml files) should be in /WEB-INF folder of the application.

Here is the tiles-defs.xml file:

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

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

<definition name="homepage.page" page="/Layout.jsp">
<put name="header" value="/Header.jsp" />
<put name="body" value="/Body.jsp" />
<put name="footer" value="/Footer.jsp" />
</definition>

</tiles-definitions>

4) web.xml file.
Here is the code:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app id="WebApp">

<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>

<load-on-startup>2</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

<taglib>
<taglib-uri>/tags/struts-tiles</taglib-uri>
<taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
</taglib>

</web-app>

Now we have made the initial set up for using tiles.
We are just left with writing the jsp files that will be displayed using Tiles.

5) Write index.jsp page.
Here is the code:

<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<html:html>
<body>
<html:link action="HomePage">Using Tiles in Struts</html:link>
</body>
</html:html>

6) Write Header.jsp page.
Here is the code:

You are in header page!!!!

7) Write Body.jsp page.
Here is the code:

You are in body page!!!!

8) Write Footer.jsp page.
Here is the code:

You are in footer page!!!

Thats it!!!

Hope your code works.

Thanks for taking the time to reading my post!

If you like my code, feel free to leave comments 🙂

Best Regards

Advertisements

About Badal Chowdhary

I am a Software Engineer by profession. I have done SCJP and SCWCD certifications. Like working on cutting edge technologies and frameworks. Driven by challenges and fascinated by technology. I love playing and watching sports: Cricket, Ping Pong, Tennis, Badminton, Racket Ball and Gym.
This entry was posted in Struts and tagged , . Bookmark the permalink.

One Response to Struts Tiles Integration

  1. chandrasekhar says:

    Hei Badal,
    Your code was very much helpful to me and has solved some of my doubts too.I could find the amount of hard work u have put on this. Continue your hard work and help many more people like me the same way.
    Best of Luck

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s