HTTP状态404 –在Tomcat服务器上找不到

时间:2018-12-22 17:24:59

标签: java spring-mvc tomcat9

我正在为Web开发人员在Eclipse 4.9.0 Java EE IDE上创建新的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管理端口。

这是怎么回事,这是什么原因?我不知道为什么。有人可以在这里指出我的错误吗?

1 个答案:

答案 0 :(得分:0)

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

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

http://localhost/Latest/hello