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> |
You need to replace your DOCTYPE with this code. This is the new version of your struts.
Replace in struts.xml
|
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.
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
|
classiclayout.jsp
|
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
|
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



