Spring mvc - org.springframework.web.servlet.PageNotFound noHandlerFound

时间:2015-11-12 06:53:17

标签: java spring spring-mvc

网上有很多类似的问题,但没有解决我的问题。我想使用基于spring mvc annotations的方法制作简单的'hello world'应用程序,但现在坚持这个错误1周。

我在浏览器上点击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>

调度-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

P.S。 当我使用基于非注释的方法时,一切都运行正常,我能够看到正在运行的网页,直到我采用基于注释的方法。

编辑:我的目录结构的屏幕截图:

enter image description here

4 个答案:

答案 0 :(得分:0)

检查你的tomcat配置文件,你的项目名称是:FirstSpringMVCProject,你可以尝试访问http://localhost:8080/welcome

答案 1 :(得分:0)

默认情况下,spring项目将基本包名称的最后一部分作为项目名称,并将其作为artifactId标记添加到pom.xml中,因此请尝试点击此URL

<强> http://localhost:8080/hellocontroller/welcome

答案 2 :(得分:0)

“dispacher-servlet.xml”似乎拼写错误,即它应该是“dispatcher-servlet.xml”。 它应该匹配您在web.xml中定义的servlet名称

答案 3 :(得分:0)

try the following solution:

1.double click on the server in eclipse there you have modules click the modules tab there will be a path mentioned change it to FirstSpringMvcProject

2.go to the properties tab by pressing alt+enter in eclipse there you have web project settings change the path to FirstSpringMvcProject

  1. go to http://localhost:8080/FirstSpringMVCProject/welcome there you will have you first mvc spring app