简体   繁体   中英

Why do I always have this when starting a webmvc project? No mapping found for HTTP request?

This problem have been driving me crazy for a week now. I have been searching around solutions but cannot find it. I have tried starting new project and it keeps happening to me. I do not think its my code error. I think it has to do with my Eclipse configuration.

I am using JDK1.8, latest Eclipse IDE, and tomcat 8.5. If anyone can help me solve this problem, then u guys will make my whole week.

Please help me figure out the problem... PLEASE!

I'm going to try to make this clean as possible because I need help badly.

here is my directory tree:

目录树

First off, this is my pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.tutorialspoint</groupId>
  <artifactId>HelloWeb</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>HelloWeb Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>3.2.18.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>3.2.18.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>3.2.18.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>3.2.18.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>3.2.18.RELEASE</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>HelloWeb</finalName>
  </build>
</project>

This is my web.xml

    <!DOCTYPE web-app PUBLIC
     "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
     "http://java.sun.com/dtd/web-app_2_3.dtd" >

    <web-app>
      <display-name>Archetype Created Web Application</display-name>
      <servlet>
          <servlet-name>HelloWeb</servlet-name>
          <servlet-class>
             org.springframework.web.servlet.DispatcherServlet
          </servlet-class>
          <load-on-startup>1</load-on-startup>
       </servlet>

       <servlet-mapping>
          <servlet-name>HelloWeb</servlet-name>
          <url-pattern>/</url-pattern>
       </servlet-mapping>
    </web-app>


This is my Servlet 'HelloWeb-servlet.xml'
<?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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        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-3.2.xsd">


    <context:component-scan
        base-package="com.tutorialspoint">
    </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></context:annotation-config>
</beans>

My controller 'HelloController'

package com.tutorialspoint;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.ModelMap;

@Controller
@RequestMapping("/hello")
public class HelloController {
    @RequestMapping(method = RequestMethod.GET)
    public String printHello(ModelMap model) {
        model.addAttribute("message", "Hello Spring MVC Framework!");
        return "hello";
    }

}

Lastly, my jsp 'hello.jsp'

<%@ page contentType = "text/html; charset = UTF-8" %>
<html>
   <head>
      <title>Hello World</title>
   </head>

   <body>
      <h2>${message}</h2>
   </body>
</html>

Here is the console output and some warning code:

Apr 11, 2018 11:09:32 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.j2ee.server:HelloWeb' did not find a matching property.
Apr 11, 2018 11:09:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version:        Apache Tomcat/8.5.29
Apr 11, 2018 11:09:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built:          Mar 5 2018 13:11:12 UTC
Apr 11, 2018 11:09:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number:         8.5.29.0
Apr 11, 2018 11:09:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name:               Mac OS X
Apr 11, 2018 11:09:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version:            10.13.4
Apr 11, 2018 11:09:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture:          x86_64
Apr 11, 2018 11:09:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home:             /Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre
Apr 11, 2018 11:09:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version:           1.8.0_161-b12
Apr 11, 2018 11:09:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor:            Oracle Corporation
Apr 11, 2018 11:09:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE:         /Users/boss/Documents/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0
Apr 11, 2018 11:09:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME:         /Users/boss/Documents/apache-tomcat-8.5.29
Apr 11, 2018 11:09:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=/Users/boss/Documents/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0
Apr 11, 2018 11:09:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=/Users/boss/Documents/apache-tomcat-8.5.29
Apr 11, 2018 11:09:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=/Users/boss/Documents/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps
Apr 11, 2018 11:09:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=/Users/boss/Documents/apache-tomcat-8.5.29/endorsed
Apr 11, 2018 11:09:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=UTF-8
Apr 11, 2018 11:09:32 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/Users/boss/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.]
Apr 11, 2018 11:09:33 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
Apr 11, 2018 11:09:33 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Apr 11, 2018 11:09:33 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-nio-8009"]
Apr 11, 2018 11:09:33 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Apr 11, 2018 11:09:33 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 532 ms
Apr 11, 2018 11:09:33 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service [Catalina]
Apr 11, 2018 11:09:33 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/8.5.29
Apr 11, 2018 11:09:35 PM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Apr 11, 2018 11:09:37 PM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Apr 11, 2018 11:09:37 PM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
Apr 11, 2018 11:09:37 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'HelloWeb'
Apr 11, 2018 11:09:37 PM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'HelloWeb': initialization started
Apr 11, 2018 11:09:37 PM org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh
INFO: Refreshing WebApplicationContext for namespace 'HelloWeb-servlet': startup date [Wed Apr 11 23:09:37 PDT 2018]; root of context hierarchy
Apr 11, 2018 11:09:37 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/HelloWeb-servlet.xml]
Apr 11, 2018 11:09:37 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@7bb8b4fb: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.web.servlet.view.InternalResourceViewResolver#0,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
Apr 11, 2018 11:09:37 PM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'HelloWeb': initialization completed in 571 ms
Apr 11, 2018 11:09:37 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
Apr 11, 2018 11:09:37 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8009"]
Apr 11, 2018 11:09:37 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 4879 ms
Apr 11, 2018 11:09:38 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/HelloWeb/] in DispatcherServlet with name 'HelloWeb'

EDIT:

I have made an edit to my HelloController.java on @RequestMapping from /hello to /

The code still does not work.

package com.tutorialspoint;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.ModelMap;

@Controller
@RequestMapping("/")
public class HelloController {
    @RequestMapping(method = RequestMethod.GET)
    public String printHello(ModelMap model) {
        model.addAttribute("message", "Hello Spring MVC Framework!");
        return "hello";
    }

}

I didn't test it but you may try to adding the following to your web.xml file.

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/HelloWeb-servlet.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

Also, based on your controller the path would be /HelloWeb/hello

You are defining your web.xml for Tomcat 5, Servlet 2.3. Try switching to a newer version, at least 2.5, usign XSD instead of DTD

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">

Also, don't forget to declare a <mvc:annotation-driven /> member in your XML configuration.

And generally speaking, perhaps it should be better to start using a more up to date Spring MVC tutorial since the Spring version you're using (3.2) was released like 7 years ago?

I have done two things and it works fine in my side.

First,add <mvc:annotation-driven/> in HelloWeb-servlet.xml

Then,enable EL expression in hello.jsp with the following code:

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ page isELIgnored="false" %><!-- add this line to enable ex expression -->

After doing this, it can run success in my side,and the running result is as below:

在此处输入图片说明

If it still do not work in your side,you need to clean and rebuild it,or recreat it again.

I have upload my source code into Google Drive ,you can check it if necessary(note the project name is utest but the build path is /HelloWeb )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM