[ Team LiB ] Previous Section Next Section

struts-config.xml File

The struts-config.xml file resides in the same directory as the web.xml file and contains the application flow mappings necessary for correct interaction with Struts. Looking at the sample struts-config.xml shown in Listing 19.2, we can quickly see the important features.

Listing 19.2 struts-config.xml
1 <?xml version="1.0" encoding="ISO-8859-1" ?>
2 <!DOCTYPE struts-config PUBLIC
3        "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
4        "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
5 <struts-config>
6 <data-sources>
7        <data-source key = "myDatabase">
8          <set-property property="autoCommit"
9                           value="false"/>
10          <set-property property="description"
11                           value=" Login Validation Database"/>
12          <set-property property="driverClass"
13                           value="org.postgresql.Driver"/>
14          <set-property property="maxCount"
15                           value="4"/>
16          <set-property property="minCount"
17                            value="2"/>
18           <set-property property="user"
19                           value="myusername"/>
20          <set-property property="password"
21                           value="mypassword"/>
22          <set-property property="url"
23                           value="jdbc:postgresql://localhost/myAppDatabase"/>
24        </data-source>
25      </data-sources>
26 <!-- ========== Form Bean Definitions =================================== -->
27 <form-beans>
28  <!-- Logon form bean -->29  <form-bean      name="login"
30                  type="myApp.FormBeans.LoginForm"/>
31     <!-- Order List -->
32  <form-bean      name="orderlist"
33                  type="myApp.FormBeans.OrderForm"/>
34 </form-beans>
35 <!-- ========== Global Forward Definitions ============================== -->
36  <global-forwards>
37    <forward   name="login"                path="/login.jsp"/>
38  </global-forwards>
39 <!-- ========== Action Mapping Definitions ============================== -->
40  <action-mappings>
41 <!-- Process a user logon -->
42    <action    path="/login"
43               type="myApp.action.LoginExec"
44               name="login"
45               validate = "true"
46              scope="session"
47              input="/login.jsp">
48             <forward name="success"       path="/secondpage.jsp"/>
49             <forward name="listorder"     path="/orderlistentry.do"/>
50             <forward name="killSession"   path="/SystemError.html"/>
51    </action>
52 <!-- Process a user logoff -->
53    <action    path="/logout"
54               type="myApp.action.LogoutExec">
55      <forward name="success"              path="/logout.jsp"/>
56    </action>
57 <!-- Process an order list -->
58    <action path="/orderlistentry"
59            type="myApp.action.OrderListExec"
60            name="orderlist"
61           scope="session"
62           input="/orderlistentry.jsp">
63      <forward name="success" path="/orderlistdisp.jsp"/>
64      <forward name="killSession" path="/SystemError.html"/>
65      <forward name="loginAgain" path="/login.jsp"/>
66      <forward name="selectSalesArea" path="/salesAreas.jsp"/>
67    </action>
68  </action-mappings>
69 </struts-config>

The first important actions occur on lines 26–34, where the form beans are registered. Form beans are classes used to store actions and values from the JSP page. The parameters are

  • name, which names the form bean

  • The full package and class name of the applications page bean that extends the struts org.apache.struts.action.ActionForm class

Sample code is given later in the chapter.

The lines 35–38 deal with global forwards. This registers the name login to the login.jsp page.

The most important part of the struts-config.xml file is the action-mappings section. Lines 39–68 give three examples of action mappings used in an application. Struts Optionally allows us to perform data mappings to different databases. This is shown on lines 6–24. This isn't needed; it's just a nice feature for simple applications.

<action-mappings>

Each <action> element defines one specific task associated with one action class for each defined mapping. Each associated action performs a task that will forward to one of the <forward/> XML tags. The sample code uses three actions: login, logout, and orderlistentry. Each action is associated with a *.do extension from the web.xml configuration file. So, when calling an action from the Web server, the action always has a .do appended. Most action elements define at least

  • path— The path the application maps to the action. For instance, http://localhost/myserver/login.do would call the user login in the example that follows.

  • type— The full package and class of the action needed.

  • name— The name of the <form-bean> element that's used with the action.

<forward>

The <forward> tag tells the Struts framework what page to go to next. The names of these tags are used within the action classes to look up the correct page for an associated action. Most forward elements define at least

  • name— The name of the forward to be performed.

  • path— The full path to another action, local JSP page, or remote page.

<data-sources>

The <data-source> element provides the ability to map multiple databases within the parent element, <data-sources>, in one place. An attribute named key allows the connections to be distinguished from one another. Within the <data-source> element is an element named <set-property> that defines

  • property— The name of the data property

  • value— The value of the data property

When the struts-config.xml file is finished, it will contain the skeleton of the application. Each action will have associated forwards mapping to other actions, JavaServer pages, or servlets.

    [ Team LiB ] Previous Section Next Section