Use of wildcard characters (WildCard)

Source: Internet
Author: User

First, about wildcard: a Web application, there are thousands of action declarations, you can use the mapping mechanism provided by STRUTS2 to simplify a number of similar mappings to a mapping relationship, that is, wildcard characters.

1. New class Actionwildcard, methods for validating wildcards

2.1 Add student two methods that need to be practiced add,deete

Package Cn.cqvie.wildcard;

Import Com.opensymphony.xwork2.ActionSupport;

public class Studentaction extends Actionsupport {
Public String Add () {
return SUCCESS;
}

Public String Delete () {
return SUCCESS;
}

}

2.2 Add teacher two methods that need to be practiced add,deete

Package Cn.cqvie.wildcard;

Import Com.opensymphony.xwork2.ActionSupport;

public class Teacheraction extends Actionsupport {
Public String Add () {
return SUCCESS;
}
Public String Delete () {
return SUCCESS;
}
}

3. The method indicates that a new two JSP page is needed to display the results of the operation

3.1 studentadd_success.jsp: Note that the student here must be capitalized

Just output a word inside:

<body>
Student ADD success! <br>
</body>

3.2 studentdelete_success.jsp: Method Ibid.

You just have to output a single word.

<body>
Student Delete success!<br>
</body>

3.3 teacher_add_success.jsp: Note that T must be capitalized here

Just output a word.

<body>
Teacher ADD success!<br>
</body>

3.4 teacher_delete_success.jsp: Note that T must be capitalized here

Output a sentence prompt:

<body>
Teacher Delete success! <br>
</body>

4. Configure the contents of the Struts.xml file: * * *

<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.1//en" "http://struts.apache.org/dtds/ Struts-2.1.dtd ">
<struts>
<constant name= "Struts.devmode" value= "true"/>
<package name= "Actions" extends= "Struts-default" namespace= "/actions" >
<action name= "student*" class= "cn.cqvie.wildcard.StudentAction" method= "{1}" >
<result>/Student{1}_success.jsp</result>
</action>
<action name= "*_*" class= "Cn.cqvie.wildcard. {1} Action "method=" {2} ">
<result >/{1}_{2}_success.jsp</result>
</action>
</package>
</struts>

Note: "*" Here represents all, that is, all methods that student has, {1}= "*", that is, "{1}" means "add" when "*" stands for "add". This means that "*" is consistent with the "{}" content.

"*_*" stands for: the first "*" stands for "Teacher"; the second "*" represents the "add" method or the "delete" method.

5. Modify the mapping in Web. xml: You need to change the Url-pattern original "*.action" to "/*", otherwise you cannot find the method corresponding to the actions.

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping></web-app>

6. Run/debug

Deploy the project on Tomcat--Open browser--Enter the name of the item in the Address bar--carriage return

6.1 Click "Add Student" to find the Address bar change: Action can successfully find student "add" method

6.2 Click "Delete Student" to find the Address bar change: Action can successfully find student "Delete" method

6.3 Click "Add Teacher" to find the Address bar change: Action can successfully find teacher "add" method

6.4 Click "Delete Teacher" to find the Address bar change: Action can successfully find teacher "Delete" method

7. Summary and Lessons learned

1) when more than one match is found, the one with no wildcards wins;

2) If the specified action does not exist, struts will match the URI with any of the wildcard characters that contain the action name "*";

3) It is important to note that when you configure the "Struts.xml" file, the corresponding "action" action should be consistent with it, otherwise the Access object will not be found, and a 404 error is reported.

4) Follow the "Convention better than the configuration" principle, you can make it easier to configure. For example, the first letter of the class needs to be capitalized, and the method name needs lowercase.

Use of wildcard characters (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.