Struts2中关于"There is no Action mapped for namespace / and action name"的总结 今天在调试一个基础的Struts2框架小程

本文总结了在使用Struts2框架时遇到的There is no Action mapped for namespace and action name错误的原因和解决方案,包括struts.xml文件错误和web.xml文件配置问题。

Struts2中关于"There is no Action mapped for namespace / and action name"的总结

      今天在调试一个基础的Struts2框架小程序。总是提示"There is no Action mapped for namespace / and action name"的错误。上网查询后发现这是一个初学者经常碰到的问题,导致错误的原因主要有两种。总结如下:

      一、struts.xml文件错误。这种错误又分为以下几种:1,struts.xml文件名错误。一定要注意拼写问题;2,struts.xml文件放置路径错误。一定要将此文件放置在src目录下。编译成功后,要确认是否编译到classes目录中;3,struts.xml文件内容错误。下面给出一个正确的struts.xml文件以供参考。注意背景色部分。

复制代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"
>


<struts>
<package name="default" namespace="/" extends="struts-default">
<action name="login" class="com.wanggc.struts2.sample.Struts2Action">
<result name="success">/jsp/result.jsp</result>
</action>
</package>
</struts>
复制代码

 

      二、如果排除了struts.xml文件的问题,还有一种可能就是,在web.xml文件中的<welcome-file>信息中是否配置了自己工程的启动页面。如果没有配置,地址栏中要输入完成的url,如:http://localhost:8080/Struts2Sample/jsp/login.jsp,后面的资源详细信息不输入的时候也会报这个错误,我就是栽在这个问题上的(^_^)。


Struts2 中,当出现 `There is no Action mapped for namespace /pay and action name payVoucher` 的错误信息时,通常表示框架无法找到与请求的命名空间和动作名称相对应的 Action 类。这个问题可能由多个原因引起,包括配置错误、命名空间不匹配、Action 名称拼写错误或类路径问题等。 ### 可能的原因及解决方案 #### 1. 检查 `struts.xml` 配置文件中的命名空间和 Action 映射 确保在 `struts.xml` 文件中为 `/pay` 命名空间定义了对应的包,并且其中包含名为 `payVoucher` 的 Action。例如: ```xml <package name="pay" namespace="/pay" extends="struts-default"> <action name="payVoucher" class="com.example.PayVoucherAction"> <result name="success">/success.jsp</result> </action> </package> ``` 如果命名空间或 Action 名称拼写错误,则会导致找不到对应 Action 的问题[^1]。 #### 2. 确保 Action 类存在并正确实现 检查 `PayVoucherAction` 是否存在于指定的包中,并且继承了 `ActionSupport` 或实现了 `Action` 接口。此外,确保该类具有默认构造函数,并且方法(如 `execute()`)被正确覆盖。 ```java package com.example; import com.opensymphony.xwork2.ActionSupport; public class PayVoucherAction extends ActionSupport { public String execute() { return SUCCESS; } } ``` 如果类未正确实现或不存在,则会引发映射失败的问题。 #### 3. 检查 URL 请求是否正确 确保访问的 URL 正确无误。例如,若应用上下文为 `myapp`,则正确的请求应为: ``` http://localhost:8080/myapp/pay/payVoucher.action ``` 如果 URL 中的命名空间或 Action 名称与配置不符,则 Struts2 无法找到对应的 Action[^1]。 #### 4. 启用 Struts2 的开发模式以获取详细日志 在 `struts.xml` 中启用开发模式,以便获得更详细的调试信息: ```xml <constant name="struts.devMode" value="true"/> ``` 这将帮助识别配置文件加载问题或 Action 映射错误。 #### 5. 检查插件依赖和配置 确保所有必要的 Struts2 插件已正确引入项目。例如,如果使用了 Spring 集成,则需添加以下依赖: ```xml <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-spring-plugin</artifactId> <version>2.5.20</version> </dependency> ``` 缺少必要插件可能导致 Action 无法正确初始化或映射[^1]。 #### 6. 检查 web.xml 配置 确保 `web.xml` 中正确配置了 Struts2 的过滤器: ```xml <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>/*</url-pattern> </filter-mapping> ``` 如果过滤器未正确配置,Struts2 将无法拦截请求并处理 Action 映射[^1]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值