網上有很多類似的問題,但沒有解決我的問題。我想使用基於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
附: 當我使用基於非註釋的方法時,一切正常,我能夠看到正在運行的網頁,直到我採用基於註釋的方法。
編輯:我的目錄結構的截圖:
在Eclipse上工作? –
如果您執行'@RequestMapping(value =「/ welcome」),會發生什麼 – bmarkham
@James Jithin是使用Eclipse。 –