1. 程式人生 > >struts 找不到action 的可能原因

struts 找不到action 的可能原因

struts

struts版本2.5.13,配置如下


<?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>
    <package name="app" namespace="/" extends="struts-default">
        <action name="userAction_*" class="web.actions.UserAction" method="{1}">
            <result>/success.jsp</result>
        </action>
    </package>

</struts>


訪問 http://localhost/userAction_add 提示無法找action


HTTP Status 404 - There is no Action mapped for namespace [/] and action name [userAction_add] associated with context path [].
type Status report
message There is no Action mapped for namespace [/] and action name [userAction_add] associated with context path [].
description The requested resource is not available.
Apache Tomcat/7.0.82


struts為了安全,限制了Action被調用的方法,如下配置可正常訪問:


<?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>
    <package name="app" namespace="/" extends="struts-default">
        <global-allowed-methods>regex:.*</global-allowed-methods> <!-- 方法一 -->
        <action name="userAction_*" class="web.actions.UserAction" method="{1}">
            <result>/success.jsp</result>
            <allowed-methods>regex:.*</allowed-methods> <!-- 方法二 -->
        </action>
    </package>

</struts>
標簽<global-allowed-methods>和<allowed-methods>中可是使用regex:開頭的正則表達式,來表示允許被執行的Action方法,regex:.*表示任何方法都可以被調用;
也可以將允許被調用的方法名放入其中,多個方法用逗號隔開,如:add,delete

本文出自 “田書培” 博客,請務必保留此出處http://tianshupei.blog.51cto.com/1105157/1976527

struts 找不到action 的可能原因