Search code examples
struts-1struts-config

In struts-config.xml, an action has an "attribute" attribute in it. What's it do?


In our web app's struts-config.xml file, there are a bunch of <action>s defined, and they have both a name and an attribute attribute on most of them, with the form name in both. For example:

<action
    attribute="LoginForm"  <-- this line
    name="LoginForm"
    path="/welcome"
    type="com.foo.presentation.action.LandingActionPre">
    <forward name="SUCCESS" path="landing.welcome" />
    <forward name="ALREADY_LOGGED_IN" path="/landing.do?m=getLandingPage" redirect="true"/>
</action>

What does the attribute attribute do? Or is it not used and is present here by mistake?

This app has been around for ~10 years, and has seen over 40 programmers touch the code base during that time. So it's possible that one added it to a few actions and every one else copied and pasted it throughout the rest of the app not knowing any better.


Solution

  • According to the Struts 1 doc type definition, it is used as a sort of alias for the ActionForm.

    If it's the same as name, it does nothing. But if name is specified and attribute is specified and different from name, then the form can be accessed using the value of either name or attribute.

    Relevant section:

    <!-- The "action" element describes an ActionMapping object that is to be used
        to process a request for a specific module-relative URI. The following
        attributes are defined:
    
        attribute       Name of the request-scope or session-scope attribute that
                        is used to access our ActionForm bean, if it is other than
                        the bean's specified "name". Optional if "name" is specified,
                        else not valid.