IntelliJ IDEA 中配置 Spring MVC 环境的详细步骤及问题解决

2025-04-28 05:50

本文主要是介绍IntelliJ IDEA 中配置 Spring MVC 环境的详细步骤及问题解决,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

《IntelliJIDEA中配置SpringMVC环境的详细步骤及问题解决》:本文主要介绍IntelliJIDEA中配置SpringMVC环境的详细步骤及问题解决,本文分步骤结合实例给大...

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

步骤 1:创建 Maven Web 项目

新建项目

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

项目结构
确保项目包含以下目录:

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”)
– 如下图

IntelliJ IDEA 中配置 Spring MVC 环境的详细步骤及问题解决

步骤 3:配置 DispatcherServlet

方式 1:通过 web.xml 配置

配置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>javascript;

配置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/beans
     http://www.springframework.org/schema/beans/spring-beans.xsd
     http://www.springframew编程ork.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 配置(推荐)

创建配KlgfDdwG置类 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 {
    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        registry.jsp("/WEB-INF/views/", ".jsp");
    }
}

修改 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 和视图

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

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 并运行

添加 Tomcat 服务器

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

部署项目

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

启动服务器

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

常见问题解决

404 错误

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

JSP 无法解析

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

依赖冲突

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

扩展配置

静态资源处理

  • spring-mvc-servlet.xml 中添加:
<mvc:resources mapping="/static/**" location="/static/"/>
  • 静态文件存放在 src/main/webapp/static/ 目录下。

启用注解驱动

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

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

到此这篇关于IntelliJ IDEA 中配置 Spring MVC 环境的详细步骤的文章就介绍到这了,更多相关idea配置spring mvc环境内容请搜索China编程(www.chinasem.cn)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程China编程(www.chinasem.cn)!

这篇关于IntelliJ IDEA 中配置 Spring MVC 环境的详细步骤及问题解决的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1154405

相关文章

Spring Security自定义身份认证的实现方法

《SpringSecurity自定义身份认证的实现方法》:本文主要介绍SpringSecurity自定义身份认证的实现方法,下面对SpringSecurity的这三种自定义身份认证进行详细讲解,... 目录1.内存身份认证(1)创建配置类(2)验证内存身份认证2.JDBC身份认证(1)数据准备 (2)配置依

SpringBoot整合OpenFeign的完整指南

《SpringBoot整合OpenFeign的完整指南》OpenFeign是由Netflix开发的一个声明式Web服务客户端,它使得编写HTTP客户端变得更加简单,本文为大家介绍了SpringBoot... 目录什么是OpenFeign环境准备创建 Spring Boot 项目添加依赖启用 OpenFeig

Java Spring 中 @PostConstruct 注解使用原理及常见场景

《JavaSpring中@PostConstruct注解使用原理及常见场景》在JavaSpring中,@PostConstruct注解是一个非常实用的功能,它允许开发者在Spring容器完全初... 目录一、@PostConstruct 注解概述二、@PostConstruct 注解的基本使用2.1 基本代

springboot使用Scheduling实现动态增删启停定时任务教程

《springboot使用Scheduling实现动态增删启停定时任务教程》:本文主要介绍springboot使用Scheduling实现动态增删启停定时任务教程,具有很好的参考价值,希望对大家有... 目录1、配置定时任务需要的线程池2、创建ScheduledFuture的包装类3、注册定时任务,增加、删

SpringBoot整合mybatisPlus实现批量插入并获取ID详解

《SpringBoot整合mybatisPlus实现批量插入并获取ID详解》这篇文章主要为大家详细介绍了SpringBoot如何整合mybatisPlus实现批量插入并获取ID,文中的示例代码讲解详细... 目录【1】saveBATch(一万条数据总耗时:2478ms)【2】集合方式foreach(一万条数

SpringBoot中配置文件的加载顺序解读

《SpringBoot中配置文件的加载顺序解读》:本文主要介绍SpringBoot中配置文件的加载顺序,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录SpringBoot配置文件的加载顺序1、命令⾏参数2、Java系统属性3、操作系统环境变量5、项目【外部】的ap

SpringBoot UserAgentUtils获取用户浏览器的用法

《SpringBootUserAgentUtils获取用户浏览器的用法》UserAgentUtils是于处理用户代理(User-Agent)字符串的工具类,一般用于解析和处理浏览器、操作系统以及设备... 目录介绍效果图依赖封装客户端工具封装IP工具实体类获取设备信息入库介绍UserAgentUtils

Spring 中的循环引用问题解决方法

《Spring中的循环引用问题解决方法》:本文主要介绍Spring中的循环引用问题解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录什么是循环引用?循环依赖三级缓存解决循环依赖二级缓存三级缓存本章来聊聊Spring 中的循环引用问题该如何解决。这里聊

Java学习手册之Filter和Listener使用方法

《Java学习手册之Filter和Listener使用方法》:本文主要介绍Java学习手册之Filter和Listener使用方法的相关资料,Filter是一种拦截器,可以在请求到达Servl... 目录一、Filter(过滤器)1. Filter 的工作原理2. Filter 的配置与使用二、Listen

Spring Boot中JSON数值溢出问题从报错到优雅解决办法

《SpringBoot中JSON数值溢出问题从报错到优雅解决办法》:本文主要介绍SpringBoot中JSON数值溢出问题从报错到优雅的解决办法,通过修改字段类型为Long、添加全局异常处理和... 目录一、问题背景:为什么我的接口突然报错了?二、为什么会发生这个错误?1. Java 数据类型的“容量”限制