当前位置: 代码网 > it编程>编程语言>Java > IntelliJ IDEA 中配置 Spring MVC 环境的详细步骤及问题解决

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

2025年04月27日 Java 我要评论
以下是在 intellij idea 中配置 spring mvc 环境的详细步骤:步骤 1:创建 maven web 项目新建项目file -> new -> project &rarr

以下是在 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 无法解析

  • 确认视图解析器的 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环境内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com