IDEA运行spring mvc 项目报404

问题描述

IDEA运行spring mvc 项目报404,百度未能解决
参考教程:https://edu.aliyun.com/lesson...

相关代码

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">

    <!--  配置分发器  -->
    <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>*.do</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"
       xsi:schemaLocation="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.xsd">

    <!--  配置handlerMapping  -->
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

    <!--  配置handlerAdapter  -->
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>

    <!--  配置  -->
    <bean id= "jspViewResolver" class= "org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name= "viewClass" value= "org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <!--  配置请求及处理器  -->
    <bean name="/index.do" class="Main"/>

</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>

运行类

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

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

public class Main implements Controller {
    @Override
    public ModelAndView handleRequest(HttpServletRequest hrt,
                                      HttpServletResponse hrs) throws Exception {
        ModelAndView mv = new ModelAndView();
        mv.addObject("msg","Hello SpringMVC!");
        mv.setViewName("index");
        return mv;
    }
}

运行截图

image.png

运行环境

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

相应文件下载

【因代码较多,为方便排错,项目已放在和彩云,链接: https://caiyun.139.com/m/i?0d... 提取码:VU2m 复制内容打开和彩云手机APP,操作更方便哦】

阅读 2.3k
1 个回答

<bean name="/index.do" class="Main"/> class使用全称

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