Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Tying It All Together: The struts-config.xml File

As mentioned before, the Struts framework enables you to break an application down into components to simplify and speed development. The job of the struts-config.xml file is to let you specify how the components go together and identify when they should be used.

Listing 3.7 shows the struts-config.xml file for the HelloWorld application.

Listing 3.7 The Struts Configuration File for HelloWorld! (struts-config.xml)

<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC
     "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"

   This is the Struts configuration file for the "Hello!" sample application


  <!-- ======== Form Bean Definitions =================================== -->
    <form-bean name="HelloForm" type="ch03.hello.HelloForm"/>

 <!-- ========== Action Mapping Definitions ============================== -->
  <!-- Say Hello! -->
  <action  path   = "/HelloWorld"
        type   = "ch03.hello.HelloAction"
        name   = "HelloForm"
        scope   = "request"
        validate = "true"
        input   = "/hello.jsp"
    <forward name="SayHello" path="/hello.jsp" />

 <!-- ========== Message Resources Definitions =========================== -->

 <message-resources parameter="ch03.hello.Application"/>


This is a bare-bones configuration file with only a single <form-bean>, one <action>, and one <message-resource> entry. Other possible elements include <plug-in>, <data-source>, and <global-forwards>, among others. More detail on these elements is provided in Chapter 10, "The struts-config.xml File: Tying It All Together." For detailed information that is guaranteed to be correct for your version of Struts, refer to the Document Type Definition (DTD) file for your particular release of Struts. For Struts version 1.1, this DTD is located at http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd.

In real-world language, this configuration file says:

  • There is only a single form bean defined for this application. It's referred to as HelloForm and is defined in the class ch03.hello.HelloForm.

  • There is only a single Action defined for the application. It's invoked by requesting the path (or URI, to be specific) /webappname/HelloWorld. For example, if this application were deployed on the server myServer in the Web application archive hello.war, the Action would be invoked by requesting the path http://myServer/hello/HelloWorld.

  • When the Action is invoked, it expects the HelloForm form bean to be passed to it. The form bean should be request scope and should validate the user's input prior to the Action class being invoked. If the validation fails, the user should be sent to the input page /hello.jsp to correct his entries.

  • There is only a single MessageResources bundle associated with this application. The text messages to be stored in this MessageResources are located in the file ch03/hello/Application.properties, somewhere on the application classpath.

  • + Share This
  • 🔖 Save To Your Account