簡體   English   中英

獲取org.springframework.web.servlet.PageNotFound noHandlerFound錯誤

[英]Getting org.springframework.web.servlet.PageNotFound noHandlerFound error

我的第一個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>

嘿那里

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。

除了@EnableWebMvc注釋(特定於Java配置)與您的<mvc:annotation-driven />標記相同之外,您的配置看起來還不錯。(請參見EnableWebMvc注釋的含義

還針對此問題的修復,如在注釋中規定應添加<context:component-scan base-package="com.aditya.spring" />的后彈簧調度-servlet.xml中<mvc:annotation-driven>標簽,用於在spring應用程序上下文中注冊控制器。

根據您的例外,您的控制器已映射到/ welcome,因此您應嘗試localhost:<YourServerPort>/<YourWebAppDeployName>/welcome輸入您的控制器方法並訪問jsp-view。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM