没有为命名空间[/]和动作名称[login]映射任何操作与上下文路径[/Struts2Test]相关联。

7

我是Struts2的初学者,我知道这个问题在这里已经被问了很多次,但是我尝试解决并阅读了很多帖子,花费了6个小时,仍然无法使它工作。真的需要更多建议...

这是我的Package:

Struts2Test
  +Struts2Test/src
    +tw.com.rrstudio.java.test
      -TestAction.java
  +Struts2Test/build
  +Struts2Test/WebContent
    +Struts2Test/WebContent/META-INF
      +Struts2Test/WebContent/WEB-INF/classes
      +Struts2Test/WebContent/WEB-INF/lib
      -Struts2Test/WebContent/WEB-INF/spring-context.xml
      -Struts2Test/WebContent/WEB-INF/spring-mvc.xml
      -Struts2Test/WebContent/WEB-INF/struts.xml
      -Struts2Test/WebContent/WEB-INF/struts2-action.xml
      -Struts2Test/WebContent/WEB-INF/web.xml
    -Struts2Test/WebContent/error.jsp
    -Struts2Test/WebContent/index.jsp
    -Struts2Test/WebContent/TestAction.jsp

我的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>Struts2Test</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
      /WEB-INF/spring-context.xml
      /WEB-INF/spring-mvc.xml
    </param-value>
  </context-param>

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    <init-param>
      <param-name>actionPackages</param-name>
      <param-value>tw.com.rrstudio.java.test</param-value>
    </init-param>
  </filter>

  <jsp-config>
    <taglib>
      <taglib-uri>HTTP://java.sun.com/jsp/jstl/core</taglib-uri>
      <taglib-location>/WEB-INF/lib/tld/c.tld</taglib-location>
    </taglib>
    <jsp-property-group>
      <url-pattern>*.jsp</url-pattern>
      <page-encoding>UTF-8</page-encoding>
    </jsp-property-group>
  </jsp-config>

  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>

  <session-config>
    <session-timeout>10</session-timeout>
  </session-config>

</web-app>

此外,struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
  <!-- struts 2.3.16.3 has problem of security,needing to set DynamicMethodInvocation=false -->
  <constant name="struts.enable.DynamicMethodInvocation" value="false" />
  <constant name="struts.devMode" value="false" />
  
  <constant name="struts.objectFactory" value="spring" />

  <constant name="struts.action.extension" value="do"/>
  <constant name="struts.action.excludePattern" value="/jsp/.*?,/image/.*?,/css/.*?,/js/.*?,.*\\.jsp"/>

  <package name="default" extends="json-default" ></package>

  <package name="Strut2Test" extends="json-default" >
    <global-results>
      <result name="SystemErrorPage">/WebContent/error.jsp</result>
    </global-results>
    <action name="login" class="tw.com.rrstudio.java.test.TestAction">
      <result name="index">/WebContent/index.jsp</result>
      <result name="success">/WebContent/TestAction.jsp</result>
    </action>
  </package>

</struts>

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<META HTTP-EQUIV="Refresh" CONTENT="0;URL=login.action">
<title>Index of Struts2Test</title>
</head>
<body>
  <h1>Index of Struts2Test</h1>
  <form action="testAction" method="post">
    <label for="name">Please enter your name</label><br/>
    <input type="text" name="name"/>
    <input type="submit" value="Say Hello"/>
  </form>
</body>
</html>

服务器版本为Tomcat 8.0.38,启动时没有报错。但是当我访问以下网址时:
  • http://127.0.0.1:8080/Struts2Test/
  • http://127.0.0.1:8080/Struts2Test/login
就会出现如下错误(作为标题),如果我访问:
  • http://127.0.0.1:8080/Struts2Test/index.jsp
我会得到一个普通的404结果。
现在我不知道该怎么办了,欢迎任何建议或提示...

为什么要将Struts2与Spring MVC混合使用?尝试从web.xml中删除任何Spring MVC引用,同时从struts2过滤器中删除actionPackages参数,并注释Spring objectFactory设置。还要注意,您需要使用struts2-spring-plugin将Spring用作objectFactory,而SpringMVC与此无关。 - Andrea Ligios
因为这是我的公司一直在使用的现有结构,所以我需要尝试一些关于错误修复/新功能的事情,都基于这个结构。 - RRTW
2
那么,你最好的尝试方式就是查看你的配置与使用此结构的工作项目(同一部分)的配置之间的区别。特别注意xml struts文件(我看到你有两个,你只发布了一个)。但在进行这项繁琐的工作之前,您仍然可以尝试(如果尚未存在,则添加它)config-browser-plugin,并查找http://127.0.0.1:8080/Struts2Test/config-browser/actionNames.action: 这将告诉您整个Web应用程序中映射到什么内容。值得一试;) - Andrea Ligios
4个回答

5

与命名空间和动作名称相关的问题:没有为上下文路径关联的动作映射

如果您使用URL调用操作,请确保将此URL映射到Struts配置中。要解决与URL映射相关的问题,您可以使用config-browser插件。只需将此插件添加到项目依赖项中,并在部署后,您就可以访问显示可用于调用操作的运行时配置的网站。例如,如果您在本地端口8080上运行Tomcat并在context上部署了应用程序,则可以使用插件的URL访问该网站。

http://localhost:8080/[context]/config-browser/index.action

您可以在侧边栏的命名空间下的操作页面上单击任何可用操作。另外请注意,如果您的操作在包中找不到,则可能在default包中。Struts会对未在URL映射的命名空间中找到的操作在default命名空间中进行额外搜索。

请查看config-browser插件以调试配置问题。
为了正确地将URL映射到操作,需要两个参数:操作名称和命名空间。
Struts在启动时加载xml配置,并应知道struts.xml的位置。默认情况下,它正在查找具有已知名称(例如struts.xml)的文件来查找类路径。然后解析文档并创建运行时配置。此配置用于查找操作URL的适当配置元素。如果在请求期间未找到此类元素,则返回404错误代码并显示消息:There is no Action mapped for namespace and action name
此消息还包含用于查找操作配置的命名空间和操作名称。然后您可以在struts.xml中检查配置设置。有时,存储在ActionMapping中的操作名称和命名空间指向错误的操作。这些值由ActionMapper确定,它可能具有不同的实现,由插件使用。
还有另一个设置可能会影响该映射器和映射,但是如果收到此消息,则在运行时配置中没有映射任何操作配置的URL相同。如果无法确定应使用哪个URL,则可以尝试config-browser插件以查看可用于使用的URL列表。

它帮助我了解我的实际操作路径,谢谢。现在我又遇到了另一个404错误...... - RRTW

0

检查 struts.xml 中的常量 struts.action.excludePattern,确保其中是否包含登录条目。在我的情况下,是排除模式导致了问题。

如果您正在为基于 Struts2 的应用程序提供 Servlet 调用,则适用此方法。


0

请查看config-browser插件以调试配置问题。

在我们的设置中,我们使用了链接:

http://localhost:8080/[context]/config-browser/index.do

在我们的情况下,我们发现在两个不同的文件夹层次结构中有冲突的struts.xml文件。所谓冲突是指它们内部的操作重叠并使用相同的名称。

0
我遇到了这个错误,因为struts.xml文件没有放在正确的位置,src文件夹是什么!('!' == 新手惊喜):
ApplicationProjectRoot
|
|__Java Resources
|  |
|  |__src
|  |  |__struts.xml
|  |  |__any.package.app
|  |     |__AnyClass.java
|  |
|  |__Libraries                
 [...]  

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接