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 a set of result types, a set of exception handlers, and an interceptor stack. Only the name
attribute is required. The other attributes can also be provided at package scope.
Code Block |
---|
|
<action name="Logon" class="tutorial.Logon">
<result type="redirectAction">Menu</result>
<result name="input">/Logon.jsp</result>
</action>
|
...
Code Block |
---|
|
<s:form action="Hello">
<s:textfield label="Please enter your name" name="name"/>
<s:submit/>
</s:form>
|
...
Code Block |
---|
|
public interface Action {
public String execute() throws Exception;
}
|
...
Sometimes, developers like to create more than one entry point to an Action. For example, in the case of a data-access Action, a developer might want separate entry-points for create
, retrieve
, update
, and delete
. A different entry point can be specified by the method
attribute.
Code Block |
---|
|
<action name="delete" class="example.CrudAction" method="delete">
...
|
If there is no execute
method and no other method specified in the configuration the framework will throw an exception.
excerptDefaultActionInvocation
will try to call also doDelete
if it couldn't find delete
method in action's class.
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.
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 |
---|
|
<action name="*Crud" class="example.Crud" method="{1}">
...
|
...
To use a postfix wildcard, just move the asterisk and add an underscore.
Code Block |
---|
|
<action name="Crud_*" class="example.Crud" method="{1}">
|
...
Code Block |
---|
| xml |
---|
title | Example struts.xml |
---|
| xml |
---|
|
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
<package name="default" extends="struts-default" strict-method-invocation="true">
<action name="index" class="org.apache.struts2.examples.actions.Index">
<result name="success" type="redirectAction">hello</result>
</action>
<action name="hello" class="org.apache.struts2.examples.actions.HelloAction">
<result name="success">/WEB-INF/content/hello.jsp</result>
<result name="redisplay" type="redirectAction">hello</result>
<allowed-methods>add</allowed-methods>
</action>
</package>
</struts>
|
...
If the class attribute in an action mapping is left blank, the com.opensymphony.xwork2.ActionSupport
class is used as a default.
Code Block |
---|
<action name="Hello">
// ...
</action>
|
...
Code Block |
---|
|
<s:form>
<s:textfield label="Please enter your name" name="name"/>
<s:submit/>
</s:form>
|
...
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 omnibus action handle any unmatched requests, you can specify a default action. If no other action matches, the default action is used instead.
Code Block |
---|
|
<package name="Hello" extends="action-default">
<default-action-ref name="UnderConstruction"/>
<action name="UnderConstruction">
<result>/UnderConstruction.jsp</result>
</action>
...
|
...
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 |
---|
|
<action name="*">
<result>/{1}.jsp</result>
</action>
|
...