Sunday, September 28, 2008

Tiles with Struts 1.3 or Upgrading to Struts 1.3 Tutorial Step 13

To use Tiles with Struts or to upgrade to a newer version of Tiles with Struts, follow these instructions. These instructions are for Struts version 1.3.

The configuration for tiles is a little different from prior versions. The old versions of Struts required a tld folder to hold the tld files. You do not need that folder with Struts 1.3. With Struts 1.3, the tld files are already contained within the jar file.

There is a bug in older versions of the common-chain.jar file. You should visit this website and download the latest version of commons-chain and copy it into your project lib directory.

http://commons.apache.org/downloads/download_chain.cgi


You need to update a few other files as well.

Using struts 1.3 you need to add this information to your web.xml file. You should add it in the servlet section under the servlet-class section. If you were using an older version of struts, then remove the tld tags that are referencing the old version of the struts tags.  Add the below code within your servlet section of your web.xml file.

Add to web.xml


<init-param>
<param-name>chainConfig</param-name>
<param-value>org/apache/struts/tiles/chain-config.xml</param-value>
</init-param>



You need to replace your DOCTYPE with this code. This is the new version of your struts.


Replace in struts.xml


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


Also, in your struts.xml file you need to change your forward that is currently going to your user.jsp file to this.
<forward name="success" path="tile.userpage" />

You are changing it to reference the tile path in your tiles configuration file.
Add this code to to your struts.xml file.  This is your TilesPlugin declaration.

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

What you need to do is create a folder called layout under your jsp folder. In this folder, create three jsp pages called: classiclayout.jsp, footer.jsp, and header.jsp. Also, create a file called include-taglibs.jspf under the jsp folder as well. This file will be used to call the tag libraries. You can also include other files you will want to import into your application.

Copy the following code into the proper files:

include-taglibs.jspf



<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>




classiclayout.jsp




<%@ include file="../include-taglibs.jspf" %>

<html>
<body>
test classic layout
<tiles:insert attribute="header" />
<tiles:insert attribute="body" />
<tiles:insert attribute="footer" />
</body>
</html>




In the header and footer files just put some text called test footer (for the footer file) and test header (for the header file).

These pages will test your configuration. The classiclayout.jsp file is the main file. It has your open and closing html and body tags. It uses tiles:insert tags to insert the proper jsp page.

In the same folder as your struts.xml file, you should create a file called tiles-default-defs.xml.
This file should already be declared in the struts.xml file. You can see my prior posts for an example of the struts.xml file.

Paste the following code into that file.


tiles-default-defs.xml



<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 1.3//EN"
"http://struts.apache.org/dtds/tiles-config_1_3.dtd">

<!-- Definitions for Tiles -->
<!--
This file contains definitions common to all struts module.
In particular, it contains the main layout and the common menus.
There is also the main page as a portal.
-->
<tiles-definitions>
<!-- Doc index page description -->
<definition name="mainLayout" path="/WEB-INF/jsp/
layout/classiclayout.jsp">
<put name="title" value="Struts Test version 1" />
<put name="header" value="/WEB-INF/jsp/layout/header.jsp" />
<put name="body" value="" />
<put name="footer" value="/WEB-INF/jsp/layout/footer.jsp" />
</definition>
<definition name="tile.userpage" extends="mainLayout">
<put name="body" value="/WEB-INF/jsp/user.jsp"/>
</definition>
</tiles-definitions>



The purpose of the Tiles is to be able to maintain your layout easier. You might want the header and the footer to be the same throughout all the pages, but the body is what will change.

The definition first references the classiclayout.jsp page. It then displays the path to the header, body, and footer. The body can be left blank because we will be inserting that below.
In your struts.xml file you reference the tile.userpage which called the tile.userpage definition. That definition is using the mainLayout definition above. The body becomes the user.jsp page.

Build your war file again and visit your Project URL.

http://localhost:8080/UserBook/

You should see something like this:
test classic layout test header This is a test test footer

If you see something like this, then that means that your forward from your action class called the proper tile.user forward which used the mainlayout and inserted user.jsp page into your classiclayout page. All of your pages are included in your tiles configuration and now you are ready to start constructing your project.

Go To Step 14

2 comments:

Zander April 3, 2010 11:13 PM  

im having trouble with in this section. Error below:

HTTP Status 404 - Servlet action is not available

type Status report

message Servlet action is not available

description The requested resource (Servlet action is not available) is not available.
Apache Tomcat/5.5.27

Greg April 5, 2010 8:47 AM  

Post your stack trace from your log file. It looks like the action class you are calling isn't there. What is the URL you are going to?