SpringMVC框架之入门程序

SpringMVC入门程序

创建web项目

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

添加Maven依赖

<dependencies>
<!-- 添加springmvc依赖 -->
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.3.16</version>
  </dependency>
    <!-- 添加serlvet依赖 -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>

在web.xml中配置核心前端控制器

替换web.xml版本

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

web.xml

<?xml version="1.0" encoding="UTF-8"?> <!-- XML 声明,指定 XML 版本和编码 -->
<!-- web-app:根元素,定义了整个 Web 应用的配置信息。
    通过 xmlns 和 xsi:schemaLocation 指定了 XML 的命名空间和对应的 XML Schema -->
<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">
    <!-- 定义Servlet -->
    <servlet>
        <!-- Servlet 的名称,这里是 springmvc -->
        <servlet-name>springmvc</servlet-name>
        <!-- Servlet 类的完整类名,这里是 Spring MVC 的核心前端控制器 DispatcherServlet -->
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--  Servlet 的初始化参数 -->
        <init-param>
            <!-- 参数名,这里是 contextConfigLocation,指定 Spring MVC 配置文件的位置 -->
            <param-name>contextConfigLocation</param-name>
            <!-- 参数值,classpath:springmvc.xml 表示 Spring MVC 配置文件 springmvc.xml 位于类路径下 -->
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        <!-- 指定 Servlet 的加载顺序,值为 0 表示容器启动时加载该 Servlet -->
        <load-on-startup>0</load-on-startup>
    </servlet>
    <!-- Servlet 的映射配置 -->
    <servlet-mapping>
        <!-- 指定要映射的 Servlet 名称,这里是 springmvc -->
        <servlet-name>springmvc</servlet-name>
        <!--  指定 URL 的模式,只有匹配 *.action 的请求会交由 springmvc Servlet 处理 -->
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>

</web-app>

Servlet 是 Java Web 应用程序中处理客户端请求的服务器端程序

Servlet 运行在服务器端的小型 Java 程序,通常作为 Web 服务器或应用服务器的一部分。它可以接收来自客户端(如浏览器)的请求,并生成动态的内容作为响应,或者处理传入的数据并调用后端逻辑进行处理

Servlet 通过处理 HTTP 请求和响应来实现与客户端的交互。它可以读取表单数据、处理用户输入、调用业务逻辑等,然后生成 HTML、JSON 或其他格式的响应返回给客户端

springmvc.xml

<?xml version="1.0" encoding="UTF-8"?> <!-- XML 声明,指定 XML 版本和编码 -->
<!--  beans: 根元素,定义了 Spring 配置文件的起始点
      xmlns: 属性定义了 XML 命名空间,这些命名空间都是 Spring Framework 提供的,用于不同的 Spring 模块和功能
        xmlns="http://www.springframework.org/schema/beans":定义 Spring Beans 的命名空间。
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance":XML Schema 实例命名空间。
        xmlns:mvc="http://www.springframework.org/schema/mvc":Spring MVC 模块的命名空间,用于配置 MVC 相关的内容。
        xmlns:aop="http://www.springframework.org/schema/aop":Spring AOP 模块的命名空间,用于配置面向切面编程相关的内容。
        xmlns:context="http://www.springframework.org/schema/context":Spring 上下文模块的命名空间,用于配置组件扫描等上下文相关的功能。
      xsi:schemaLocation 属性指定了每个命名空间对应的 XML Schema 的位置
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd:Spring Beans 模块的 XML Schema 位置。
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd:Spring AOP 模块的 XML Schema 位置。
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd:Spring MVC 模块的 XML Schema 位置。
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd:Spring 上下文模块的 XML Schema 位置。
 -->
<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:aop="http://www.springframework.org/schema/aop"
       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/aop
         http://www.springframework.org/schema/aop/spring-aop.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">


  <!-- context:component-scan:Spring Framework 提供的一个标签,用于自动扫描指定包及其子包下的类,
       base-package: 指定要扫描的基础包路径,这里是 com.sin.controller,意味着 Spring 将扫描该包及其子包下的类,查找并注册成为 Spring 的 Bean。-->
  <context:component-scan base-package="com.sin.controller"/>

</beans>

Controller

package com.sin.controller;

// 导入@Controller注解,声明该类为Spring MVC的Controller
import org.springframework.stereotype.Controller;
// 导入@RequestMapping注解,用于映射请求路径
import org.springframework.web.bind.annotation.RequestMapping;
// 导入ModelAndView类,用于封装视图和模型数据
import org.springframework.web.servlet.ModelAndView;

/**
 * @createTime 2024/8/5 10:31
 * @createAuthor SIN
 * @use
 */
@Controller  // 声明该类为Spring MVC的Controller
public class DemoController {

    @RequestMapping("/index.action")  // 处理对"/index.action"路径的请求
    public ModelAndView hello(){  // 处理请求的方法,返回一个ModelAndView对象
        // 创建一个ModelAndView对象
        ModelAndView modelAndView = new ModelAndView();
        // 设置视图名称为"/index.jsp",即返回到名为index.jsp的视图页面
        modelAndView.setViewName("/index.jsp");
        // 添加名为"msg"的模型数据,值为"我在学习spring-mvc程序"
        modelAndView.addObject("msg","我在学习spring-mvc程序");
        // 返回ModelAndView对象,包含视图名称和模型数据
        return modelAndView;
    }
}

ModelAndView 是 Spring MVC 框架中用于封装视图名和模型数据的类。它主要用于控制器方法处理请求后,向视图层传递数据并指定要渲染的视图页面

JSP

<%--
  Created by IntelliJ IDEA.
  User: sin80
  Date: 2022/11/8
  Time: 15:16
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<div style = "text-align: center">
    <h1>hello SpringMVC</h1>
    <h1>msg:${msg}</h1>
</div>

</body>
</html>

tamcat

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

运行结果

在这里插入图片描述

springMVC请求过程

  • tomcat启动的时候,会初始化DispatcherServlet(前端控制器),DispatcherServlet(前端控制器)中会创建一个SpringMVC容器,其实就是spring容器(ApplicationContext),只不过该容器的类型是(WebApplicationContext),该容器会加载到web.xml中contextConfigLocation指定的SpringMVC配置文件中

在这里插入图片描述

  • 由于springmvc.xml中指定了扫描包的规则,而HelloController.java在该包中,所以会被注册到SpringMVC容器中

在这里插入图片描述

  • 当发起*.action请求的时候,请求会到达DispatcherServlet(前端控制器)中,前端控制器会根据请求路径,去springmvc容器中找到能够处理这个请求的方法,这里通过注解–@RequestMapping来匹配的,这个注解可以将请求和方法进行映射,匹配的请求会被@RequestMapping标注的方法处理,所以该步骤中springmvc容器会发现HelloController这个bean的hello方法可以处理hello.action请求
  • DispatcherServlet中通过反射来调用DemoController这个bean的方法
  • DispatcherServlet接收到了hello方法的返回值
  • DispatcherServlet根据hello方法的返回值,做跳转操作,相当于servlet中的
request.getRequestDispatcher("/index.jsp").forward(request.response);
简化过程

客户端发送请求–》到达tomcat–》tomcat发现请求是hello.action的请求–》tomcat将请求转发给前端控制器DispatcherServlet–》前端控制器会根据url将转发给我们自定义的controller–》DispatcherServlet根据controller的返回结果做跳转操作–》将结果输出给客户端

SpringMVC整合Thymeleaf

pom.xml
<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf</artifactId>
    <version>3.0.11.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf-spring5</artifactId>
    <version>3.0.11.RELEASE</version>
</dependency>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- XML 声明,指定 XML 版本和编码 -->
<!-- 根元素,定义了整个 Web 应用的配置信息。
    通过 xmlns 和 xsi:schemaLocation 指定了 XML 的命名空间和对应的 XML Schema -->
<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">
    <!-- 定义Servlet -->
    <servlet>
        <!-- Servlet 的名称,这里是 springmvc -->
        <servlet-name>springmvc</servlet-name>
        <!-- Servlet 类的完整类名,这里是 Spring MVC 的核心前端控制器 DispatcherServlet -->
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--  Servlet 的初始化参数 -->
        <init-param>
            <!-- 参数名,这里是 contextConfigLocation,指定 Spring MVC 配置文件的位置 -->
            <param-name>contextConfigLocation</param-name>
            <!-- 参数值,classpath:springmvc.xml 表示 Spring MVC 配置文件 springmvc.xml 位于类路径下 -->
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        <!-- 指定 Servlet 的加载顺序,值为 0 表示容器启动时加载该 Servlet -->
        <load-on-startup>0</load-on-startup>
    </servlet>
    <!-- Servlet 的映射配置 -->
    <servlet-mapping>
        <!-- 指定要映射的 Servlet 名称,这里是 springmvc -->
        <servlet-name>springmvc</servlet-name>
        <!--  指定 URL 的模式,只有匹配 *.action 的请求会交由 springmvc Servlet 处理 -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>
springmvc.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- XML 声明,指定 XML 版本和编码 -->
<!--  beans: 根元素,定义了 Spring 配置文件的起始点
      xmlns: 属性定义了 XML 命名空间,这些命名空间都是 Spring Framework 提供的,用于不同的 Spring 模块和功能
        xmlns="http://www.springframework.org/schema/beans":定义 Spring Beans 的命名空间。
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance":XML Schema 实例命名空间。
        xmlns:mvc="http://www.springframework.org/schema/mvc":Spring MVC 模块的命名空间,用于配置 MVC 相关的内容。
        xmlns:aop="http://www.springframework.org/schema/aop":Spring AOP 模块的命名空间,用于配置面向切面编程相关的内容。
        xmlns:context="http://www.springframework.org/schema/context":Spring 上下文模块的命名空间,用于配置组件扫描等上下文相关的功能。
      xsi:schemaLocation 属性指定了每个命名空间对应的 XML Schema 的位置
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd:Spring Beans 模块的 XML Schema 位置。
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd:Spring AOP 模块的 XML Schema 位置。
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd:Spring MVC 模块的 XML Schema 位置。
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd:Spring 上下文模块的 XML Schema 位置。
 -->
<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:aop="http://www.springframework.org/schema/aop"
       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/aop
         http://www.springframework.org/schema/aop/spring-aop.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">


  <!-- context:component-scan:Spring Framework 提供的一个标签,用于自动扫描指定包及其子包下的类,
       base-package: 指定要扫描的基础包路径,这里是 com.sin.controller,意味着 Spring 将扫描该包及其子包下的类,查找并注册成为 Spring 的 Bean。-->
  <context:component-scan base-package="com.sin.controller"/>


  <!--
     启用 Spring MVC 注解驱动
     注册处理器映射器(HandlerMapping)、处理器适配器(HandlerAdapter)以及其他必要的组件,使得 Spring MVC 能够识别并处理使用了注解的控制器类和方法
   -->
  <mvc:annotation-driven />

  <!--
      配置模板解析器
      SpringResourceTemplateResolver:模板解析器负责解析模板文件,并将其转换为可以由模板引擎处理的模板对象

   -->
  <bean id="templateResolver" class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
    <!-- 指定模板文件的前缀路径。这里设置为 classpath:/templates/,表示模板文件位于类路径下的 resources/templates 目录中 -->
    <property name="prefix" value="classpath:/templates/" />
    <!-- 模板文件的后缀。这里设置为 .html,表示模板文件的后缀是 .html -->
    <property name="suffix" value=".html" />
    <!-- 模板的类型。这里设置为 HTML,表示使用 HTML 模板 -->
    <property name="templateMode" value="HTML" />
    <!-- 模板文件的字符编码,这里设置为 UTF-8,确保能正确地处理包含非英文字符的模板 -->
    <property name="characterEncoding" value="UTF-8" />
  </bean>


  <!--
      配置模板引擎
      SpringTemplateEngine 模板引擎负责处理模板的渲染和处理
   -->
  <bean id="templateEngine" class="org.thymeleaf.spring5.SpringTemplateEngine">
    <!-- 模板解析器用于解析 Thymeleaf 模板 -->
    <property name="templateResolver" ref="templateResolver" />
  </bean>

  <!--
      配置 Thymeleaf 视图解析器
      ThymeleafViewResolver:视图解析器,会根据视图名称解析出对应的视图对象
  -->
  <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
    <property name="templateEngine" ref="templateEngine" />
    <property name="characterEncoding" value="UTF-8" />
  </bean>
</beans>
Demo1Controller.java
package com.sin.controller;

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

/**
 * @createTime 2024/8/5 17:49
 * @createAuthor SIN
 * @use
 */

@Controller// 声明这是一个控制器类
public class Demo1Controller {


    /**
     * 请求方法,它接受一个 Model 对象作为参数,用于向视图传递数据
     */
    @RequestMapping("/hello")// 处理对 "/hello" 路径的请求
    public String hello(Model model) {
        // 向模型中添加属性
        model.addAttribute("message", "我在使用thymeleaf渲染数据");
        // 方法返回的字符串 "hello" 是视图的逻辑名称。这个逻辑名称会被视图解析器解析为一个实际的视图资源是一个thymeleaf模板文件
        return "hello";
    }
}

hello.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Hello</title>
</head>
<body>
<h1 th:text="${message}"></h1>
</body>
</html>
运行结果

在这里插入图片描述

  • 11
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

陆卿之

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

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

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

打赏作者

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

抵扣说明:

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

余额充值