Here is what I’ve found to be the easiest and fastest way of setting up Struts Tiles for a webapp project that doesn’t already use Struts. (If your webapp already uses Struts, just skip the steps for setting up Struts and add the relevant Tiles parts to your current configuration.) For more information about Struts Tiles, please read my entry about Java Layout Frameworks.
- Download the latest Struts binary release (please note that these instructions are for version 1.1).
- Extract the contents of the downloaded zip/tar.gz file into a directory within your webapp project, e.g.
project/lib. This directory will now contain a directory named something like jakarta-struts-1.1. For simplicity, I have renamed it to jakarta-struts.
- Add the following to your Ant
build.xml file (or equivalent):
<target name="struts">
<copy todir="dist/webapp/WEB-INF">
<fileset dir="lib/jakarta-struts">
<include name="lib/commons-*.jar" />
<include name="lib/struts.jar" />
</fileset>
<fileset dir="lib/jakarta-struts/lib">
<include name="struts-*.tld" />
</fileset>
</copy>
</target>
Change dist/webapp to the directory where the files to be depolyed (i.e. put into a WAR file) are located.
- Add the following to your
WEB-INF/web.xml file:
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>
org.apache.struts.action.ActionServlet
</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>
/WEB-INF/struts-config.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<taglib>
<taglib-uri>
/tags/struts-tiles
</taglib-uri>
<taglib-location>
/WEB-INF/struts-tiles.tld
</taglib-location>
</taglib>
- Create a file in
WEB-INF named struts-config.xml containing the following:
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<plug-in className="org.apache.struts.tiles.TilesPlugin">
<set-property
property="definitions-config"
value="/WEB-INF/tiles-defs.xml" />
<set-property
property="definitions-debug"
value="2" />
<set-property
property="definitions-parser-details"
value="2" />
<set-property
property="definitions-parser-validate"
value="true" />
</plug-in>
</struts-config>
- Create a file in
WEB-INF named tiles-defs.xml containing the following:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/tiles-config_1_1.dtd">
<tiles-definitions>
<definition name="default" path="/layout.jsp">
<put name="title" value="" type="string" />
<put name="body" value="" type="string" />
<!-- Insert more attributes here as
appropriate for your site -->
</definition>
</tiles-definitions>
Here’s a good reference of how to use the Struts Tiles JSP tags and XML elements (the JSP tags and the elements in the tiles-defs.xml file work basically the same way).
That’s pretty much it!
To get you started with using Tiles in your JSP files, here’s an example. Create a file named index.jsp, containing this:
<%@ page pageEncoding="UTF-8"
contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="/tags/struts-tiles" prefix="tiles" %>
<tiles:insert definition="default" flush="true">
<tiles:put name="title" type="string" value="Home" />
<tiles:put name="body" value="/index-content.jsp" />
</tiles:insert>
Then create a file named index-content.jsp containing:
<%@ page pageEncoding="UTF-8" %>
<p>These are the body-contents of index.jsp.</p>
Finally, create the file used for putting the layout together, layout.jsp, containing:
<%@ page pageEncoding="UTF-8" %>
<%@ taglib uri="/tags/struts-tiles" prefix="tiles" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US">
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8" />
<meta http-equiv="Content-Language"
content="en-US" />
<title>
Website
<tiles:insert attribute="title" />
</title>
</head>
<body>
<tiles:insert attribute="body" />
</body>
</html>
Now you should be ready to build your project (don’t forget to invoke the struts build-file task) and deploy it to your servlet container. Good luck! If you have any questions or suggestions of how to do this differently, feel free to add a comment.