struts2.5 使用感叹号和通配符实现动态方法调用无效的问题及解决!

先说 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>

这样在 struts2.3 上能够成功,但在 struts2.5 上却无论如何定位不到 http://localhost:8888/HelloWorld/helloworld!add.action,会发生如下错误:

HTTP Status 404 - Method add for action helloworld is not allowed!
type Status report

message 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>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值