Wildcard Mappings

本文介绍Struts 2框架中如何使用通配符映射简化配置过程。通过实例展示如何匹配所有以/edit开头的页面请求,并解释了如何使用特殊标记如*、**及来实现灵活的路径匹配。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

       As an application grows in size, so will the number of action mappings. Wildcards can be used to combine similar mappings into one more generic mapping.

     The best way to explain wildcards is to show an example and walk through how it works. This example modifies a conventional mapping to use wildcards to match all pages that start with /edit:

<!-- Generic edit* mapping -->
<action name="/edit*" class="org.apache.struts.webapp.example.Edit{1}Action">
    <result name="failure" path="/mainMenu.jsp"/>
    <result path="/\{1\}.jsp"/>
</action>

     The "*" in the path attribute allows the mapping to match the request URIs /editSubscription, editRegistration, or any other URI that starts with /edit, however /editSubscription/add 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.

    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.

     Also, the action mapping properties (set using the <set-property key="foo" value="bar"> syntax) will accept wildcard-matched strings in their value attribute.

     Like the action mapping, the action result properties (set using the <set-property key="foo" value="bar"> syntax) will accept wildcard-matched strings in their value attribute.

 

from : http://struts.apache.org/2.0.6/docs/wildcard-mappings.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值