Thursday, August 8, 2013

Action Wildcards with Struts2

When we deal with a bigger Application we have to deal with more Actions.Wildcards can be used to combine similar mappings into one more generic mapping.

Let take an example to understand Action wildcards :

struts.xml (Without Wildcard)
Inside Action Tag Action name="takeForm" and result tag is rediecting to takeForm.jsp and for another Action Tag Action name="takeChart" and result tag is rediecting to takeChart.jsp
 <action name="takeForm>  
 <result>takeForm.jsp</result>  
 </action>  
 <action name="takeChart">  
 <result>takeChart.jsp</result>  
 </action>   
but we want to perform both actions with only one action and one result tag which is possible through wildcards.

struts.xml (With Wildcard)
 <action name="take*>  
 <result>take{1}.jsp</result>  
 </action>  

struts.xml (with multiple Wildcards)
If page names are taSomekeThing.jsp or taAnykeWhat.jsp and action name are taSomekeThing or taAnykeWhat respectively.
 <action name="ta*ke*>  
 <result>ta{1}ke{2}.jsp</result>  
 </action>  
In the action mapping and action results, the wildcard-matched values can be accessed with the token {N} where N is a number from 1 to 9 indicating which wildcard-matched value to substitute.

strut.xml ( Wildcard for Action Class)
If action name is takeEdit and action class is takeEditAction
 <action name="take*" class="blog.webideaworld.in.take{1}Action">  
 <result>success.jsp</result>  
 </action>  

The "*" in the path attribute allows the mapping to match the request URIs /takeSummer, takeWinter, or any other URI that starts with /take, however /takeSummer/bad would not be matched.
The part of the URI matched by the wildcard will then be substituted into various attributes of the action mapping and its action results replacing {1}.
For the rest of the request, the framework will see the action mapping and its action results containing the new values.

Mappings are matched against the request in the order they appear in the framework's configuration file.
If more than one pattern matches the last one wins, so less specific patterns must appear before more specific ones.
However, if the request URL can be matched against a path without any wildcards in it, no wildcard matching is performed and order is not important.
 Also, note that wildcards are not greedy, meaning they only match until the first occurrence of the following string pattern. 

For example, consider the following mapping:
struts.xml
If page names are taSomeke.jsp or taAnyke.jsp and action name are taSomeke or taAnyke respectively.
 <action name="ta*ke>  
 <result>ta{1}ke.jsp</result>  
 </action>  

This mapping would work correctly for the URI taSomethingke but not for taSomekeThingke, because the latter would turn into this configuration:

struts.xml
 <action name="taSomeke>  
 <result>taSomeke.jsp</result>  
 </action>  
Wildcard patterns can contain one or more of the following special tokens:
* Matches zero or more characters excluding the slash ('/') character.
** Matches zero or more characters including the slash ('/') character.
\character The backslash character is used as an escape sequence. Thus '\*' matches the character asterisk ('*'), and '\\' matches the character backslash ('\').

In the action mapping and action results, the wildcard-matched values can be accessed with the token {N} where N is a number from 1 to 9 indicating which wildcard-matched value to substitute.
The whole request URI can be accessed with the {0} token.

No comments:

Popular Posts