2016-11-17 87 views
3

我是Struts2的初学者,我知道这个问题在这里被问了很多次,但我试过要解决这个问题,并在这里阅读很多线索,花费6个小时,仍然无法完成工作。真的需要更多的建议...Struts2没有Action映射的命名空间[/]和动作名称[登录]与上下文路径[/ Struts2Test]相关联

这里是我的包

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结果。

现在我不知道,有什么建议或提示,欢迎...

+0

你为什么用Spring MVC的混合Struts2的?尝试从web.xml中删除任何Spring MVC引用,也从struts2过滤器中删除actionPackages参数,并注释Spring objectFactory设置。另外请注意,你需要struts2-spring-plugin使用spring作为objectFactory,而SpringMVC与它无关。 –

+0

因为这是我的公司做到的,所以它是一个多年来存在的结构。我需要尝试一些关于错误修复/新功能的东西,全部基于这种结构。 – RRTW

+1

那么,你最好的办法就是看看你的配置和基于这个结构的工作项目配置(同一部分)之间的差异。特别注意到xml struts文件(我看到你有两个,你只发布了一个)。但在这个单调乏味的工作之前,你仍然可以尝试(如果不存在的话,可以添加它)[** config-browser-plugin **](https://struts.apache.org/docs/config-browser-plugin.html )并寻找'http://127.0.0.1:8080/Struts2Test/config-browser/actionNames.action':它会告诉你到底是什么被映射到你的整个webapp中。值得一试; –

回答

2

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

如果你使用url来调用一个动作,确保这个url映射到struts配置。要解决URL映射问题,您可以使用config-browser插件。只需将此插件添加到项目依赖关系中,并在部署时即可访问显示运行时配置的网站,并使用可用的URL来调用该操作。例如,如果你是在本地端口8080上的context运行Tomcat和部署应用程序,那么你就可以

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

访问插件的网址,您可以点击任何操作可用的操作页面上的命名空间下侧边栏。另请注意,如果您的操作未在包装上找到,它可能位于default包装中。 Struts会在default命名空间中为不在URL中映射的名称空间上的操作执行其他搜索。


退房的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是 使用没有映射运行时配置中的任何动作配置。如果您的 无法识别您应该使用哪个网址,则可以尝试使用 config-browser插件查看可供使用的网址列表。


+0

它帮助我了解我的真实行动路径,谢谢。现在我又有一个404问题...... – RRTW

相关问题