简体   繁体   English

找不到资源错误404

[英]Resource not found error 404

I am trying to run a web application example on struts2 and i am facing a problem. 我正在尝试在struts2上运行Web应用程序示例,但是我遇到了问题。 Here are the codes web.xml 这是代码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>Struts 2</display-name>
       <welcome-file-list>
          <welcome-file>index.jsp</welcome-file>
       </welcome-file-list>
       <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>

index.jsp index.jsp

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
       pageEncoding="ISO-8859-1"%>
    <%@ 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>
    <title>Hello World</title>
    </head>
    <body>
       <h1>Hello World From Struts2</h1>
       <form action="abc">

      <label for="name">Please enter your name</label><br/>
      <input type="text" name="name"/>
      <input type="submit" value="Say Hello"/>
   </form>
</body>
</html>

struts.xml 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>
<constant name="struts.devMode" value="true" />
   <package name="helloworld" extends="struts-default namespace="/">

      <action name="abc" 
            class="com.junaid1.struts2.HelloWorldAction" 
            method="execute">
            <result name="success">HelloWorld.jsp</result>
            <result name="error">/oops.jsp</result>
      </action>
   </package>
</struts>

HelloWorldAction.java HelloWorldAction.java

package com.junaid1.struts2;

public class HelloWorldAction{
       private String name;

       public String execute() throws Exception {
          return "success";
       }

       public String getName() {
          return name;
       }

       public void setName(String name) {
          this.name = name;
       }
    }

When i execute this project on tomcat server it displays the index.jsp page just fine .This page has a form . 当我在tomcat服务器上执行该项目时,它会显示index.jsp页面。该页面具有一个form。 when i press the submit button it gives an error saying "the requested resource is not available" with a message "/test1/abc" . 当我按下提交按钮时,显示错误消息“ / test1 / abc”,提示“请求的资源不可用”。 abc is the name of the action and this action is not being called . abc是操作的名称,并且不会调用此操作。 I have searched a lot before posting this question . 发布此问题之前,我进行了很多搜索。 At first i thought i might be missing some libraries but this does not seem to be the issue now to me as i am using maven for dependency management .Here is my pom.xml. 起初我以为我可能会缺少一些库,但这对我来说似乎不是问题,因为我正在使用maven进行依赖管理。这是我的pom.xml。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>test</groupId>
  <artifactId>test1</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
            <source>1.7</source>
            <target>1.7</target>
        </configuration>
      </plugin>  
    </plugins>
  </build>
  <dependencies>
    <dependency>
        <groupId>org.apache.struts</groupId>
        <artifactId>struts2-core</artifactId>
        <version>2.3.16</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.14</version>
    </dependency>
  </dependencies>
</project>

Thanks in advance . 提前致谢 。 link to the project structure is 链接到项目结构是 s17.postimg.org/axpn8cxbz/error.png

You can see the index page because it's shown by the web container Tomcat, and it doesn't have Struts tags (like any other web project without Struts framework). 您可以看到索引页面,因为它由Web容器Tomcat显示,并且没有Struts标记(就像其他任何没有Struts框架的Web项目一样)。

The browser url shows /test1/ . 浏览器网址显示/test1/ Then you are making request /test1/abc by pressing a submit button, and it was filtered by struts2, but it couldn't find action with name abc neither in namespace / nor in default namespace, and you don't have any resource with that path, so you fairly got 404 error. 然后,您通过按一个提交按钮发出请求/test1/abc ,它被struts2过滤,但是在命名空间/或默认命名空间中都找不到名称为abc操作,并且您没有任何资源该路径,因此您相当会出现404错误。

You might say that configured the action with the name abc in the default namespace. 您可能会说在默认名称空间中用名称abc配置了操作。 But this configuration is not available at runtime, also the package name should be struts-default which your package extends. 但是此配置在运行时不可用,软件包名称也应为软件包扩展的struts-default

The struts.xml file should be on classpath, it means that in src or resources folder. struts.xml文件应位于类路径上,这意味着该文件位于srcresources文件夹中。 When compiling it's copied to compiler output folder. 编译时将其复制到编译器输出文件夹。 After building it will be copied to WEB-INF/classes from the compiler output folder. 构建后,它将从编译器输出文件夹复制到WEB-INF/classes These two folders are temporarily made and could be deleted before the process, so if you don't have struts.xml in source folder then you are missing this file when you run your application. 这两个文件夹是临时创建的,可以在此过程之前删除,因此,如果源文件夹中没有struts.xml ,则在运行应用程序时会丢失此文件。

Also note, that you don't have any JSP pages in the webcontent folder that you used in the results. 另请注意,结果中使用的webcontent文件夹中没有任何JSP页面。

At first you should use in web.xml 首先,您应该在web.xml中使用

<filter-class>
        org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>

as org.apache.struts2.dispatcher.FilterDispatcher is deprecated. 由于不推荐使用org.apache.struts2.dispatcher.FilterDispatcher

Please take a look at this example . 请看这个例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM