【问题标题】:HTTP Status 404 – Not Found on Tomcat serverHTTP 状态 404 - 在 Tomcat 服务器上未找到
【发布时间】:2023-03-03 22:10:02
【问题描述】:

我正在 Eclipse 版本 4.9.0 Java EE IDE 上为 Web 开发人员创建新的 spring mvc 动态 Web 项目。我正在设置一个新的 Server Tomcat 版本 9.0.8。 当我选择hello.jspright-click ->Run As -> Run on Server 时,hello.jsp 工作正常,我访问helo.jsp。但是当我在服务器上选择项目根文件夹和right-click ->Run As -> Run 时,它会显示HTTP Status 404 并带有描述:

源服务器没有找到目标资源的当前表示或不愿意透露存在的表示。'

这是我的 Web.xml:

     <web-app id = "WebApp_ID" version = "2.4"
       xmlns = "http://java.sun.com/xml/ns/j2ee" 
       xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation = "http://java.sun.com/xml/ns/j2ee 
       http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Spring MVC Application</display-name>
    <servlet>
       <servlet-name>HelloWeb</servlet-name>
       <servlet-class>
         org.springframework.web.servlet.DispatcherServlet
       </servlet-class>
       <load-on-startup>1</load-on-startup>
     </servlet>

     <servlet-mapping>
       <servlet-name>HelloWeb</servlet-name>
         <url-pattern>/</url-pattern>
     </servlet-mapping>
     <welcome-file-list>
       <welcome-file>index.html</welcome-file>
       <welcome-file>index.htm</welcome-file>
       <welcome-file>index.jsp</welcome-file>
       <welcome-file>default.html</welcome-file>
       <welcome-file>default.htm</welcome-file>
       <welcome-file>default.jsp</welcome-file>
     </welcome-file-list>
   </web-app>

这是我完整的 HelloWeb-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns = "http://www.springframework.org/schema/beans"
  xmlns:context = "http://www.springframework.org/schema/context"
  xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
  xmlns:mvc="http://www.springframework.org/schema/mvc"
  xsi:schemaLocation = "http://www.springframework.org/schema/beans     
  http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
  http://www.springframework.org/schema/context 
  http://www.springframework.org/schema/context/spring-context-4.3.xsd
  http://www.springframework.org/schema/mvc
  http://www.springframework.org/schema/mvc/spring-mvc.xsd">

  <context:component-scan base-package="com.tutorialspoint"/> 
  <mvc:annotation-driven/>

  <bean name="jspViewResolver" 
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WebContent/views/"></property>
    <property name="suffix" value=".jsp"></property>
  </bean>

</beans>

这是我的控制器:

package com.mypackage;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.ModelMap;

@Controller
public class HelloController {
   @RequestMapping(path="/hello",method = RequestMethod.GET)
   public String printHello(ModelMap model) {
      model.addAttribute("message", "Hello Spring MVC Framework!");
      return "hello";
   }
}

这是我的目录结构: https://imgur.com/CHKEsJo

这是我的 hello.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
  <head>
    <meta charset="ISO-8859-1">
    <title>Hello World</title>
  </head>
  <body>
     <h2>done!!!</h2>
     <h2>${message}</h2>
  </body>
</html>

Tomcat 版本 9.0.8 上的 server.xml 保持原样。我没有改变任何东西。只有我改变了 HTTP/1.1 端口、AJP/1.3 端口和 Tomcat 管理端口。

这里有什么问题,这是什么原因?我不知道为什么。谁能在这里指出我的错误?

【问题讨论】:

  • 不走运。我从 Web.xml 编辑了 /*。保存并在Tomcat服务器上运行,显示同样的错误。
  • 另外提一下,你是如何访问 jsp 文件的,意思是我要问的上下文路径
  • 朋友们,抱歉耽搁了回复。这是我的目录结构和 hello.jsp

标签: java spring-mvc tomcat9


【解决方案1】:

尝试将文件夹views 移动到/WEB-INF/ 下,然后相应地更改您的HelloWeb-servlet.xml

另外,请确保您使用此 url 来访问资源:

http://localhost/Latest/hello

【讨论】:

  • 我搬到了这里。但是没有运气。
  • 看起来你的基础包不正确。 base-package="com.mypackage"
  • 请看我关于 Web.xml 文件的帖子。我添加了 HelloWeb/ 并且 tomcat 服务器显示 url localhost:8080/Latest 但这不正确.
  • 您如何尝试访问printHello()?你打的网址是什么?此外,您的 base-package 仍然不正确。
  • 当我点击 url localhost:8080/Latest/views/hello.jsp 时,helo.jsp 页面工作正常。 Bu当我选择(最新文件夹)->右键单击->在服务器上运行(在Tomcat 9.0.8上),错误显示'HTTP状态404 - 未找到:源服务器没有找到目标资源的当前表示或不愿意透露它的存在。
猜你喜欢
  • 2020-07-31
  • 2016-03-25
  • 2020-04-04
  • 2018-05-06
  • 2015-12-29
  • 2017-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多