【问题标题】:Getting org.springframework.web.servlet.PageNotFound noHandlerFound error获取 org.springframework.web.servlet.PageNotFound noHandlerFound 错误
【发布时间】:2017-04-17 07:23:59
【问题描述】:

我的第一个 spring mvc 应用程序出现 404 错误,遵循所有配置但没有运气。请您帮忙解决问题。

    Following is the change:

web.xml

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


    <display-name>HereWeGo</display-name>
    <servlet>
        <servlet-name>spring-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>

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


</web-app>

spring-dispatcher-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="
     http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context-4.1.xsd
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
     http://www.springframework.org/schema/tx
     http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">

    <mvc:annotation-driven />

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

HeyThere.java

package com.aditya.spring;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.servlet.ModelAndView;

import org.springframework.web.servlet.config.annotation.EnableWebMvc;


@EnableWebMvc

@Controller

public class HeyThere{

      @RequestMapping("/welcome")
      protected ModelAndView handleRequestInternal(){
          ModelAndView modelandview = new ModelAndView("HelloPage");

          modelandview.addObject("welcomeMessage","Hi user, welcome to the first spring mvc application");

          return modelandview;

      }

}

Hello.jsp

<html>
<body>
    <h1>First Spring MVC Application Demo</h1>
    <h2>${welcomeMessage}</h2>
</body>
</html>

在服务器控制台中,我收到以下警告:

org.springframework.web.servlet.PageNotFound noHandlerFound

WARNING: No mapping found for HTTP request with URI [/HereWeGo/] in DispatcherServlet with name 'spring-dispatcher'

我的项目名称是 HereWeGo。

【问题讨论】:

    标签: java spring http-status-code-404


    【解决方案1】:

    您的配置看起来不错,除了 @EnableWebMvc 注释(特定于 Java 配置)与您的 &lt;mvc:annotation-driven /&gt; 标记相同。(参考此处 EnableWebMvc annotation meaning

    此外,此问题的修复方法,如 cmets 中所述,应添加 &lt;context:component-scan base-package="com.aditya.spring" /&gt; &lt;mvc:annotation-driven&gt; 标签后面的 spring-dispatcher-servlet.xml 用于在 spring 应用程序上下文中注册控制器。

    根据您的例外情况,您的控制器已映射到 /welcome,因此您应该尝试 localhost:&lt;YourServerPort&gt;/&lt;YourWebAppDeployName&gt;/welcome 进入您的控制器方法并访问您的 jsp 视图。

    【讨论】:

    • 删除了指定的注解并使用“/welcome”访问,得到相同的响应并出现以下错误。 org.springframework.web.servlet.PageNotFound noHandlerFound 警告:在名为“spring-dispatcher”的 DispatcherServlet 中找不到具有 URI [/HereWeGo/welcome] 的 HTTP 请求的映射
    • 如果 8080 是您的服务器端口,请尝试 localhost:8080/com.aditya.spring/welcome 并告诉我它是否工作
    • 不走运,还有一个观察结果是 : 也给出了 404 页面。
    • 您尝试添加 以在 spring 上下文中注册您的控制器 bean。
    • 通过在 &lt;mvc:annotation-driven&gt; 标签之后的 spring-dispatcher-servlet.xml 中添加 &lt;context:component-scan base-package="com.aditya.spring" /&gt;
    猜你喜欢
    • 2016-02-09
    • 2016-02-13
    • 2016-08-26
    • 1970-01-01
    • 1970-01-01
    • 2020-02-04
    • 2016-09-14
    • 2018-02-18
    • 2015-04-03
    相关资源
    最近更新 更多