【问题标题】:The requested resource (/ProjectName/) is not available in Struts 2请求的资源 (/ProjectName/) 在 Struts 2 中不可用
【发布时间】:2014-09-11 20:20:35
【问题描述】:

我创建了一个 Web 动态项目 Struts2Starter,对于 Target 运行时,Web 容器是 Apache Tomcat 7。

当我运行这个项目时,它给出了一个错误:

The requested resource(/Struts2Starter/) is not available

这是路径:

Webb 文件夹元素/文件/内容已部署并在 Deployed Resources 文件夹中:

代码 sn-ps:

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" extends="default">
<action name="getTutorial" class="TutorialAction">
<result name="success">/success.jsp</result>
<result name="failure">/error.jsp</result>

</action>
</package>


</struts>

web.xml:

 <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
      <display-name>Struts2Starter</display-name>
      
     
      <filter>
      <filter-name>struts2</filter-name>
      <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
      </filter>
      
      <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>/*</url-pattern>
      </filter-mapping>
      
    </web-app>

error.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Error Page!
</body>
</html>

success.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Success Page!
</body>
</html>

但是,当我运行其他 Web 项目时,它们运行良好。我在web.xml 中提供了欢迎文件。我后来删除了它,因为我不需要它。

我试过跑步:

http://localhost:8083/Struts2Starter/
http://localhost:8083/Struts2Starter/TutorialAction
http://localhost:8083/Struts2Starter/TutorialAction.action

由于编译器找不到可用的项目,所有错误都相同。

项目已建成。端口是 8083 而不是 8080,因为 8080 已经用于某些服务。但是,正如我之前提到的,其他 Web 项目在 Apache 7 上运行良好,使用 8083。在项目的部署程序集中,我已将我的用户库“Struts2”(如上面的项目资源管理器图像所示)映射到部署路径“WEB-INF /lib"。

我无法弄清楚为什么项目没有运行,有什么建议吗? 我已经提到了其他有类似问题的线程,但其中大多数是针对 servlet 的,我没有做任何 servlet 映射,而是使用过滤器。

错误快照:

【问题讨论】:

  • 文件夹webb 在您的部署程序集中吗?
  • @Yogi 是的,我的 webb 文件夹中的所有内容都在 Deployed Resorces 文件夹中。我将在主线程中添加其项目结构的快照。
  • 我尝试将 struts.xml DOCTYPE 更改为:ttp://struts.apache.org/dtds/struts-2.3.dtd" rel="nofollow" target="_blank">struts.apache.org/dtds/struts-2.3.dtd"> 它仍然不起作用。

标签: jsp struts2 http-status-code-404 struts2-namespace


【解决方案1】:

你用过

http://localhost:8083/Struts2Starter/
http://localhost:8083/Struts2Starter/TutorialAction
http://localhost:8083/Struts2Starter/TutorialAction.action

但它们都没有映射到动作。 struts.xml 应该修改和更改为正确的 DTD。

<?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>
 <package name="default" extends="struts-default">
  <action name="getTutorial" class="TutorialAction">
    <result name="success">/success.jsp</result>
    <result name="failure">/error.jsp</result>
  </action>
</package>

试试这个网址

http://localhost:8083/Struts2Starter/getTutorial
http://localhost:8083/Struts2Starter/getTutorial.action

还将所有必要的 jar 下载到应用程序的 lib 文件夹中。例如commons-lang3-3.3.2.jar

【讨论】:

  • 嗨@Roman C 是的,你是对的,我的 URL 本身是错误的。我将所有必需的 JAR 添加到 lib 文件夹中。然后给出正确的网址。但它不工作。关于我在哪里错过关键点的任何建议。?谢谢。
  • 您可以执行该操作吗?另外,您为什么不配置项目的 Web 根文件夹?这是什么webb.xml 你最后的编辑?
  • 嗨@Roman C webb是内容目录文件夹,我存储所有Web文件(核心java中的核心业务逻辑不在这里)。另一方面,web.xml 用于配置。在一个普通的 web 动态项目中,应该使用 JSP 或 URL 映射 servlet,但由于这是 struts 项目,我不需要 servlet,并且该文件用于过滤器。我在主线程中添加了一个快照,显示 webb 文件夹文件位于已部署的资源中。 (主线程中的第二个项目资源管理器图像)。谢谢。
  • 在文件部署到服务器之前,这个文件夹是无用的,如果你把你的 java 文件放在 web-if 下。如何在 Eclipse 中配置项目对于在具有已定义名称的 Web 部署描述符上运行的服务器来说毫无意义。除非您使用重新发送的 servlet 技术,否则您也无法在没有它的情况下运行您的项目。部署项目时,您可以通过使用 struts 配置指定映射到操作的 url 来访问操作。不正确的映射会导致404状态码,这个是tomcat用来处理的默认servlet产生的。
  • 嗨@Roman C,感谢您抽出宝贵时间。我不知道如何在服务器上显式部署我的文件。我以创建任何其他正在运行的 Web 应用程序的确切方式创建了项目及其文件。如何明确说明这一点?为什么我要把我的核心 java 文件放在 WEB/INF 文件夹中。在打电话时,我希望我是准确的。非常感谢您的时间和耐心。
【解决方案2】:

尝试在您的 struts 配置中进行以下操作

<?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" extends="default" namespace="/">

        <action name=""> <!-- or you can include index.jsp file in web.xml -->
            <result>/index.jsp</result>
        </action>

        <action name="getTutorial" class="TutorialAction">
            <result name="success">/success.jsp</result>
            <result name="failure">/error.jsp</result>
        </action>

    </package>
</struts>

如果您不提供命名空间默认命名空间是“”一个空字符串。这里是documentation

【讨论】:

  • 嗨,Yogi,我尝试了你的建议。也创建了 index.jsp 并如上所示使用。给了命名空间。但它不工作..还有什么建议吗?谢谢你的时间。
猜你喜欢
  • 1970-01-01
  • 2014-06-09
  • 2021-01-25
  • 2015-02-05
  • 2011-12-09
  • 2014-07-01
  • 1970-01-01
  • 2015-05-26
相关资源
最近更新 更多