struts2.5 使用感叹号和通配符实现动态方法调用无效的问题及解决!
2016-09-18 18:28
706 查看
先说 struts2.5 使用感叹号实现动态方法调用的问题,使用通配符的方式出现的问题跟这一样!
在 struts2.3 的版本中,我们通常这么配置 struts.xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="default" namespace="/" extends="struts-default">
<result>/result.jsp</result>
<result name="add">/add.jsp</result>
<result name="update">/update.jsp</result>
</action>
</package>
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
</struts>
HelloWorldAction类如下:
package com.imooc.action;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorldAction extends ActionSupport {
public String add(){
return "add";
}
public String update(){
return "update";
}
@Override
public String execute() throws Exception {
System.out.println("执行Action");
return SUCCESS;
}
}
这样在 struts2.3 上能够成功,但在 struts2.5 上却无论如何定位不到 http://localhost:8888/HelloWorld/helloworld!add.action,会发生如下错误:
message Method add for action helloworld is not allowed!
description The requested resource is not available.
原来是 struts2.5 为了增加安全性,在 struts.xml 添加了这么个属性:<global-allowed-methods>regex:.*</global-allowed-methods>
同时要注意,struts.xml 的解析版本要为 2.5,即头部信息应为:
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
最后,struts.xml 应该为:
<?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="default" namespace="/" extends="struts-default">
<global-allowed-methods>regex:.*</global-allowed-methods>
<action name="helloworld" class="com.imooc.action.HelloWorldAction">
<result>/result.jsp</result>
<result name="add">/add.jsp</result>
<result name="update">/update.jsp</result>
</action>
</package>
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
</struts>
同样,使用通配符实现动态方法调用时,
<?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="default" namespace="/" extends="struts-default">
<global-allowed-methods>regex:.*</global-allowed-methods>
<action name="helloworld_*" method="{1}" class="com.imooc.action.HelloWorldAction">
<result>/result.jsp</result>
<result name="add">/{1}.jsp</result>
<result name="update">/update.jsp</result>
</action>
</package>
<constant name="struts.enable.DynamicMethodInvocation" value="false"></constant>
</struts>
在 struts2.3 的版本中,我们通常这么配置 struts.xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="default" namespace="/" extends="struts-default">
<result>/result.jsp</result>
<result name="add">/add.jsp</result>
<result name="update">/update.jsp</result>
</action>
</package>
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
</struts>
HelloWorldAction类如下:
package com.imooc.action;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorldAction extends ActionSupport {
public String add(){
return "add";
}
public String update(){
return "update";
}
@Override
public String execute() throws Exception {
System.out.println("执行Action");
return SUCCESS;
}
}
这样在 struts2.3 上能够成功,但在 struts2.5 上却无论如何定位不到 http://localhost:8888/HelloWorld/helloworld!add.action,会发生如下错误:
HTTP Status 404 - Method add for action helloworld is not allowed!
type Status reportmessage Method add for action helloworld is not allowed!
description The requested resource is not available.
Apache Tomcat/7.0.70
搜了半天,有说是要添加 <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>,明确打开动态方法调用功能,因为 struts2 默认是关的,但在这里并不能解决问题,因为配置是正确的。原来是 struts2.5 为了增加安全性,在 struts.xml 添加了这么个属性:<global-allowed-methods>regex:.*</global-allowed-methods>
同时要注意,struts.xml 的解析版本要为 2.5,即头部信息应为:
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
最后,struts.xml 应该为:
<?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="default" namespace="/" extends="struts-default">
<global-allowed-methods>regex:.*</global-allowed-methods>
<action name="helloworld" class="com.imooc.action.HelloWorldAction">
<result>/result.jsp</result>
<result name="add">/add.jsp</result>
<result name="update">/update.jsp</result>
</action>
</package>
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
</struts>
同样,使用通配符实现动态方法调用时,
<?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="default" namespace="/" extends="struts-default">
<global-allowed-methods>regex:.*</global-allowed-methods>
<action name="helloworld_*" method="{1}" class="com.imooc.action.HelloWorldAction">
<result>/result.jsp</result>
<result name="add">/{1}.jsp</result>
<result name="update">/update.jsp</result>
</action>
</package>
<constant name="struts.enable.DynamicMethodInvocation" value="false"></constant>
</struts>
相关文章推荐
- struts2.5 使用感叹号和通配符实现动态方法调用无效的问题及解决!
- Struts2.5使用感叹号实现动态方法调用无效的解决办法
- Struts2.5使用通配符的方式实现动态方法调用报错
- Struts2.5使用通配符的方式实现动态方法调用报错
- 解决Struts2.5不能使用通配符方式动态方法调用
- [Struts2 框架学习] Struts2.5 通配符动态方法调用问题
- Struts2.5动态方法调用action和使用通配符访问action
- struts2.5感叹号和通配符实现动态方法
- 升级Struts2.5后使用DMI动态方法调用遇到问题
- struts2.5动态方法调用不能使用问题
- 升级Struts2.5后使用DMI动态方法调用遇到问题
- 动态方法调用和使用通配符实现类似struts1.x的dispatchAction功能
- struts2.5的method使用通配符无效问题
- 编写SqlHelper使用,在将ExecuteReader方法封装进而读取数据库中的数据时会产生Additional information: 阅读器关闭时尝试调用 Read 无效问题,解决方法与解释
- Messages:This method: edit for action XXX is not allowed!-Struts2.5动态方法调用错误解决方法
- struts中动态方法调用使用通配符
- struts--动态方法调用和使用通配符(8)
- struts中3种情况通配符的使用方法 及 动态方法调用
- 关于struts2.5以后 遇到的几点改变以及动态调用方法问题
- struts2.5动态方法调用失败的解决