Struts 2.5 配置动态调用方法

Struts 2动态调用使用通配符和感叹号两种方法,感叹号官方不建议使用,所以想用通配符的方法,在2.5之前的版本都是直接配置即可:

<?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>
 	<constant name="struts.i18n.encoding" value="UTF-8"></constant>
 	<package name="demo" extends="struts-default">
 		<global-allowed-methods>regex:.*</global-allowed-methods>
 		<action name="*Action" class="com.ymk.action.userAction" method="{1}">
 			<result name="success">/update.jsp</result>
 			<result name="error">/index.jsp</result>
 		</action>
 	</package>
 </struts>
当上配置在Struts 2.5 的环境下运行的时候,会出现异常:

HTTP Status 404 - There is no Action mapped for namespace [/] and action name [user_add] associated with context path [/struts].

网上很多教程说加上 常量配置<constant name="struts.enable.DynamicMethodInvocation" value="true" />即可,因为默认值为false,但试了还是错误。

正确的解决方法是在配置文件中加上<global-allowed-methods>regex:.*</global-allowed-methods>

<?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="Lyu" extends="struts-default" >
    <global-allowed-methods>regex:.*</global-allowed-methods>
        <action name="user_*" class="com.ymk.action.testAction" method="{1}">
            <result name="success">/index.jsp</result>
        </action>
    </package>
</struts>
另:2.5的jar包将xwork-core包合并到了struts-core下,因此配置过滤器时的路径改为 org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值