1. 程式人生 > >struts2 <action>無法使用萬用字元解決辦法

struts2 <action>無法使用萬用字元解決辦法

在我們使用struts2的時候,肯定不想每一個方法就寫一<action>,所以通常我們會使用萬用字元,簡化我們的程式碼。

 <action name="userAction_*" class="userAction" method="{1}" >
            <result name="login">/login.jsp</result>
            <result name="home">/index.jsp</result>
        </action>



但自己發現執行的時候,前端會報錯,說找不到action

Struts Problem Report 
Struts has detected an unhandled exception:

Messages: There is no Action mapped for namespace [/] and action nam [userAction_login] associated with context path [/bos-web].

       但在網上找,發現是因為版本的問題,因為我使用的是struts2 2.5,搜尋後發現在struts2.3版本之後需要在action標籤加上這行程式碼<allowed-methods>方法1,方法2…</allowed-methods>才能使用萬用字元。

  <action name="userAction_*" class="userAction" method="{1}" >
            <result name="login">/login.jsp</result>
            <result name="home">/index.jsp</result>
            <allowed-methods>login</allowed-methods>


        </action>