13.there is no action mapped for namespace and action name associated with cont

在學習struts框架時常常會使用到通配符調用方法,以下:git

<struts>
    <package name="demo1" extends="struts-default" namespace="/">
        <action name="strutsDemo1_*" class="com.action.strutsDemo1" method="{1}">
            <!--<result name="success">/demo1.jsp</result>-->
            <result name="add">/add.jsp</result>
            <result name="delete">/delete.jsp</result>
            <result name="update">/update.jsp</result>
            <result name="find">/find.jsp</result>
        </action>
    </package>
    <constant name="struts.custom.i18n.resources" value="utf-8"/>
    <constant name="struts.devMode" value="false"/>
</struts>
複製代碼

其中actionname那裏表示傳入strutsDemo1_* 這個*號表示傳的值會傳入metho進行匹配但這裏會有一個問題github

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

若是看到提示的是映射問題,你能夠按照映射路線排除一遍,apache

第一步:先排查訪問的連接有沒有問題(細節問題)bash

第二步:查看struts.xml的配置(仔細排查,出現問題概率很大)app

第三步:查看相關的action類及方法(好比return的值是否是跟配置文件中的result對應得上等)框架

第四步:查看結果響應頁面是否存在問題(出現問題的概率比較小)jsp

(具體的作法我不細講了,網上不少優秀篇章都有說起,可自行百度)學習

若是上面的四個步驟沒出問題,但是仍是報錯,怎麼辦?那就多是內部屬性配置的問題了spa

在Struts 2的核心jar包struts2-core中,有一個default.properties的默認配置文件(路徑:struts-2.5.2-min\lib\org\apache\struts2\default.properties)裏面配置了一些全局的信息code

其中有條語句是配置動態方法調用的

struts.enable.DynamicMethodInvocation = true
複製代碼

當使用動態調用方法時(action名 + 感嘆號 + 方法名進行方法調用),須要將其屬性改成true,

當使用通配符調用語法時,建議將其屬性改成false(struts2.5.2中默認是false)

當咱們須要將其屬性改爲false時,

只在struts.xml配置文件中加入此句便可修改屬性

<constant name="struts.enable.DynamicMethodInvocation" value="false" />
複製代碼

不少網友都說改了以後都行了,不過我換了struts2.5以後,整了很久都仍是不行……

最後是在配置文檔struts.xml的Action中配置了

<allowed-methods>Action內的方法名</allowed-methods>才成功了
複製代碼

好比上面代碼修改以下:

<struts>
    <package name="demo1" extends="struts-default" namespace="/">
        <action name="strutsDemo1_*" class="com.action.strutsDemo1" method="{1}">
            <!--<result name="success">/demo1.jsp</result>-->
            <result name="add">/add.jsp</result>
            <result name="delete">/delete.jsp</result>
            <result name="update">/update.jsp</result>
            <result name="find">/find.jsp</result>
            <allowed-methods>add,delete,update,find</allowed-methods>
        </action>
    </package>
    <constant name="struts.custom.i18n.resources" value="utf-8"/>
    <constant name="struts.devMode" value="false"/>
    <constant name="struts.enable.DynamicMethodInvocation" value="false"/>
</struts>
複製代碼

WiHongNoteBook

相關文章
相關標籤/搜索