Home > Articles > Programming > Java

  • Print
  • + Share This
From the author of

Action-Mappings

The action-mappings section of the struts-config.xml file is by far the most important one because it is the one that defines the application's workflow: This determines which request is mapped to which Action subclass and, from there, which possible forwards can be invoked, adding the global-forwards to the list. Action subclasses represent the processes implemented by the web application. The action-mapping element defines which input forms generate data, whether the data entered should be validated, and whether the ActionForm bean representing that data will be found on the session or the request scope.

The action element has a number of attributes defining these parameters of the mapping, and the body of the action element contains the set of forwards that can be invoked from the related Action class and added to the global-forwards available, if any. It also contains property initialization information for custom ActionMapping classes, along with a number of presentation-oriented elements—icon, display-name, and description, meant to support development tools. The attributes on the action element are: id, attribute, className, forward, include, input, name, parameter, path, prefix, scope, suffix, type, unknown, and validate. These are the properties defined in the ActionMapping class, with the exception of className, which is optional and defines a custom ActionMapping class to be used in this mapping, and id, which identifies this mapping.

The body of the mapping element contains the forwards that can be used by the Action class after processing. These are forward elements, the same kind as defined in the global-forwards element.

The set-property element is used when you subclass the ActionMapping class and add custom properties to it. Using set-property elements, you can still benefit from the way the struts-config.xml file gets loaded into ActionMapping instances by the Digester utility. With this element, you can specify initial values for the specific properties on your ActionMapping subclasses without having to alter the struts-config DTD for every ActionMapping customization implemented.

Listing 4 shows a fragment of a struts-config.xml action-mappings element, with several actions of the ArticleDB application configured.

Listing 4 - Action-Mappings on the struts-config.xml File

<action-mappings>

 <!— The basic query in the application -->
 <action  path="/getArticleList"
type="jspbook.example.GetArticleListAction"
       name="articleListForm"
      scope="request"
     validate="false">
   <forward name="success" path="/articleList.jsp"/>
 </action>

 <!— Detail info on a certain article -->
 <action  path="/getArticleDetail"
       type="jspbook.example.articleDB.GetArticleDetailAction"
       name="articleForm"
      scope="request"
     validate="false">
   <forward name="success" path="/articleDetail.jsp"/>
 </action>

 <!— Centralized logon -->
 <action  path="/logon"
       type="jspbook.example.authcontrol.LogonAction"
       name="logonForm"
      scope="request"
      input="/logon.jsp"
    className="jspbook.example.authcontrol.SecSensitiveMapping"    
     validate="true">
   <forward name="success" path="/menu.jsp"/>
   <set-property name="secExtraInfo" value="public level"/>
   <set-property name="authContext" value="articleDB"/>
 </action>

...

</action-mappings>  

The first action in Listing 4 maps the /getArticleList request path with the GetArticleListAction class, which needs an ArticleListForm bean. In case of success, control will be redirected to the articleList.jsp page. This form bean won't be validated.

The second action shows more detailed information on a specific article. Again, there is the ArticleForm bean to hold information; it won't be validated. In case of success, articleDetail.jsp is called.

The third mapping presents a centralized logon facility, something that is pretty common in many organizations. Everything is different here. There is an outsider LogonAction class coming from the authControl package, together with SecSensitiveMapping, which has two extra attributes. These are initialized by set-property elements inside the action mapping. The className attribute declares the specific ActionMapping used in this example.

  • + Share This
  • 🔖 Save To Your Account