以下是在 intellij idea 中配置 spring mvc 环境的详细步骤:
步骤 1:创建 maven web 项目
新建项目
file -> new -> project→ 选择 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”)
– 如下图

步骤 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>配置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.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 配置(推荐)
创建配置类 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 无法解析
- 确认视图解析器的
prefix和suffix配置正确。 - 确保 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环境内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论