2016-08-03 112 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