在 IDEA 中配置 Spring MVC 环境

新建 Maven 工程

和普通的Maven 工程相同,选择好工程目录,工程名(我用的是 SpringMVC01) 即可

在 pom.xml 文件中添加 依赖

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.1.19.RELEASE</version>
    </dependency>
</dependencies>

配置 Project Structure

  1. 鼠标选择 IDEA 右上角 File -> Project Structure -> Facts -> Web -> Chose Modules 窗口中选择当前工程 点OK

  2. Deployment Descriptor 中 Web Module Deployment Descriptor 中的路径, 修改为: 工程根目录\src\main\webapp\WEB-INF\web.xml , Deployment description version 选择 3.1 -> 修改 Web Resource Directories 中 Web Resource Directory 的路径为 : 工程根目录\src\main\webapp, Relative path in deployment directory 为 : /

  3. 点击右下角的 Create Artifact

随后工程目录中出现 src\main\webapp 目录

添加默认首页

在 webapp 目录下 新建一个 index.html 文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1> I am a test page !</h1>
</body>
</html>

配置 Add Configuration

找到 IDEA 右上角 Add Configuration,单击

  1. 点击 Templates -> Tomcat Server -> Local

  2. 点击 Application Server 旁边的 Configure, Tomcat Home 选择 Tomcat 安装目录(Tomcat 版本至少 8.5 ), 确保 URL 为 : http://localhost:8080/, HTTP port 为 8080, On 'update' Action 选项选择 update clasess and resources 也就是热部署, 点击右下角 OK

  3. 再次点击 IDEA 右上角的 Add Configuration -> 点击左上角的 加号 -> Tomcata Server -> Local, 可以看到刚才配置好的 Tomcat 选项

  4. 点击 Deployment -> 点击右边的加号 -> Artifact , 此时可以看到 工程名:Web: exploded 出现在 Deploy at the server startup 窗口中 -> 将 Application context 的路径改为: / ->点击OK

  5. 再次点击 IDEA 右上角的 Add Configuration ->点击 Tomcat -> Deployment -> 点击右边的 笔 Edit -> 将右边的 Maven 依赖全选 右键, 点击 Put into/WEB-INF/lib ->点击OK

运行默认首页

IDEA 右上角的 Add Configuration 选择 Tomcat, 点击右边的绿色三角形, 等待几秒钟,浏览器中显示默认主页

web.xml 中添加 DispatcherServlet

<!--   DispatchServlet
    是 Spring MVC 的核心对象, 用于来接 Http 请求
    并根据请求的 URL 调用与之对应的 Controller 方法, 来完成 Http 请求的处理 -->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <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>
        <load-on-startup>0</load-on-startup> <!-- Web 应用启动时, 自动创建 Spring IOC 容器, 并初始化 DispatchServlet -->
    </servlet>
    
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <!-- / 代表拦截 所有请求 -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>

配置 applicationContext 的 MVC 标记

在 src/main/resources 目录下新建 applicationContext.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mv="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!--
    context:component-scan 标签作用
    在Spring IOC初始化过程中,自动创建并管理indi.chester.springmvc及子包中
    拥有以下注解的对象.
    @Repository  DAO类 与数据打交道
    @Service 业务逻辑类
    @Controller 控制器类
    @Component 组件类. 不好区分的类
    -->
    <context:component-scan base-package="indi.chester.springmvc"></context:component-scan>

    <!-- 将图片/JS/CSS等静态资源排除在外,可提高执行效率 -->
    <mvc:default-servlet-handler/>

</beans>

创建 Controller 对象

@Controller
public class TestController {
    @GetMapping("/t") // 绑定 Get 请求 "http://localhost:8080/t" 就可以访问到
    @ResponseBody //直接向响应输出字符串数据, 不跳转界面
    public String test(){
        return "<h1> TestController success ! </h1>";
    }
}

随后点击右上角的绿色三角形运行 web 工程,等待弹出默认首页后,输入 http://localhost:8080/t, 即可访问到 TestController 处理后的页面。

Last updated