java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > idea配置spring mvc环境

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

作者:yuren_xia

这篇文章主要介绍了IntelliJ IDEA 中配置 Spring MVC 环境的详细步骤及问题解决,本文分步骤结合实例给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧

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

步骤 1:创建 Maven Web 项目

新建项目

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

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 服务器

部署项目

启动服务器

常见问题解决

404 错误

JSP 无法解析

依赖冲突

扩展配置

静态资源处理

<mvc:resources mapping="/static/**" location="/static/"/>

启用注解驱动

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

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

您可能感兴趣的文章:
阅读全文