Spring MVC的HTTP状态404错误

问题描述:

我打字的URL后收到此错误 本地主机:8080 /的HelloWebSpring MVC的HTTP状态404错误

HTTP Status 404 - /HelloWeb 

type Status report 

message /HelloWeb 

description The requested resource is not available. 
Apache Tomcat/7.0.30 

我不能够解决这个问题,请帮助别人 这里是我所需要做的文件执行此Spring MVC的招呼程序

HelloController.java

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

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

     return "hello"; 
    } 
} 

的hello.jsp

<%@ page contentType="text/html; charset=UTF-8" %> 
<html> 
<head> 
<title>Hello World</title> 
</head> 
<body> 
    <h2>${message}</h2> 
</body> 
</html> 

的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> 

</web-app> 

的HelloWeb-servlet.xml中

<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" 
    xsi:schemaLocation=" 
    http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

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

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/jsp/" /> 
     <property name="suffix" value=".jsp" /> 
    </bean> 

</beans> 

,这里是我的项目层次

的HelloWeb
  SRC
    com.tutorialspoint
 的WebContent
      JSP
         的hello.jsp
      META-INF
          MANIFEST.MF
      WEB_INF
          LIB
         的HelloWeb-servlet.xml中
         的index.jsp
         的web.xml

对不起,我忘了包括以下我从

tutorialspoint.com/spring/spring_mvc_hello_world_example.htm

+0

我也得到同样的错误。你有解决方案吗? – Chetan 2015-02-18 06:24:13

+0

@Chetu不,我没有。 – 2015-02-18 06:58:09

您使用了错误的URL这个Spring MVC的例子。根据您的部署方式,正确的URL是localhost:8080/hello或localhost:8080/HelloWeb/hello。

+0

没有先生@atamanroman我使用URL本地主机:8080/HelloWeb /但只要我输入Http状态404错误来了。 – 2014-08-31 08:35:25

当你点击url localhost:8080/HelloWeb/它需要一个登陆页面来显示你可以在web.xml中指定的东西。添加到您的web.xml下

<welcome-file-list> 
    <welcome-file>/WEB-INF/index.jsp</welcome-file> 
</welcome-file-list> 

,但如果你需要访问servlet需要指定URL作为 localhost:8080/HelloWeb/hello

+0

我想要显示servlet,但只要我输入localhost:8080/HelloWeb/hello但是再次出现错误Http Status 404出现 – 2014-08-31 09:01:08

+0

你是什么意思通过显示servlet和你的url必须是localhost:8080/HelloWeb/hello HelloWeb用大写H – SparkOn 2014-08-31 09:05:34

+0

此外,您在HelloWeb-servlet.xml中缺少'' – SparkOn 2014-08-31 09:09:33

这是晚了,但我也有同样的问题,因为我也采取了代码tutorialspoint.com

尝试添加这在HelloWeb-servlet.xml

<context:annotation-config /> 

这将使annotat离子

和以及包括这在你的web.xml

<welcome-file-list> 
<welcome-file>/WEB-INF/jsp/login.jsp</welcome-file> 
</welcome-file-list> 

我有同样的问题,发现我的JSP名称有一个空格。来自tutorialspoint的示例完美无缺。

+1

这是一个比真实的答案更多的评论,因为你只是猜测。 – maRtin 2015-10-10 14:39:12

我知道这篇文章很旧,这是为了其他可能面临同样挑战的人。 您的网址映射未正确映射。使用这个作为你的控制器。一个变化就是“你好”到“的HelloWeb”你Controoler名称(@RequestMapping(“/你好”))应该是相同的web应用程序( 的HelloWeb / )

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

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

      return "hello"; 
     } 
    } 
名称

这是你的代码...

`<property name="prefix" value="/WEB-INF/jsp/" />` 

it shows that you have folder 'jsp' inside the 'WEB-INF' . 

但你没有一个文件夹中有通过看你的项目层次结构。

因此,更改代码到下面:

`<property name="prefix" value="/WEB-INF/" />.`