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
struts.xml (With Wildcard)
struts.xml (with multiple Wildcards)
If page names are taSomekeThing.jsp or taAnykeWhat.jsp and action name are taSomekeThing or taAnykeWhat respectively.
strut.xml ( Wildcard for Action Class)
If action name is takeEdit and action class is takeEditAction
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.
This mapping would work correctly for the URI taSomethingke but not for taSomekeThingke, because the latter would turn into this configuration:
struts.xml
* 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.
More Struts Topics :
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.
More Struts Topics :
- How to Set Struts2 Environment for Eclipse with Oracle 10g Express Edition
- Simple struts2 program with Eclipse & Apache Tomcat Server
- Login Using Struts2 and Eclipse
- Database Access using Struts2,Eclipse and Oracle
- Basic form Validation using Struts2 Framework with validate() method and ActionSupport class
- Validation using Struts2 Framework and XML file on Radio button and DropDownList with Eclipse
- Validation using Struts2 Framework and XML file with Eclipse Indigo
- AJAX validation using Struts2 Framework and XML file with Eclipse Indigo
- Session Tracking using SessionAware Interface with Struts2, Oracle Database and Eclipse IDE
- Action Wildcards with Struts2
- How to Configure methods in Action mappings with or without Wildcards using Struts2
- Store and Retrieve Numeric, Alphanumeric and Blob (Image) data with Struts2, Oracle 10g XE and Eclipse Indigo as IDE
- How to use Struts2 Iterator tag to get Users Details with Oracle 10g XE and Eclipse
- How to iterate user details through ArrayList with Iterator tag in Struts2 using Oracle 10g XE and Eclipse
- How to upload Multiple Files- Images (blob data) with enctype="multipart/form-data" in Oracle Database using Struts2, JDBC and Eclipse IDE
No comments:
Post a Comment