当前位置: 首页 > news >正文

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):

<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 文件

    <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 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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/contexthttp://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

    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 {@Overridepublic void configureViewResolvers(ViewResolverRegistry registry) {registry.jsp("/WEB-INF/views/", ".jsp");}
    }
    
  2. 修改 web.xml 使用 AnnotationConfigServletWebServerApplicationContext

    <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

    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

    <%@ 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 中添加:

    <mvc:resources mapping="/static/**" location="/static/"/>
    
    • 静态文件存放在 src/main/webapp/static/ 目录下。
  2. 启用注解驱动
    确保 <mvc:annotation-driven/>@EnableWebMvc 已配置。


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

http://www.xdnf.cn/news/134047.html

相关文章:

  • 深入理解C++ 中的list容器
  • 在 Java 项目中搭建和部署 Docker 的详细流程
  • Jenkins流水线管理工具
  • Estimands与Intercurrent Events:临床试验与统计学核心框架
  • Flink TaskManager详解
  • Unity开发者快速认识Unreal 的BluePrint(二)
  • 软件测试流程
  • 代理ip和实际ip的区别和联系
  • MySQL的MVCC【学习笔记】
  • stone 3d v3.3.0版本发布,含时间线和连接器等新功能
  • 零信任架构:重塑网络安全的IT新范式
  • Redis ⑥-string | hash | list
  • 金仓数据库征文-政务领域国产化数据库更替:金仓 KingbaseES 应用实践
  • 使用springboot+easyexcel实现导出excel并合并指定单元格
  • VuePress可以做什么?
  • Keras中Lambda层的常用方法
  • 告别默认配置!Xray自定义POC开发指南
  • Linux字符设备驱动开发的详细步骤
  • ubuntu22.04 命令行修改静态ip
  • 贝叶斯优化GAM回归(matlab代码)
  • 牛客小白月赛115-B题:签到题
  • Python Cookbook-6.9 快速复制对象
  • 代码随想录算法训练营第60期第十七天打卡
  • 小白如何使用Cursor运行python程序(含环境配置教程)
  • 隐形革命:环境智能如何重构“人-机-境“共生新秩序
  • 关于循环缓冲区
  • 【java源码】AI智能导诊系统,基于H5、小程序、app等多端,引导患者自助就诊挂号,实现科学就诊
  • 4G卡的DTU固件TCP通讯
  • 【Rust】Rust中的枚举与模式匹配,原理解析与应用实战
  • 秒级到毫秒:BFD的速度革命