The struts2.5 framework uses wildcards to specify methods (Common Errors), and struts2.5 wildcards

Source: Internet
Author: User

The struts2.5 framework uses wildcards to specify methods (Common Errors), and struts2.5 wildcards

When learning the struts framework, you will often use the wildcard call method as follows:

<Package name = "shop" namespace = "/" extends = "struts-default"> <! -- Configure Action --> <actionname = "user _ *" class = "userAction" method = "{1}"> <result name = "success">/WEB-INF/jsp/login. jsp </result> </action> </package>

In the action name = "user _ *", * the value represented by this symbol is passed into method = "{1}" and corresponds to a method name of the action class, this greatly reduces the number of actions in the configuration document.

 

However, when using this wildcard method, such a ing error message is often displayed.

Struts has detected an unhandled exception:Message:There is no Action mapped for namespace [/] and action name [user_login] associated with context path [/shop].

If you see a ing problem, you can exclude it according to the ing route,

Step 1: Check whether there is any problem with the access Link (details)

Step 2: Check the struts. xml configuration (check carefully and there is a high chance of problems)

Step 3: Check the related action classes and methods (for example, whether the return value matches the result in the configuration file)

Step 4: Check whether there is a problem on the result response page (the probability of a problem is relatively small)

(I will not elaborate on the specific practices. Many excellent chapters on the Internet are mentioned. You can use Baidu on your own)

 

If no problem occurs in the above four steps, but an error is still reported, what should I do? It may be a problem with internal attribute configuration.

In the Struts 2 core jar package struts2-core, there is a default. properties default configuration file (Path: struts-2.5.2-min \ lib \ org \ apache \ struts2 \ default. properties ).

One of the statements is used to configure dynamic method calls.

struts.enable.DynamicMethodInvocation = true

 

When you use a dynamic call method (action name + exclamation point + method name for method call), you need to change its attribute to true,

When using the wildcard call syntax, we recommend that you change its attribute to false (the default value in struts2.5.2 is false)

When we need to change its attribute to false,

You can modify attributes only by adding this sentence to the struts. xml configuration file.

<constant name="struts.enable.DynamicMethodInvocation" value="false" />

 

Many netizens say that it will work after the change, but after I changed struts2.5, it still won't work after a long time ......

Finally, configure

<Allowed-methods> method name in Action </allowed-methods>

 

For example, the above Code is modified as follows:

<Package name = "shop" namespace = "/" extends = "struts-default"> <! -- Configure Action --> <action name = "user _ *" class = "userAction" method = "{1}"> <result name = "success">/WEB-INF/jsp/ login. jsp </result> <allowed-methods> login </allowed-methods> </action> </package>

Note: The login in the red code is the method name in the corresponding Action class. You can add multiple methods. The method names are separated by commas.

 

Conclusion: In versions earlier than struts2.3, normal configuration is acceptable. However, after struts2.3, when calling methods using wildcards, internal verification is performed to determine whether to allow access to this method. Therefore, you must add

<Allowed-methods> method name 1, method name 2... </Allowed-methods> code.

I hope this little knowledge will help more programmers. On the java Road, I would like to share with like-minded people !!

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.