2016-05-13 46 views
6

我在我的應用程序的struts.xml中有以下操作映射,它在Struts 2.3.28.1中工作得很好;調用/editApplication操作,其中由x.ApplicationHandler.edit方法處理。更新到Struts 2.5後,通配符操作映射不再起作用

<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> 

升級到Struts 2.5後,這不再起作用。試圖調用/editApplication動作顯示404錯誤:

HTTP Status 404 - There is no Action mapped for namespace [/] and action name [editApplication]

我查看了Struts 2.5發行說明,並沒有看到更新的方式通配符根據動作映射作品的任何提及。這個配置不再有效嗎?

+2

這是因爲SMI,如Aleksandr已經解釋過的,它的確在不允許的情況下拋出一個異常,並拋出缺少方法。我想知道用「這種方法是不允許的」拋出異常會更好嗎,wdyt? –

+0

是的,更有意義的例外在這裏可能非常有用。 – john

+1

我註冊了一個問題以涵蓋此https://issues.apache.org/jira/browse/WW-4640 –

回答

14

它是Strict Method Invocation並且自從Struts 2.5默認啓用它。

從約SMI和通配符映射文檔:

When using wildcard mapping in actions' definitions SMI works in two ways:

  • SMI is disabled - any wildcard will be substituted with the default RegEx, ie.: <action name="Person*" method="perform*"> will be translated into allowedMethod = "regex:perform([A-Za-z0-9_$]*)" .
  • SMI is enabled - no wildcard substitution will happen, you must strictly define which methods can be accessed by annotations or <allowed-method/> tag.

,您可以在每<package>禁用。

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

,也可以使用<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> 

或者使用<global-allowed-methods>標記爲每個包添加允許的方法名稱。

<package extends="struts-default"> 

    <global-allowed-methods>firstMethod, secondMethod, thirdMethod</global-allowed-methods> 

</package> 

注意爲了使用上述標籤的struts.xml必須更新DTD定義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> 

還有@AllowedMethods標註在struts2-convention-plugin允許行動,以指定允許的操作方法。

This annotation can be used directly on Action classes or in the package-info.java class in order to specify global allowed methods for all sub-packages.