IDEA中使用注解运行spring mvc 项目报404

问题描述

IDEA运行spring mvc 项目报404,该项目使用注解编写,百度未能解决,访问后tomcat报 org.springframework.web.servlet.DispatcherServlet.noHandlerFound No mapping for GET /Hello.from【GET /Hello.from没有映射】现在不知道是什么原因导致的

参考教程:https://edu.aliyun.com/lesson...

项目结构

image.png

相关代码

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
  $END$
  ${msg }
  </body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </context-param>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.form</url-pattern>
    </servlet-mapping>
</web-app>

dispatcher-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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
        <!--前缀-->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!--后缀-->
        <property name="suffix" value=".jsp"/>
    </bean>

    <context:component-scan base-package="com.Controller"/>
</beans>

hello.jsp

<%--
  Created by IntelliJ IDEA.
  User: yujia
  Date: 2022/4/20
  Time: 17:18
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
aaa

</body>
</html>

运行类

package com.Controller;


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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class Main {
    @RequestMapping("/Hello")
    public ModelAndView hello(HttpServletRequest hsrt, HttpServletResponse hsre){
        ModelAndView mv = new ModelAndView();
        mv.addObject("msg","Hello,MVC!");
        mv.setViewName("Hello");

        return mv;
    }
}

运行截图

image.png
image.png

运行环境

interllij开发版2021.3
jdk1.8
spring 5
该项目为基于maven的spring项目

相应文件下载

【因代码较多,为方便排错,项目已放在和彩云,链接: https://caiyun.139.com/m/i?0d... 提取码:z3i1 】

阅读 3.2k
1 个回答

删掉servlet.xml下面的
image.png
代码即可

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题