更新到 Struts 2.5 后,通配符动作映射不再有效 [英] Wildcard Action Mapping no longer working after updating to Struts 2.5

查看:20
本文介绍了更新到 Struts 2.5 后,通配符动作映射不再有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序的 struts.xml 中有以下动作映射,它在 Struts 2.3.28.1 中工作得很好;调用由 x.ApplicationHandler.edit 方法处理的 /editApplication 操作.

I have the following action mapping in my application's struts.xml, which was working just fine with Struts 2.3.28.1; calls to the /editApplication action where being handled by the x.ApplicationHandler.edit method.

<action name="*Application" class="x.ApplicationHandler" method="{1}">
    <result name="input">/WEB-INF/application.jsp</result>
    <result name="success" type="redirectAction">
        <param name="actionName">browseApps</param>
    </result>
</action>   

升级到 Struts 2.5 后,这不再有效.尝试调用 /editApplication 操作显示 404 错误:

After upgrading to Struts 2.5, this no longer works. Attempting to call the /editApplication action shows the 404 error:

HTTP 状态 404 - 没有为命名空间 [/] 和操作名称 [editApplication] 映射的操作

HTTP Status 404 - There is no Action mapped for namespace [/] and action name [editApplication]

我已经查看了 Struts 2.5 发行说明,但没有看到任何提及基于通配符的操作映射工作方式的更新.此配置不再有效有什么原因吗?

I've reviewed the Struts 2.5 release notes, and don't see any mention of updates to the way wildcard based action mapping works. Is there any reason why this configuration no longer works?

推荐答案

严格方法调用,并且从 Struts 2.5 开始,它默认启用.

It is Strict Method Invocation and since Struts 2.5 it is enabled by default.

来自关于 SMI 和通配符映射的文档:

From the docs about SMI and wildcard mappings:

在动作定义中使用通配符映射时,SMI 有两种工作方式:

When using wildcard mapping in actions' definitions SMI works in two ways:

  • SMI 已禁用 - 任何通配符都将替换为默认的 RegEx,即:<action name="Person*" method="perform*"> 将被转换为 allowedMethod = "regex:perform([A-Za-z0-9_$]*)".
  • SMI 已启用 - 不会发生通配符替换,您必须严格定义注释或 标记可以访问哪些方法.
  • SMI is disabled - any wildcard will be substituted with the default RegEx, ie.: <action name="Person*" method="perform*"> will be translated into allowedMethod = "regex:perform([A-Za-z0-9_$]*)".
  • SMI is enabled - no wildcard substitution will happen, you must strictly define which methods can be accessed by annotations or <allowed-method/> tag.

您可以按 禁用它.

<package strict-method-invocation="false">

或者您可以使用 标签为每个操作添加允许的方法名称.

OR you can add allowed methods names per action using <allowed-methods> tag.

<action name="*Application" class="x.ApplicationHandler" method="{1}">
    <result name="input">/WEB-INF/application.jsp</result>
    <result name="success" type="redirectAction">
        <param name="actionName">browseApps</param>
    </result>

    <allowed-methods>firstMethod, secondMethod, thirdMethod</allowed-methods>
</action>

或使用 标签为每个包添加允许的方法名称.

OR add allowed methods names per package using <global-allowed-methods> tag.

<package extends="struts-default">

    <global-allowed-methods>firstMethod, secondMethod, thirdMethod</global-allowed-methods>

</package>

注意 为了在 struts.xml 中使用上述标签,您必须将 DTD 定义更新为 2.5.

NOTE In order to use above tags in struts.xml you must update DTD definition to 2.5.

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

struts2-convention- 中还有 @AllowedMethods 注释插件 允许动作指定允许的动作方法.

There is also @AllowedMethods annotation in struts2-convention-plugin which allows actions to specify allowed action methods.

这个注解可以直接用在 Action 类或 package-info.java 类中,以便为所有子包指定全局允许的方法.

This annotation can be used directly on Action classes or in the package-info.java class in order to specify global allowed methods for all sub-packages.

这篇关于更新到 Struts 2.5 后,通配符动作映射不再有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆