struts2.5版本struts.xml中使用通配符问题

在使用struts2.3版本前,struts.xml的配置文件中使用通配符通畅可以这么配置:

<package name="default" namespace="/" extends="struts-default">
	<action name="*_*" class="com.lonton.ssm.web.action.{1}Action" method="{2}">
		<result name="success">/{1}/welcome.jsp</result>
		<result name="input">/{1}/input.jsp</result>
	</action>
</package>

其中的 name="*_*" 中*这个符号代表的第一个值会传入class="com.lonton.ssm.web.action.{1}Action"中,第二个*代表的值会传入到method="{2}"中,并对应action类的一个方法名,这样就能很大程度地减少配置文档中action的数目。如:User_login.action路径对应访问的是com.lonton.ssm.web.action.UserAction类中的login()方法。

此配置在struts.xml中是完全正确的,但到了struts2.5的版本,会报错:

ERROR Dispatcher Could not find action or result: /ssm0602/User_login
There is no Action mapped for namespace [/] and action name [User_login] associated with context path [/ssm0602]. - [unknown location]

排错步骤:

1.检查跳转路径是否正确。

2.是否存在链接上所对应的类和方法。(注意:通配符的大小写对应问题)

3.返回字符串对应的页面。


如果上面的四个步骤没出问题,可是还是报错,怎么办?那就可能是内部属性配置的问题了

在Struts 2的核心jar包struts2-core中,有一个default.properties的默认配置文件(路径:struts-2.5.2-min\lib\org\apache\struts2\default.properties)里面配置了一些全局的信息

其中有条语句是配置动态方法调用的

struts.enable.DynamicMethodInvocation = true

当使用动态调用方法时(action名 + 感叹号 + 方法名进行方法调用),需要将其属性改为true,

当使用通配符调用语法时,建议将其属性改为false(struts2.5.2中默认是false)

当我们需要将其属性改成false时,

只在struts.xml配置文件中加入此句即可修改属性

<constant name="struts.enable.DynamicMethodInvocation" value="false" />

如果还是不能解决,则可直接尝试下面的方法:

在struts.xml文件中的action中加上:

<allowed-methods>Action内的方法名</allowed-methods>

下面给出修改后的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主配置文件 -->
<struts>

    <constant name="struts.devMode" value="true" />
    <!-- 去掉链接上的后缀 -->
    <constant name="struts.action.extension" value=","/>
	
	<package name="default" namespace="/" extends="struts-default">
		<action name="*_*" class="com.lonton.ssm.web.action.{1}Action" method="{2}">
			<result name="success">/{1}/welcome.jsp</result>
			<result name="input">/{1}/input.jsp</result>
			<allowed-methods>login</allowed-methods>
		</action>
	</package>
	
</struts>

备注:红色代码中的login为对应Action类里面的方法名,可以加上多个方法,方法名之间用逗号隔开。

 

总结:在struts2.3之前的版本,正常的配置就可以了,但在struts2.3版本之后,使用通配符调用方法时,内部会验证是否允许访问该方法,所以要加上

<allowed-methods>方法名1,方法名2…</allowed-methods>代码。

原文链接:http://www.cnblogs.com/gsy52300/p/5778754.html ,感谢这位大大神的解疑。



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值