Versions Compared

Key

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

...

Code Block
titleA Hello Action
<action name="Hello" class="cookbook2.Hello">
  <result>/pages/Hellohello/Result.jsp</result>
  <result name="input">/pageshello/Hello/Input.jsp</result>
</action>

...

Within an application, the link to an action is usually generated by a SAF tagStruts 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
<saf<s:form action="Hello" method="POST">
    <saf<s:textfield label="Please enter your name" name="name"/>
    <saf<s:submit/>
</safs:form>

Action Aliases

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

...

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

Bang Aliases

The Action method to execute can also be embedded in the form or tag reference. In either tag or form reference, use an exclamation mark (or "bang") to specify both the action name and method.

Code Block

<a href="<saf:url action="Register!input"/>">Register</a>

The idiom "Register!input" will invoke the input method on the Action class mapped to the Register action.

Making Do

However, the method is named or selected, the method must be public, take no arguments, and return a String. The String indicates the name of the result to execute.

If an alias method is used, the method name must either match the alias name or the alias name prefixed by "do".

Code Block

<a href="<saf:url action="Register!default"/>">Register</a>
Code Block

public String doDefault() throws Exception {
  ...
  return SUCCESS;
}

...

Wildcard Alaises

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.

Rather than code a separate mapping for each action class that uses this pattern, you can write it once as a wildcard mapping.

Code Block
xml
xml

<action name="*Crud" class="example.Crud" method="{1}">

Here, a reference to "editCrud" will call the edit method on an instance of the Crud Action class. Likewise, a reference to "deleteCrud" will call the delete method instead.

Another common approach is to "postfix" the method name and set it off with an exclamation point.

  • "action=Crud!input"
  • "action=Crud!delete"

To use this sort of reference, just move the asterisk and add a bang.

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
Warning

The "!" notation is also available in WebWork 2, but it is implemented differently. To use the old implementation, set }} in the {{struts.properties file. To use wildcards instead, set set {{}}.

ActionSupport Default

If the class attribute in an action mapping is left blank, the com.opensymphony.xwork.ActionSupport class is used as a default.

...

A good practice is to link to actions rather than pages. An effect of this strategy is that an action will fire before a page renders. Another common stategy is to first render a page using an alias, and then have it submit back to the default execute method. Using these 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.

...

titleLinking to Input

...

Code Block
Mapping to Input
title

<action name="Hello" class="cookbook2.Hello">
  <result>/pages/Hello/Result.jsp</result>
  <result name="input">/pages/Hello/Input.jsp</result>
</action>
Code Block
titlePosting Back
<saf:form method="POST"><s:form>
    <ww<s:textfield label="Please enter your name" name="name"/>
    <ww<s:submit/>
</safs:form>

If validation passes, control will pass to the Result.jsp; otherwise, control passes back to Input.jsp.

Action Default

Usually, if an action is requested, and the framework can't map the request to an action name, the result will be the usual "404 - Page not found" error. But, if you would prefer that an ominbus action handle any unmatched requests, you can specify a default action. If no other action matches, the default action is used instead.

...

Warning
titleOne for 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.

Wildcard Default

Using wildcards is another approach to default actions. A wildcard action at the end of the configuration can be used to catch unmatched references.

Code Block
xml
xml

<action name="*" >
  <result>/(1).jsp</result>
</action>

When a new action is needed, just add a stub page.

FAQ

How can we display dynamic or static images that can be provided as an array of bytes?
How can we test Actions?
How can we force the Action Mappings (struts.xml) to reload?