The action mappings are the basic "unit-of-work" in the framework. Essentially, the action maps an identifier to a handler class. When a request matches the action's name, the framework uses the mapping to determine how to process the request.
Action Mappings
The action mapping can specify an interceptor stack, a set of result types, and a set of exception handlers. But, only the name
attribute is required. (Everything else can also be provided at a global scope.)
<action name="Hello" class="cookbook2.Hello"> <result>/pages/Hello/Result.jsp</result> <result name="input">/pages/Hello/Input.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/action2-cookbook/Home.do
will map to the Home
action.
Within an application, the link to an action is usually generated by a SAF tag. The tag can specify the action by name, and the framework will render the default extension and anything else that is needed.
<saf:form action="Hello" method="POST"> <saf:textfield label="Please enter your name" name="name"/> <saf:submit/> </saf:form>
Action Aliases
The default entry method to the handler class is defined by the Action interface.
public interface Action { public String execute() throws Exception; }
Implementing the Action interface is optional. If Action is not implemented, the framework will use reflection to look for an execute
method.
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.
<action name="delete" class="example.CrudAction" method="delete">
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.
<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
".
<a href="<saf:url action="Register!default"/>">Register</a>
public String doDefault() throws Exception { ... return SUCCESS; }
The do
prefix makes it possible to use alias names that might otherwise be invalid method names.
ActionSupport Default
If the class attribute in an action mapping is left blank, the com.opensymphony.xwork.ActionSupport
class is used as a default.
<action name="Hello"> // ... </action>
The ActionSupport class has execute
and input
methods that return "success".
Post-Back 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.
<a href="<saf:url action="Hello!input"/>">Register</a>
<action name="Hello" class="cookbook2.Hello"> <result>/pages/Hello/Result.jsp</result> <result name="input">/pages/Hello/Input.jsp</result> </action>
<saf:form method="POST"> <ww:textfield label="Please enter your name" name="name"/> <ww:submit/> </saf: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.
<package name="Hello" extends="action-default"> <default-action-ref name="UnderConstruction"> <action name="UnderConstruction"> <result>/UnderConstruction.jsp</result> </action>
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.
One 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.
FAQ
How can we display dynamic or static images that can be provided as a array of bytes?
How can we test Actions?
How can we force the Action Mappings (struts.xml) to reload?