在春天的noHandlerFound小应用程序?不知道为什么

时间:2018-08-11 12:13:32

标签: java spring spring-mvc jsp servlets

我已经创建了一个小型的基本演示应用程序,并尝试通过spring MVC应用程序在控制台上打印“我在这里”,但是我不知道为什么它花了2个小时却失败了,但是运气还不错。

AddController.java:

package com.surajkudale;

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

@Controller
public class AddController {


    @RequestMapping("/add")
    public void add()
    {
        System.out.println("Hi Suraj Kudaleeeeeeeeee");
    }

}

spring-servlet.xml

   <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">

<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.surajkudale"></context:component-scan>

</beans>

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>Archetype Created Web Application</display-name>

    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

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

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

index.jsp

<html>
<body>
    <form action="add">
        <input type="text" name="t1"> <input type="text" name="t2">
        <input type="submit">
    </form>
</body>
</html>

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.surajkudale</groupId>
  <artifactId>SpringMVCDemo</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>SpringMVCDemo 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-context</artifactId>
            <version>5.0.0.RELEASE</version>
        </dependency>

        <!-- The spring-aop module provides an AOP Alliance-compliant aspect-oriented 
            programming implementation allowing you to define -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>5.0.0.RELEASE</version>
        </dependency>

        <!-- The spring-webmvc module (also known as the Web-Servlet module) contains 
            Spring’s model-view-controller (MVC) and REST Web Services implementation 
            for web applications -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.0.0.RELEASE</version>
        </dependency>

        <!-- The spring-web module provides basic web-oriented integration features 
            such as multipart file upload functionality and the initialization of the 
            IoC container using Servlet listeners and a web-oriented application context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.0.0.RELEASE</version>
        </dependency>
  </dependencies>
  <build>
    <finalName>SpringMVCDemo</finalName>
  </build>
</project>

错误:

2018年8月11日下午5:31:59 org.springframework.web.servlet.PageNotFound noHandlerFound 警告:在DispatcherServlet中,名称为“ spring”的URI [/ SpringMVCDemo / add]的HTTP请求未找到映射

[项目结构] [1]:https://i.stack.imgur.com/9QPTP.png

1 个答案:

答案 0 :(得分:0)

花了我宝贵的时间后发现错误。没关系。

根本原因是我在错误的文件夹中创建了Controller,它应该位于java / main / src文件夹中