eclipse中配置struts2出现There is no Action mapped for namespace [/] and action name [Login] associated wi

时间:2023-03-08 18:04:27

下午在eclipse中配置struts2时报:

There is no Action mapped for namespace [/] and action name [Login] associated with context path [/eprint]

错误

做如下检查:

1、确保struts.xml文件名大小写正确:struts.xml

2、确保struts.xml文件在src目录下(很重要!后面就着重说这个)

附:

web.xml文件内容

示例:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="3.0"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  6. http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  7. <display-name></display-name>
  8. <filter>
  9. <filter-name>struts2</filter-name>
  10. <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  11. </filter>
  12. <filter-mapping>
  13. <filter-name>struts2</filter-name>
  14. <url-pattern>/*</url-pattern>
  15. </filter-mapping>
  16. <welcome-file-list>
  17. <welcome-file>index.jsp</welcome-file>
  18. </welcome-file-list>
  19. </web-app>

struts.xml:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
  4. "http://struts.apache.org/dtds/struts-2.3.dtd">
  5. <struts>
  6. <package name="default" namespace="/" extends="struts-default">
  7. <action name="LoginAction" class="com.xxx.control.LoginAction">
  8. <!-- 定义逻辑视图和物理资源之间的映射 -->
  9. <result name="error">index.jsp</result>
  10. <result name="success">/WEB-INF/main.jsp</result>
  11. </action>
  12. </package>
  13. </struts>

index.jsp:

  1. <s:form action="LoginAction" namespace="/">
  2. <s:textfield name="username" label="username"></s:textfield>
  3. <s:password name="password" label="password"></s:password>
  4. <div id="login_button">
  5. <input type="submit" value="login"/>
  6. </div>
  7. <div  id="register_button">
  8. <input type="button" value="register"/>
  9. </div>
  10. </s:form>

反复确认之后,还是没找到错误

找度娘,好多种解决方案,木有一个可以解决我的问题的

最后发现是我的struts.xml是在src目录下,没有任何疑问

但是我的编译后classes文件默认却是在build目录下

不在WEB-INF下.......

这样它是找不到struts.xml文件的,报如上错误,也是情有可原的

最后,明确一点,struts.xm位于src下是为了编译后能找到struts配置文件,确保其在WEB-INF下才是根本!!!

更改eclipse web 项目默认编译输出路径:

eclipse中只能针对项目更改,因为其默认的是build目录下的,只能以项目更改: 
项目右键 -》properties -》JavaBuild Path -》source -》Default output folder,选择你的路径,ok!