我是Struts2的初学者,我知道这里经常问这个问题,但是我试图解决这个问题并在这里阅读了很多线程,花了6个小时仍然无法正常工作。确实需要更多建议…
这是我的包裹
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,启动时没有错误。但是当我访问
它给了我这个(作为标题)错误,如果我访问
我会得到常规的404结果。
现在我不知道,欢迎任何建议或提示…
与以下问题有关的问题:没有为与上下文路径关联的名称空间和操作名称映射任何操作
如果使用url调用动作,请确保此url映射到struts配置。要解决网址映射问题,可以使用config- browser插件。只需将此插件添加到您的项目依赖项中,并在部署它后,就可以访问一个显示运行时配置的网站,并提供可用的URL来调用操作。例如,如果您在本地8080端口上运行Tomcat,并在上部署了应用程序context,则可以使用以下命令访问插件的url
context
http://localhost:8080/[context]/config-browser/index.action
您可以在侧边栏名称空间下的操作页面上单击任何可用的操作。另请注意,如果在包装上找不到您的操作,则可能在default包装中。Struts在default名称空间中进行其他搜索,以查找不在URL映射的名称空间中的操作。
default
查看config-browser插件以调试配置问题。 要将URL正确映射到操作,需要两个参数:操作名称和名称空间。 Struts在启动时会加载xml配置,并且应该知道的位置struts.xml。默认情况下,它会在classpath上查找具有类似的已知名称的文件struts.xml。然后,它解析文档并创建运行时配置。此配置用于为操作URL查找适当的配置元素。如果在请求期间未找到此类元素,则返回404错误代码,并显示以下消息:There is no Action mapped for namespace and action name。 此外,此消息还包含用于查找操作配置的名称空间和操作名称。然后,您可以在中检查配置设置 struts.xml。有时,存储的操作名称和名称空间 ActionMapping指向错误的操作。这些值ActionMapper由插件所使用的实现方式可能有所不同。 还有另一个设置可能会影响此映射器和映射,但是要点是相同的,如果您收到此消息,则使用URL不会在运行时配置中映射任何操作配置。如果您不知道应该使用哪个URL,则可以尝试使用 config-browser插件查看可用的URL列表。
查看config-browser插件以调试配置问题。
要将URL正确映射到操作,需要两个参数:操作名称和名称空间。
Struts在启动时会加载xml配置,并且应该知道的位置struts.xml。默认情况下,它会在classpath上查找具有类似的已知名称的文件struts.xml。然后,它解析文档并创建运行时配置。此配置用于为操作URL查找适当的配置元素。如果在请求期间未找到此类元素,则返回404错误代码,并显示以下消息:There is no Action mapped for namespace and action name。
struts.xml
There is no Action mapped for namespace and action name
此外,此消息还包含用于查找操作配置的名称空间和操作名称。然后,您可以在中检查配置设置 struts.xml。有时,存储的操作名称和名称空间 ActionMapping指向错误的操作。这些值ActionMapper由插件所使用的实现方式可能有所不同。
ActionMapping
ActionMapper
还有另一个设置可能会影响此映射器和映射,但是要点是相同的,如果您收到此消息,则使用URL不会在运行时配置中映射任何操作配置。如果您不知道应该使用哪个URL,则可以尝试使用 config-browser插件查看可用的URL列表。