Elsie esq.
Photo credit: Elsie esq.
del.icio.us Digg DZone Reddit StumbleUpon
1 | 2 | 3 | Next »
Software Development

Adding Modules to a Struts/Tiles Application

Use Struts modules to tidy up your Struts/Tiles application.

[I wrote this article some years back for Struts 1.2.7. It may work for Struts 1.3.x as well though I haven't actually tried that. — Willie]

In this article I'm going to show you how to add modules to an existing Struts/Tiles project. First we'll start by separating out the Struts part, without separating out the Tiles part. Once we get that working, we'll move on to separating the Tiles definitions as well.

Modify web.xml

In web.xml, the ActionServlet definition needs to contain one init param for each module in your app. For the sake of example, let's consider my web site. [I wrote this article when I had a personal web site that used Struts and Tiles — Willie] I have a default module, an articles module, and a software module. (My web site is probably too small to warrant subdivision into modules, but being small makes for a good example.) Here's my ActionServlet definition:

<servlet>
    <servlet-name>ActionServlet</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>
    <init-param>
        <param-name>config/articles</param-name>
        <param-value>/WEB-INF/struts-config-articles.xml</param-value>
    </init-param>
    <init-param>
        <param-name>config/software</param-name>
        <param-value>/WEB-INF/struts-config-software.xml</param-value>
    </init-param>
</servlet>

The default module handles requests that aren't handled by the other two modules. Notice that the param names have slashes. The filenames themselves can be whatever you like; I've followed standard convention in using the format struts-config-xxx.xml.

(Don't forget the servlet mapping.)

You'll also need to make sure that you define the Tiles taglib in web.xml. Moreover, in most cases you'll want to have the Struts taglibs as well, since they are module-aware. Here are the definitions that I have:

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

That does it for web.xml. Now let's do the module config files.

Social bookmarks: del.icio.us Digg DZone Reddit StumbleUpon
1 | 2 | 3 | Next »
Show comments (0)

Post a comment

Your name:
Your e-mail address (won't be displayed):
Your web site (optional):
example: www.xyz.com
Your comment:
Please help us prevent comment spam:
Spring in Practice
My brother and I are writing Spring in Practice for Manning!

What's New?

2008-10-20 - I've added a new mailing list feature to the site. Sign up to receive e-mail updates about new articles.
2008-09-30 - We've released chapter 4 (User registration) and chapter 5 (Authentication) of Spring in Practice.
2008-09-11 - By popular demand, I've added an RSS feed to the site.
Home | Consulting | Tech Articles | Mailing List | About | Contact | Spring Blog
Copyright © 2008 Wheeler Software, LLC.