Dynamic method invocation and using wildcards to implement struts1.x-like dispatchaction functionality

Source: Internet
Author: User

Java EE Development of various resources download list, the history of the most IT resources, personal collection summary.


The STRUTS2 also provides similar functionality in STRUTS1 by invoking any of the methods in the action by inheriting the parameter parameters in the Dispatchaction class and Struts-config.xml, and there are two main ways:

1. Dynamic Method Invocation

If there are multiple methods in action, we can use the namespace+action name +! + Method Name calls the specified method. such as Http://localhost:8080/Struts2_03/user/userOperator!delete.action. but Struts2 officially does not recommend this approach, and he recommends using wildcards to implement functions like struts1.x dispatchaction.

By default, the dynamic method call of Struts is active, and if you want to disable the feature, you can add the following constant element to the Struts.xml file:
<constant name= "Struts.enable.DynamicMethodInvocation" value= "false"/>

Useraction.java

Struts-user.xml

When the browser input: http://localhost:8080/Struts2_03/user/useroperator(do not write the method, the default method of calling Methods property), .../user/ Useroperator!add,

.../user/useroperator!add.action


When browser input: http://localhost:8080/Struts2_03/user/useroperator!delete, .../user/useroperator! Delete.action


2. Wildcard calls to methods in action

You can use the "*" wildcard character in the <action> Name property, and then, in the <action>class,method property and the <result> 's Name property and the <result> value, pass "{ Wildcard index} "refers to the value of a wildcard" * ", such as:

Wildcard Mappings

(1) A WEB app may have hundreds of action declarations. You can use the wildcard mapping mechanism provided by struts to simplify multiple, similar mappings to a single mapping relationship

(2) Wildcard mapping rules
If more than one match is found, the one without the wildcard will prevail. If the specified action does not exist, struts will attempt to match the URI with any action name that contains the wildcard * and matches it. If struts finds more than one match with wildcards, the last match wins by wildcards The substring of the URI string can be referenced with {1}, {2}. {1} matches the first substring, {2} matches the second substring, {0} matches the entire URI * can match 0 or more characters, but not/characters. If you want to include/characters, you need to use * *. If you need to escape a character, you need to use \.

use of wildcard characters

Struts-product.xml


Productaction.java


Browser input: Http://localhost:8080/Struts2_03/product/ope_Product_delete, .../product/ope_product_delete.action


Browser input: Http://localhost:8080/Struts2_03/product/ope_Product_add, .../product/ope_product_add.action


When forwarding, note the source: Dynamic method invocation and using wildcards to invoke methods in action

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.