STRUTS2 Action Wildcard

Source: Internet
Author: User

First, look at a struts2 configuration file:

< Packagename= "Actions"extends= "Struts-default"namespace= "/actions">    <Actionname= "student*"class= "Com.bjsxt.struts2.action.StudentAction"Method= "{1}">     <result>/student{1}_success.jsp</result>    </Action></ Package> 

Here, "{1}" represents a 1th *, for example, when the following path is requested:

Reference Http://localhost:9000/Struts2_0600_ActionWildcard/actions/Studentadd


* For Add, so method= "add", the page returned is/studentadd_success.jsp
That is, the Add method in Studentaction is actually called.

The STRUTS2 configuration file can also be more concise, that is, the use of wildcards in the action class, as follows:

< Packagename= "Actions"extends= "Struts-default"namespace= "/actions">      <Actionname="*_*"class= "Com.bjsxt.struts2.action." {1} Action "Method= "{2}">          <result>/{1}_{2}_success.jsp</result>             </Action>         </ Package>

Using two wildcard characters, {1} represents the second * number for the first *,{2}.
This configuration file means to call {1}action {2} method, return to {1}_{2}_success.jsp this page. For example, when you request a path:

Reference
Http://localhost:9000/Struts2_0600_ActionWildcard/actions/Teacher_add


where {1} is teacher,{2} is add, which is actually called the Add method in the Teacheraction class and returns to the Teacher_add_success.jsp page.

As you can see, using the second wildcard, the configuration file is very concise, even if you add classes and methods, and you do not need to modify the configuration file, so we recommend using wildcards in a second way.

Struts2 the Action using wildcard characters in a configuration

1). XML code

1. <action name= "user*" class= "com. Useraction "method=" {1} ">
2. <result name= "Succee" >/logon/welcome.jsp</result>
3. <result name= "Input" >/logon/logon.jsp</result>
4. </action>

<action name= "user*" class= "com. Useraction "method=" {1} ">
<result name= "Succee" >/logon/welcome.jsp</result>
<result name= "Input" >/logon/logon.jsp</result>
</action>

For example, when a user requests Userlogon, it is handled by the Useraction class, and the {1} value in Method{1} is the first value in the Name property.

2). XML code

1. <action name= "*action_*" class= "Com.{1}" method= "{2}" >
2. <result name= "Succee" >/logon/welcome.jsp</result>
3. <result name= "Input" >/logon/logon.jsp</result>
4. </action>

<action name= "*action_*" class= "Com.{1}" method= "{2}" >
<result name= "Succee" >/logon/welcome.jsp</result>
<result name= "Input" >/logon/logon.jsp</result>
</action>

When the user requests Useraction_logon, it is handled by the logon method of the Useraction class
As above, the value of {1} in class is the first value in the name attribute, that is, the value of Useraction,{2} is the second value in the Name property, which is the logon

STRUTS2 Action Wildcard

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.