找不到资源错误 404 [英] Resource not found error 404

查看:54
本文介绍了找不到资源错误 404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 struts2 上运行一个 Web 应用程序示例,但我遇到了一个问题.这里是代码web.xml

I am trying to run a web application example on struts2 and i am facing a problem. Here are the codes 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

    <%@ 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

<?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;
       }
    }

当我在 tomcat 服务器上执行这个项目时,它显示 index.jsp 页面就好了.这个页面有一个表单.当我按下提交按钮时,它给出一条错误消息,说请求的资源不可用",并显示一条消息/test1/abc".abc 是动作的名称,这个动作没有被调用.在发布这个问题之前,我已经搜索了很多.起初我以为我可能缺少一些库,但这对我来说现在似乎不是问题,因为我使用 maven 进行依赖管理.这是我的 pom.xml.

When i execute this project on tomcat server it displays the index.jsp page just fine .This page has a form . when i press the submit button it gives an error saying "the requested resource is not available" with a message "/test1/abc" . abc is the name of the action and this action is not being called . 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.

<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

推荐答案

您可以看到索引页,因为它是由 Web 容器 Tomcat 显示的,并且它没有 Struts 标签(就像任何其他没有 Struts 框架的 Web 项目一样).

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).

浏览器 url 显示 /test1/.然后您通过按下提交按钮发出请求 /test1/abc,并且它被 struts2 过滤,但它在命名空间 <中都找不到名称为 abc 的操作code>/ 也不在默认命名空间中,并且您没有该路径的任何资源,因此您会遇到 404 错误.

The browser url shows /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.

您可能会说在默认命名空间中配置了名为 abc 的操作.但是这个配置在运行时不可用,包名也应该是你的包扩展的struts-default.

You might say that configured the action with the name abc in the default namespace. But this configuration is not available at runtime, also the package name should be struts-default which your package extends.

struts.xml 文件应该在 classpath 上,这意味着在 srcresources 文件夹中.编译时将其复制到编译器输出文件夹.构建后,它将从编译器输出文件夹复制到 WEB-INF/classes.这两个文件夹是临时创建的,可以在此过程之前删除,因此如果源文件夹中没有 struts.xml,那么在运行应用程序时就会丢失此文件.

The struts.xml file should be on classpath, it means that in src or resources folder. When compiling it's copied to compiler output folder. After building it will be copied to WEB-INF/classes from the compiler output folder. 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.

另请注意,您在结果中使用的 webcontent 文件夹中没有任何 JSP 页面.

Also note, that you don't have any JSP pages in the webcontent folder that you used in the results.

这篇关于找不到资源错误 404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆