2016-08-03 120 views
0

这是错误信息: -Spring MVC中显示HTTP状态404

enter image description here

这是我的项目结构: -

enter image description here

以下是我的代码: -

HelloController.java

package com.home.pack; 

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 
@RequestMapping("/hello") 
public class HelloController{ 

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

     return "hello"; 
    } 

} 

的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.home.pack" /> 

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

的hello.jsp

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

我正在学习本教程 - >http://www.tutorialspoint.com/spring/spring_mvc_hello_world_example.htm,但我无法使其工作。似乎我在这里失去了一些东西,如果有人指出这一点,我会很感激。谢谢。

+0

我对spring mvc一无所知,但404意味着你的url不正确(找不到资源)。你有没有检查你的web.xml和绑定你/你好的逻辑。 HelloWeb servlet如何与HelloController相关? –

+0

只要提供特定方法的网址,就像您在@RequestMapping(method = RequestMethod.GET)中提供的方法一样...在这里提供值并将其添加到您的网址 –

+0

@NiravChhatrola:但我已经在班级本身。 –

回答

-1

您正在尝试显示页面“hello”?自从你做了@RequestMapping("/hello")之后,Spring可能会将它看作控制器...所以我想在这里你试图将控制器显示为一个页面,那肯定会显示404错误。

试图改变自己的方法,休耕:

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

    return "hello"; 
} 

,拔出控制器 我想这将更好地工作的请求映射......

或者让你的控制器上的请求映射和改变你的请求映射的方法为其他东西,并通过添加... /你好/ yourMethodValue访问它

+0

不,这也没有帮助。 –

+1

问题是,为什么上面的代码没有运行。如何以其他方式运行是另一回事 –

+0

我明白了,我的错误。 – anthomaxcool