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
原文:http://tianshupei.blog.51cto.com/1105157/1976527