Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The action mapping can specify an interceptor stack, a set of result types, and a set of exception handlers, and an interceptor stack. But, only the name attribute is required. (Everything else The other attributes can also be provided at a global package scope.)

Code Block
titleA Hello Logion Action
<action name="HelloLogon" class="cookbook2tutorial.HelloLogon">
   <result>/hello/Result.jsp</<result type="redirect-action">Menu</result>
  <result name="input">/hellotutorial/InputLogon.jsp</result>
</action>

Action Names

In a web application, the name attribute is matched a part of the location requested by a browser (or other HTTP client). The framework will drop the host and application name and the extension, and match what's in the middle. So, a request for http://www.planetstruts.org/action2struts2-cookbookmailreader/HomeWelcome.do will map to the Home Welcome action.

Within an application, the link to an action is usually generated by a Struts Tag. The tag can specify the action by name, and the framework will render the default extension and anything else that is needed.

Code Block
titleA Hello Form
<s:form action="Hello" method="POST">
    <s:textfield label="Please enter your name" name="name"/>
    <s:submit/>
</s:form>

Action

...

Methods

The default entry method to the handler class is defined by the Action interface.

...

Sometimes, developers like to create more than one entry point to an Action. For example, in the case of of a data-access Action, a developer might want separate entry-points for create, retrieve, update, and delete. A different entry point , or "alias", can be specified by the method attribute.

...

(warning) If there is no execute method, and no other method, specified in the configuration, the framework will throw an exception.

Wildcard

...

Method

Many times, a set of action mappings will share a common pattern. For example, all your edit actions might start with the word "edit", and call the edit method on the Action class. The delete actions might use the same pattern, but call the delete method instead.

...

Another common approach is to postfix the method name and set it off with an exclamation point (aka "bang"), underscore, or other special character.

  • "action=Crud!_input"
  • "action=Crud!_delete"

To use a postfix -bang idiomwildcard, just move the asterisk and add a bangan underscore.

Code Block
xml
xml
<action name="Crud!_*" class="example.Crud" method="{1}">

From the framework's perspective, a wildcard mapping creates a new "virtual" mapping with all the same attributes as a conventional, static mapping. As a result, you can use the expanded wildcard name as the name of validation, type conversion, and localization files, just as if it were an Action name (which it is!).

  • Crud!_input-validation.xml
  • Crud!_delete-conversion.xml

(minus) The postfix "!" notation is also available in WebWork 2, but it is implemented differently. To use the old implementation, set struts.enable.DynamicMethodInvocation=TRUE in the struts.properties file. To use wildcards instead (preferred), set struts.enable.DynamicMethodInvocation=FALSE.

...

A good practice is to link to actions rather than pages. An effect of this strategy is that an action will Linking to actions encapsulates which server page renders, and ensures that an Action class can fire before a page renders.

Another common workflow stategy is to first render a page using an aliasalternate method, like input and then have it submit back to the default execute method.

Using these two strategies together creates an opportunity to use a "post-back" form that doesn't specify an action. The form simply submits back to the action that created it.

...

There are no special requirements for the default action. Each package can have its own default action, but there should only be one default action per namespace.

Warning
titleOne for to a Namespace

The default action features should be setup so that there is only one default action per namespace. If you have multiple packages declaring a default action with the same namespace, there is no guarantee which action will be the default.

...