当前位置: 首页 > news >正文

SpringMVC环境搭建

文章目录

    • 1.模块创建
        • 1.创建一个webapp的maven项目
        • 2.目录结构
    • 2.代码
        • 1.HomeController.java
        • 2.home.jsp
        • 3.applicationContext.xml Spring配置文件
        • 4.spring-mvc.xml SpringMVC配置文件
        • 5.web.xml 配置中央控制器以及Spring和SpringMVC配置文件的路径
        • 6.index.jsp
    • 3.配置Tomcat
        • 1.配置路径以及热加载
        • 2.配置war包以及上下文路径为/
        • 3.启动
          • 1.首页
          • 2.访问 /home
    • 4.初始化基本流程解析

1.模块创建

1.创建一个webapp的maven项目

CleanShot 2025-02-10 at 12.21.01@2x

2.目录结构

CleanShot 2025-02-10 at 12.39.37@2x

2.代码

1.HomeController.java
package com.example.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Description:
 *
 * @Author sun
 * @Create 2025/2/10 12:23
 * @Version 1.0
 */
@Controller
public class HomeController {

    @RequestMapping("/home")
    public String home(Model model) {
        System.out.println("SpringMVC 执行了 home() 方法");
        model.addAttribute("message", "Hello, SpringMVC!");
        return "home"; // 返回 home.jsp
    }
}
2.home.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>SpringMVC Demo</title>
</head>
<body>
<h2>${message}</h2>
</body>
</html>
3.applicationContext.xml Spring配置文件
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 扫描 Service 组件 -->
    <context:component-scan base-package="com.example.service"/>

</beans>
4.spring-mvc.xml SpringMVC配置文件
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="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">

    <!-- 启用Spring MVC注解 -->
    <mvc:annotation-driven/>

    <!-- 扫描 Controller 层 -->
    <context:component-scan base-package="com.example.controller"/>

    <!-- 视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>
5.web.xml 配置中央控制器以及Spring和SpringMVC配置文件的路径
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
  <!-- 配置Spring MVC的DispatcherServlet -->
  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <!-- 配置Spring的ContextLoaderListener(用于加载根上下文) -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
  </context-param>
</web-app>
6.index.jsp
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>

3.配置Tomcat

1.配置路径以及热加载

CleanShot 2025-02-10 at 12.43.14@2x

2.配置war包以及上下文路径为/

CleanShot 2025-02-10 at 12.43.41@2x

3.启动
1.首页

CleanShot 2025-02-10 at 12.44.26@2x

2.访问 /home

CleanShot 2025-02-10 at 12.44.53@2x

CleanShot 2025-02-10 at 12.44.40@2x

4.初始化基本流程解析

  1. Tomcat启动,读取web.xml,装载中央控制器以及获取Spring以及SpringMVC的配置文件路径
  2. 由于中央控制器配置了load-on-startup所以会调用中央控制器的init方法完成Spring以及SpringMVC容器的初始化

相关文章:

  • 数据库——韩顺平(每日进行更新,直至更完)
  • 【Git】三、远程管理
  • Java 大视界 -- 深度洞察 Java 大数据安全多方计算的前沿趋势与应用革新(52)
  • MySQL数据库误删恢复_mysql 数据 误删
  • 物联网领域的MQTT协议,优势和应用场景
  • 大模型参数规模解析:32B中的“B“代表什么?如何影响AI性能?
  • C# CultureInfo 地区影响字符串
  • 如何通过腾讯 ima.copilot 训练自己的知识库
  • Repo、manifest以及Gerrit分别是什么?
  • C#的async异步方法里如果使用了await,那么它跟同步方法有什么区别?
  • KubeSphere 和 K8s 高可用集群离线部署全攻略
  • 解决No matching client found for package name xxx编译报错的问题
  • 软考高级《系统架构设计师》知识点(二)
  • Vue.js 与低代码开发:如何实现快速应用构建
  • git 克隆指定 tag 的项目
  • 基于MATLAB的沥青试样孔隙率自动分析——原理详解与代码实现
  • (前端基础)HTML(一)
  • 判断函数是否为react组件或lazy包裹的组件
  • flink cdc2.2.1同步postgresql表
  • 设置mysql的主从复制模式
  • 暗蓝评《性别打结》丨拆解性别之结需要几步?
  • 观察|英国航母再次部署印太,“高桅行动”也是“高危行动”
  • 我的科学观|张峥:AI快速迭代,我们更需学会如何与科技共处
  • 新干式二尖瓣瓣膜国内上市,专家:重视瓣膜病全生命周期管理
  • 持续更新丨伊朗内政部长:港口爆炸已致8人死亡750人受伤
  • 俄罗斯称已收复库尔斯克州