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 »

Comments (0)

No comments have yet been posted.

Post a comment

Comments currently suppressed due to excessive spam. I'll reinstate them later, this time with approvals turned on. :-)

Spring Annotations RefCard
Check out the new DZone Spring Annotations Refcard by Craig Walls!

What's New?

2009-08-30 - Check out my two-part series on DZone: Spring Integration: A Hands-On Tutorial.
2009-03-25 - My new article Getting Started with Spring Batch 2.0 is available on DZone.
Home | Consulting | Tech Articles | Mailing List | Contact | Spring Blog
Copyright © 2008 Wheeler Software, LLC.