Home > Articles > Programming > Java

The Struts Framework's Action Mappings Configuration File

  • Print
  • + Share This
Wellington L.S. da Silva, author of JSP and Tag Libraries for Web Development, shows you how to set up the Struts configuration file, one of the most important components of the Struts framework. The struts-config.xml file allows the developer to set application information, such as multiple data sources and general redirection links; to define the components used to represent most of the application state, the ActionForm beans; and to define and fully describe the actions to be performed by the application. Each of these elements is fully presented and exemplified.
This article is excerpted from JSP and Tag Libraries for Web Development, by Wellington L.S. da Silva.
Like this article? We recommend

The main control file in the Struts framework is the struts-config.xml XML file, where action mappings are specified. This file's structure is described by the struts-config DTD file, which is defined at http://jakarta.apache.org/struts/. A copy of the DTD can be found on the /docs/dtds subdirectory of the framework's installation root directory. The top-level element is struts-config. Basically, it consists of the following elements:

  • data-sources—A set of data-source elements, describing parameters needed to instantiate JDBC 2.0 Standard Extension DataSource objects

  • form-beans—A set of form-bean elements that describe the form beans that this application uses

  • global-forwards—A set of forward elements describing general available forward URIs

  • action-mappings—A set of action elements describing a request-to-action mapping

Each of the subelements listed is described in the next sections.

Data Sources and the data-source Element

A data-source element describes the parameters necessary to configure a JDBC 2.0 Standard Extension DataSource. These parameters are defined as attributes of the data-source element:

  • autoCommit—The default auto-commit state to be set when creating a new connection to the database.

  • description—A description for this data source.

  • driverClass—The complete Java class name of the JDBC driver to be used. This is a required attribute.

  • key—Once created, this DataSource will be stored under an attribute on the application servlet context. This attribute holds the name to be used for the context's attribute. The default attribute name is specified by the Action.DATA_SOURCE_KEY String.

  • loginTimeout—The maximum number of seconds to wait for a connection to be created or returned.

  • maxCount—The maximum number of connections to be created.

  • minCount—The minimum number of connections to be created.

  • password—The database password to use when connecting. This is a required attribute.

  • readOnly—The default read-only state for newly created connections.

  • url—The JDBC URL to use when connecting. This is a required attribute.

  • user—The database username to use when connecting. This is a required attribute.

The code fragment in Listing 1 describes a data-sources element with two DataSources defined.

Listing 1 - Defining DataSources in the data-sources Element

   description="First Database Config"
   driverClass=" org.gjt.mm.mysql.Driver"
   description="Second Database Config"

The code fragment in Listing 1 shows a configuration with two databases: a MySQL-based ARTICLEDB database and an Oracle-based AUTHORDB database. The ARTICLEDB DataSource element is stored under the default key, the default attribute name defined by Action.DATA_SOURCE_KEY. The second database DataSource will be stored under the attribute named REFDB.

  • + Share This
  • 🔖 Save To Your Account