2015-11-12 33 views
0

網上有很多類似的問題,但沒有解決我的問題。我想使用基於spring mvc annotations的方法制作簡單的'hello world'應用程序,但卡住了現在這個錯誤爲1周。Spring mvc - org.springframework.web.servlet.PageNotFound noHandlerFound

,我是說我在瀏覽器中點擊http://localhost:8080/FirstSpringMVCProject/welcome的錯誤是404,所請求的資源不可用,控制檯顯示的以下內容:

Nov 12, 2015 11:56:12 AM org.springframework.web.servlet.PageNotFound noHandlerFound 
WARNING: No mapping found for HTTP request with URI [/FirstSpringMVCProject/welcome] in DispatcherServlet with name 'dispatcher' 

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app id="WebApp_ID" version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 

<display-name>FirstSpringMVCProject</display-name> 


<servlet> 
<servlet-name>dispatcher</servlet-name> 
    <servlet-class> 
       org.springframework.web.servlet.DispatcherServlet 
    </servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
<servlet-name>dispatcher</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 

</web-app> 

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


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

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


    </beans> 

HelloController.java

package com.gontuseries.hellocontroller; 


import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.servlet.ModelAndView; 

@Controller 
public class HelloController { 

    @RequestMapping("/welcome") 
    public ModelAndView helloWorld() { 

     ModelAndView model = new ModelAndView("HelloPage"); 
     model.addObject("msg","hello world"); 

     return model; 
    } 

} 

HelloPage.jsp

<html> 
<body> 
    <h1>First Spring MVC Application Demo</h1> 

    <h2>${msg}</h2> 

</body> 
</html> 

我使用Spring MVC的4.2和Apache Tomcat 7.0

附: 當我使用基於非註釋的方法時,一切正常,我能夠看到正在運行的網頁,直到我採用基於註釋的方法。

編輯:我的目錄結構的截圖:

enter image description here

+0

在Eclipse上工作? –

+0

如果您執行'@RequestMapping(value =「/ welcome」),會發生什麼 – bmarkham

+0

@James Jithin是使用Eclipse。 –

回答

0

默認情況下Spring項目採取將基礎包名稱的最後一部分作爲項目名稱,並將其作爲artifactId標記添加到您的pom.xml中,因此請嘗試點擊此URL

http://localhost:8080/hellocontroller/welcome

+0

中提到的方法進行操作,因爲我沒有使用像maven或gradle這樣的自動化工具,只是使用基本設置,所以沒有任何pom.xml。 –

+0

好的。你有沒有試過這個URL http:// localhost:8080/hellocontroller/welcome? –

+0

只是給出了相同的結果 –

0

「dispacher-servlet.xml」似乎是錯誤拼寫的,即它應該是「dispatcher-servlet.xml」。 它應該匹配在web.xml中定義的servlet名稱

+0

謝謝,在寫問題時拼錯了,在我的開發環境中是正確的......現在更新了! –

+0

你能分享你的項目結構嗎? –

+0

我的目錄結構的屏幕截圖在問題中更新,請檢查。 –

0

嘗試以下解決方案:

Eclipse中的服務器上。雙點擊你有模塊單擊模塊選項卡將有路徑提到改變它按Alt鍵FirstSpringMvcProject

2.進入屬性選項卡在Eclipse中有+進入您的網絡項目設置路徑更改爲FirstSpringMvcProject

  • http://localhost:8080/FirstSpringMVCProject/welcome那裏你會有ve你第一mvc春季應用程序
  • 相關問題