org.springframework.web.servlet.PageNotFound noHan

2019-05-06 15:51发布

问题:

I went through many forums and blogs to get the answer but couldn't get any useful tip or advice. So please if anybody can help in below issue it would be a great help.

I am getting the below Warning and error when tried to connect to http://localhost:8080/SpringApp/hello :

INFO: Server startup in 6935 ms
Jul 19, 2014 11:15:42 AM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/SpringApp/] in DispatcherServlet with name 'HelloWeb'
Jul 19, 2014 11:16:29 AM com.example.java.HelloController printHelloWorld
INFO: HelloController : printHelloWorld : ENTER
Jul 19, 2014 11:16:29 AM com.example.java.HelloController printHelloWorld
INFO: HelloController : printHelloWorld : EXIT
Jul 19, 2014 11:16:29 AM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/SpringApp/WEB-INF/jsp/hello.jsp] in DispatcherServlet with name 'HelloWeb'

because of this I am getting HTTP Status 404 error in Tomcat.

The whole data is provided below:

The web.xml file is :

<display-name>Spring MVC Application</display-name>
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<servlet>
    <servlet-name>HelloWeb</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/HelloWeb-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>HelloWeb</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

The HelloWeb-Servlet.xml file is :

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd 
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<context:component-scan base-package="com.example.java"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/"></property>
    <property name="suffix" value=".jsp"></property>
</bean>
<context:annotation-config/>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
</beans>

The HelloController.java file is :

package com.example.java;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/hello")
public class HelloController {

    protected final Log logger = LogFactory.getLog(getClass());

    @RequestMapping(method=RequestMethod.GET)
    public String printHelloWorld(ModelMap model){
        logger.info("HelloController : printHelloWorld : ENTER");
        model.addAttribute("message", "Hello Sumit");
        logger.info("HelloController : printHelloWorld : EXIT");
        return "hello";
    }
}

回答1:

See My Answer Here= https://www.youtube.com/watch?v=-AtPAeSWz-o

The following worked for me. As you can see from the above YouTube video, I already tested it and it works.

Directory Structure:

Make sure your directory structure matches this.

web.xml:

A few things to note: The <url-pattern> should be <url-pattern>/<url-pattern> because when the app first starts up, it tries to connect to localhost:8080/YourAppName/ and not localhost:8080/YourAppName/homePage.

HelloController.java:

HelloWeb-servlet.xml



回答2:

Firstly, the file name should be HelloWeb-servlet.xml (servlet 's' lowercase). If still you have some problem try adding

<mvc:default-servlet-handler/>

in HelloWeb-servlet.xml



回答3:

I have Almost Spend 20 hrs solving this issue for Annotation based approach

this error means your DipatcherServlet didn't find the controller class

please place the Controller class or the class with annotation @RestController/@Controller in the child package of the Configuration class or class with annotaion @Component Scan

ex org.example.app ------> configuration class org.example.app.controller------->controller class



回答4:

I was also getting the same error.My mistake was that My Project name and servlet name were different.When I changed my servlet name to proj name ,I stopped getting this error.