logo
header art

Building an Enterprise-Ready Web Application with Struts

June 23, 2005

Getting Struts into Your Webapp

The first step in the process is setting up your project. If you've never used Maven before, please see my earlier Maven article. It uses the project descriptor for this project as an example to walk you through the process of creating and managing a project in Maven.

After setting up your project, it's time to start actually building your webapp, and the first step in doing that is setting it up to use the Struts framework. The easiest way to get the various Struts files into your webapp is to copy them from the struts-blank webapp that ships with the Struts framework. Inside the directory where you unzipped Struts, you should find webapps/struts-blank.war; deploy this WAR file into Tomcat. Now check out the webapps/struts-blank/WEB-INF/lib/ directory under your Tomcat install and copy its contents into your main/src/main/webapp/WEB-INF/lib directory. Now go to the webapps/struts-blank/WEB-INF/ directory under Tomcat and copy all the TLD files you find there, except struts-tiles.tld, to your main/src/main/webapp/WEB-INF/tld/ directory. Finally, copy the struts-config.xml from the deployed WEB-INF/ directory into your main/src/main/webapp/WEB-INF/ directory. The sample webapp doesn't use Tiles or declarative validation, so you can safely ignore the tile-defs.xml, validation.xml, and validator-rules.xml files.

Now it's time to set up your web.xml file to refer to the framework files we just included, so let's break it down one element at a time.

  • <webapp> - this is the root element of web.xml.
    • <display-name> - this element contains simple text to display as the name of the application to things like the Tomcat Manager application.
    • <servlet> - this element defines a specific servlet within the application; while a webapp can define as many servlets as it needs, we only have one explicit servlet in our sample webapp. The name of this servlet is "action", and it's a reference to the base ActionServlet (org.apache.struts.action.ActionServlet) provided by the Struts framework. This particular servlet is initialized with 3 parameters, each represented by an <init-param> element.
      • <init-param>
        • <param-name>config</param-name> - this parameter is named config
        • <param-value>/WEB-INF/struts-config.xml</param-value> - this is the path to the struts-config.xml file, relative to the root directory of the webapp
      • <init-param>
        • <param-name>debug</param-name> - this parameter is named debug
        • <param-value>2</param-value> - provides info about debugging
      • <init-param>
        • <param-name>detail</param-name> - this parameter is named detail
        • <param-value>2</param-value> - specifies the level of detail to provide in the info
      • <load-on-startup> - this element contains a number indicating this servlet's position in the load-up queue; this servlet is second in our webapp.
    • <servlet-mapping> - this element delegates a servlet to handle all URLs that match a specific pattern
      • <servlet-name> - the name of the servlet (as defined in this file) to handle the URLs that match the pattern below; since this is a Struts application, we want the ActionServlet to handle incoming requests, so the value of this element is action, the name of the ActionServlet we defined above.
      • <url-pattern> - the pattern a url must match to be mapped to the servlet named above; Struts convention dictates that Actions be named with a .do suffix, so this pattern is *.do.
    • <welcome-file-list> - contains a list of files, in order of appearance, the webapp should try to display when a request comes in on the webapps virtual directory without specifying an individual file. NOTE: these entries must be actual physical files - logical names will not work.
      • <welcome-file> - a file the webapp should try to display when a request comes in to the webapp's virtual directory without specifying an individual file; convention dictates the first entry be index.jsp.
    • <taglib> - each of these elements defines a tag library so the webapp can find it and use tags defined in it, e.g., struts-html
      • <taglib-uri> - defines the URI that JSP files should use to reference the custom tags defined in this tag library, e.g., tags/struts-html.
      • <taglib-location> - defines a path (relative to the root directory of the webapp) to the TLD for this particular tag library, e.g., /WEB-INF/tld/struts-html.tld.