SpringMVC环境搭建

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容器的初始化
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

S-X-S

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值