Action wildcard no longer works after Struts 2.5 update

I have the following action mapping in a struts.xml application that works fine with Struts 2.3.28.1; invokes the /editApplication action, where it is processed by the x.ApplicationHandler.edit method.

 <action name="*Application" class="x.ApplicationHandler" method="{1}"> <result name="input">/WEB-INF/application.jsp</result> <result name="success" type="redirectAction"> <param name="actionName">browseApps</param> </result> </action> 

After upgrading to Struts 2.5, this no longer works. Attempting to invoke the /editApplication action shows error 404:

HTTP Status 404 - There are no actions for the namespace displayed for the name [/] and action name [editApplication]

I looked at the Struts 2.5 release notes and donโ€™t see any mention of updates, how wildcard matching works. Is there a reason this configuration no longer works?

+6
source share
1 answer

This is the Strict Method Invocation , and since Struts 2.5 is enabled by default.

From the documents on SMI and wildcard mappings:

When using wildcard matching in action definitions, SMI works in two ways:

  • SMI is disabled - any wildcard will be replaced with the standard RegEx, i.e.: <action name="Person*" method="perform*"> will be converted to allowedMethod = "regex:perform([A-Za-z0-9_$]*)" .
  • SMI is enabled - there will be no wildcard substitution, you must strictly determine which methods can be accessed using annotations or the <allowed-method/> .

You can disable it for <package> .

 <package strict-method-invocation="false"> 

OR you can add allowed method names for each action using the <allowed-methods> .

 <action name="*Application" class="x.ApplicationHandler" method="{1}"> <result name="input">/WEB-INF/application.jsp</result> <result name="success" type="redirectAction"> <param name="actionName">browseApps</param> </result> <allowed-methods>firstMethod, secondMethod, thirdMethod</allowed-methods> </action> 

OR add the names of allowed methods for each package using the <global-allowed-methods> .

 <package extends="struts-default"> <global-allowed-methods>firstMethod, secondMethod, thirdMethod</global-allowed-methods> </package> 

NOTE To use the above tags in struts.xml, you must update the DTD definition to 2.5 .

 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> ... </struts> 

There is also a @AllowedMethods annotation in struts2-convention-plugin that allows actions to indicate valid actions.

This annotation can be used directly for the Action classes or the package-info.java class to specify globally valid methods for all subpackages.

+14
source

All Articles