intellij idea 使用struts2访问action时出现404错误

在使用IntelliJ IDEA进行Struts2开发时,遇到访问Action时返回404错误的问题。检查了struts.xml配置文件及web.xml配置,但问题依然存在。本文将探讨如何排查并解决这个问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

错误如图,

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 2配置文件的根元素 -->
<struts>
<!-- 指定全局国际化资源文件 -->
<constant name="struts.custom.i18n.resources" value="mess"/>
<!-- 指定国际化编码所使用的字符集 -->
<constant name="struts.i18n.encoding" value="GBK"/>
<!--指定constant的name为devMode表示处于开发模式,value=true修改配置文件内容自动热替换而不需要每次重新发布-->
<constant name="struts.devMode" value="true"/>
<!-- 所有的Action定义都应该放在package下,Struts2中通过package来管理action,这里的包名name并不对应java类中的包 -->
<package name="hello" extends="struts-default" >

    <action name="HelloWordAction" class="Action.hello.HelloWordAction" >
        <!-- 定义三个逻辑视图和物理资源之间的映射 -->
        <result name="input">/login.jsp</result>
        <result name="error">/error.jsp</result>
        <result name="success">/success.jsp</result>
    </action>
</package>
</struts>
后来发现是web.xml的配置有错,过滤器把action拦截了,在filter标签中添加<url-pattern>*.action</url-pattern>后问题解决。

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
        </filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>*.action</url-pattern>
    </filter-mapping>
</web-app>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值