本文转载:原文 http://blog.csdn.net/qq_29407009/article/details/51474118
-
struts2:This method: login for action user is not allowed!
在是用struts2 2.5版本的包的时候,出现一个很恶心的问题,就是使用动态方法的时候,报这样一个问题:This method: login for action user is not allowed!
出现这样一个原因是什么了?
点击这样报错的地方,进入源码查看源码,这是报错的地方的源码:
我们看到的是这个方法:if (config.isAllowedMethod(method)) { invocation.init(this); } else { throw new ConfigurationException("This method: " + method + " for action " + actionName + " is not allowed!"); }
- 1
- 2
- 3
- 4
- 5
肯定是这个方法(config.isAllowedMethod(method))返回的是false,才回报错,进入这个方法,
看到这个方法的处理方法,method.equals(methodName != null ? methodName : DEFAULT_METHOD) || allowedMethods.isAllowed(method)”.其中这个是:DEFAULT_METHOD:execute
解释一下://1.methodName != null ? methodName : DEFAULT_METHOD 如果methodName 不等于空,就是返回methodName ,等于空就是DEFAULT_METHOD (execute) //2.method.equals()或allowedMethods.isAllowed(method),因为我们知道我们的处理方法是login,所以肯定是allowedMethods.isAllowed(method)这里出问题了! return method.equals(methodName != null ? methodName : DEFAULT_METHOD) || allowedMethods.isAllowed(method)
- 1
- 2
- 3
这是我断点看到的:
然后我们进入到allowedMethods.isAllowed(method)这个方法中//allowedMethods:这是集合是读取struts的配置文件得到的,但是我在使用2.5版本之前是没有的,我看断点调式的时候看到这个集合中的内容就是struts2包中struts-default.xml在最后面有这样一句话 <global-allowed-methods>execute,input,back,cancel,browse,save,delete,list,index</global-allowed-methods> public boolean isAllowed(String method) { for (AllowedMethod allowedMethod : allowedMethods) { if (allowedMethod.isAllowed(method)) { return true; } } return false; }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
看到这里我们大概就知道了解决方法了: