IntelliJ IDEA 中配置 Spring MVC 环境的详细步骤

以下是在 IntelliJ IDEA 中配置 Spring MVC 环境的详细步骤:


步骤 1:创建 Maven Web 项目

  1. 新建项目

    • File -> New -> Project → 选择 Maven → 勾选 Create from archetype → 选择 maven-archetype-webapp
    • 输入 GroupId(如 com.example)、ArtifactId(如 spring-mvc-demo) → 点击 Next → 完成项目创建。
  2. 项目结构

    确保项目包含以下目录:

    复制代码
    src/main/
      ├── java/         # Java 代码
      ├── resources/    # 配置文件
      		└── applicationContext.xml
      └── webapp/       # Web 资源
          ├── WEB-INF/
          │   └── web.xml
          └── index.jsp

步骤 2:添加 Spring MVC 依赖

pom.xml 中添加以下依赖(Spring 5.x + Servlet 4.x):

xml 复制代码
<dependencies>
    <!-- Spring MVC -->
   <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.3.30</version>
    </dependency>  
</dependencies>
  • 1、保存后执行
    -- 在 Maven 工具窗口中,展开项目 -> Lifecycle。
    -- 双击 clean → 等待清理完成。
    -- 双击 install → 等待依赖下载和构建完成。
  • 2、将新的依赖加入到发布目录中
    -- 点击Edite Configurations-->选中当前Server-->右侧选择Deployment-->选中当前发布项目,点击编辑按钮,将新加入的依赖添加到左侧(选中依赖,右键"Put into WEB-INF/lib")
    -- 如下图

步骤 3:配置 DispatcherServlet

方式 1:通过 web.xml 配置
  1. 配置web.xml 文件

    xml 复制代码
    <web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee 
             https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd"
             version="6.0">
    
      <servlet>
         <servlet-name>springmvc</servlet-name>
         <!--配置DispatcherServlet    -->
         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
         <init-param>
             <param-name>contextConfigLocation</param-name>
             <param-value>classpath:applicationContext.xml</param-value>
         </init-param>
         <!--设置web应用启动时自动创建spring ioc容器并初始化DispatcherServlet-->
         <load-on-startup>0</load-on-startup>
     </servlet>
     <servlet-mapping>
         <servlet-name>springmvc</servlet-name>
         <!--拦截所有对象-->
         <url-pattern>/</url-pattern>
     </servlet-mapping>
    </web-app>
  2. 配置applicationContext.xml

    src/main/resource/ 下新建 applicationContext.xml

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:mvc="http://www.springframework.org/schema/mvc"
     xmlns:context="http://www.springframework.org/schema/context"
     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
">
  <!--在spring ioc初始化过程中,自动创建并管理com.hirain及其子包中拥有如下注解的对象:
  @Repository
  @Service
  @Controller
  @Component
  -->
  <context:component-scan base-package="com.hirain"/>
  <!--启用mvc注解开发模式-->
  <mvc:annotation-driven/>
  <!--将图片、css、js等静态资源排除在外,可提高执行效率-->
  <mvc:default-servlet-handler/>

</beans>
方式 2:纯 Java 配置(推荐)
  1. 创建配置类 src/main/java/com/example/config/WebConfig.java

    java 复制代码
    package com.example.config;
    
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    
    @Configuration
    @EnableWebMvc
    @ComponentScan(basePackages = "com.example.controller")
    public class WebConfig implements WebMvcConfigurer {
    
        @Override
        public void configureViewResolvers(ViewResolverRegistry registry) {
            registry.jsp("/WEB-INF/views/", ".jsp");
        }
    }
  2. 修改 web.xml 使用 AnnotationConfigServletWebServerApplicationContext

    xml 复制代码
    <context-param>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
    </context-param>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>com.example.config.WebConfig</param-value>
    </context-param>

步骤 4:创建 Controller 和视图

  1. Controller 类

    src/main/java/com/example/controller 下新建 HelloController.java

    java 复制代码
    package com.example.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    
    @Controller
    public class HelloController {
        @GetMapping("/hello")
        public String hello() {
            return "hello"; // 对应 /WEB-INF/views/hello.jsp
        }
    }
  2. JSP 视图

    src/main/webapp/WEB-INF/views 下新建 hello.jsp

    jsp 复制代码
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Hello Spring MVC</title>
    </head>
    <body>
        <h1>Hello, Spring MVC!</h1>
    </body>
    </html>

步骤 5:配置 Tomcat 并运行

  1. 添加 Tomcat 服务器

    • 点击右上角 Add Configuration+Tomcat Server -> Local
    • 指定 Tomcat 安装路径(若未配置,需先下载 Tomcat)。
  2. 部署项目

    • Deployment 标签页 → 点击 + → 选择 Artifact → 选择 spring-mvc-demo:war exploded
    • 设置上下文路径(如 /demo)。
  3. 启动服务器

    • 点击绿色三角按钮 → 访问 http://localhost:8080/demo/hello,看到页面显示 "Hello, Spring MVC!" 即成功。

常见问题解决

  1. 404 错误

    • 检查 @Controller@GetMapping 注解是否生效。
    • 确保 web.xml 中的 DispatcherServlet 映射正确(如 /*.do)。
  2. JSP 无法解析

    • 确认视图解析器的 prefixsuffix 配置正确。
    • 确保 JSP 文件位于 WEB-INF/views/ 目录下。
  3. 依赖冲突

    • 执行 mvn dependency:tree 检查依赖版本是否兼容。

扩展配置

  1. 静态资源处理

    spring-mvc-servlet.xml 中添加:

    xml 复制代码
    <mvc:resources mapping="/static/**" location="/static/"/>
    • 静态文件存放在 src/main/webapp/static/ 目录下。
  2. 启用注解驱动

    确保 <mvc:annotation-driven/>@EnableWebMvc 已配置。


完成以上步骤后,Spring MVC 环境即可正常运行。如果遇到问题,优先检查控制台日志和依赖树。

相关推荐
:-)28 分钟前
idea配置maven国内镜像
java·ide·maven·intellij-idea
小兔兔吃萝卜4 小时前
Spring 创建 Bean 的 8 种主要方式
java·后端·spring
AAA修煤气灶刘哥5 小时前
面试官: SpringBoot自动配置的原理是什么?从启动到生效,一文讲透
后端·spring·面试
qq_三哥啊8 小时前
【IDEA】设置Debug调试时调试器不进入特定类(Spring框架、Mybatis框架)
spring·intellij-idea·mybatis
别惹CC8 小时前
Spring AI 进阶之路01:三步将 AI 整合进 Spring Boot
人工智能·spring boot·spring
寒士obj8 小时前
Spring事物
java·spring
IT毕设实战小研17 小时前
基于Spring Boot 4s店车辆管理系统 租车管理系统 停车位管理系统 智慧车辆管理系统
java·开发语言·spring boot·后端·spring·毕业设计·课程设计
甄超锋18 小时前
Java ArrayList的介绍及用法
java·windows·spring boot·python·spring·spring cloud·tomcat
Java小白程序员21 小时前
Spring Framework:Java 开发的基石与 Spring 生态的起点
java·数据库·spring
甄超锋1 天前
Java Maven更换国内源
java·开发语言·spring boot·spring·spring cloud·tomcat·maven