struts2 2.5.16 通配符方式调用action中的方法报404

本文详细解析了在Struts2 2.5版本中使用通配符调用Action方法遇到的404错误。通过修改struts.xml配置文件中的package属性,设置strict-method-invocation为false,成功解决了访问特定URL时出现的未找到Action映射的问题。

1、问题描述

在struts.xml中配置用通配符方式调用action中的add()方法,访问 http://localhost:8080/Struts2Demo/helloworld_add.action时,出现了这个熟悉的错误:

HTTP Status 404 – Not Found


Type Status Report

Message There is no Action mapped for namespace [/] and action name [helloworld_add] associated with context path [/Struts2Demo].

Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.


Apache Tomcat/8.5.35

 

2、相关代码

struts.xml中部分相关代码

<package name="default" namespace="/" extends="struts-default">
        <action name="helloworld_*" method="{1}" class="com.icheny.action.HelloWorldAction">
            <result>/result.jsp</result>
            <result name="add">/{1}.jsp</result>
            <result name="update">/{1}.jsp</result>
        </action>
</package>

HelloWorldAction.java中部分相关代码

    public String add()
    {
        return "add";
    }

    public String update()
    {
        return "update";
    }

 

3、原因

  由于struts2 2.5版本的安全度提高了一个台阶,通配符禁止使用,所有不安全的访问都必须要在struts.xml中声明允许。我用的是2.5.16版本的struts,因此出现了这个错误。

 

4、解决方案

  在package中添加属性:strict-method-invocation="false"

  修改之后的struts.xml中的package部分代码为:

<package name="default" namespace="/" extends="struts-default" strict-method-invocation="false">
        <action name="helloworld_*" method="{1}" class="com.icheny.action.HelloWorldAction">
            <result>/result.jsp</result>
            <result name="add">/{1}.jsp</result>
            <result name="update">/{1}.jsp</result>
        </action>
</package>

  再次访问http://localhost:8080/Struts2Demo/helloworld_add.action,正常跳转:

 

          

 

转载于:https://www.cnblogs.com/iCheny/p/10963050.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值