SpringBoot学习笔记-4:第四章 Spring Boot Web 开发(1)

简介: SpringBoot学习笔记-4:第四章 Spring Boot Web 开发

第四章 Spring Boot Web 开发

1、web 开发简介

https://start.spring.io/

  1. 创建 SpringBoot 应用,选中需要的模块
  2. 使用 SpringBoot 自动配置
  3. 编写业务代码
@AutoConfiguration 自动配置组件
@Properties 封装配置文件的内容

webjars&静态资源映射规则

1、webjars

配置类:WebMvcAutoConfiguration

webjars 以 jar 包的方式引入静态资源

https://www.webjars.org/


资源路径映射


/webjars/**


=>


classpath:/META-INF/resources/webjars/


添加 jquery 依赖


<dependency>

   <groupId>org.webjars</groupId>

   <artifactId>jquery</artifactId>

   <version>3.5.1</version>

</dependency>


访问路径


/webjars/jquery/3.5.1/jquery.js


2、静态资源映射规则


静态资源文件夹


classpath:/META-INF/resources/

classpath:/resources/

classpath:/static/

classpath:/public/

/ 当前项目根路径


默认静态文件下查找


# 欢迎页面

index.html


# 图标路径

favicon.ico


自定义静态资源文件路径,默认资源路径失效


spring.resources.static-locations=classpath:/hello/


引入 thymeleaf

JSP、Velocity、Thymeleaf、Freemarker

模板引擎

Template ${name}  + Data {"name": "Tom"}
=> TemplateEngine =>
output

Thymeleaf 依赖

<properties>
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <!-- 切换 thymeleaf version -->
    <!-- thymeleaf3 适配 layout2 -->
    <springboot-thymeleaf.version>2.1.1.RELEASE</springboot-thymeleaf.version>
    <thymeleaf-layout-dialect.version>2.0.0</thymeleaf-layout-dialect.version>
</properties>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
    <version>${springboot-thymeleaf.version}</version>
</dependency>

thymeleaf 语法

https://www.thymeleaf.org/

默认配置

public class ThymeleafProperties {
    private String prefix = "classpath:/templates/";
    private String suffix = ".html";
}

模板使用示例

package com.example.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.HashMap;
@Controller
public class IndexController {
    @RequestMapping("/hello")
    public String hello(HashMap<String, Object> map){
        map.put("name", "Tom");
        // 模板路径
        // src/main/resources/templates/about.html
        return "hello";
    }
}

模板:

src/main/resources/templates/about.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
  <head>
    <meta charset="UTF-8" />
    <title>Title</title>
  </head>
  <body>
    <h1>Hello</h1>
    <!-- 设置文本内容 -->
    <div th:text="${name}"></div>
  </body>
</html>

语法规则

th: 任意html属性,用来替换原生属性的值
th:text 改变文本内容(转义)
th:utext 改变文本内容(不转义)
th:attr
th:href
th:src
th:each
th:for

表达式

${} 变量表达式
    获取变量值
    获取变量属性
    调用方法
    内置基本对象: #ctx #session...
    内置工具对象:
*{} 选择表达式
    配合th:object使用
#{} 获取国际化内容
@{} 定义url
~{} 片段表达式
字面量
数学运算
布尔运算
比较运算
条件运算
特殊操作

示例


<!--文本输出-->

<div th:text="${name}"></div>


<!--循环遍历-->

<div th:each="pet: ${pets}">

 <div>[[${pet}]]</div>

</div>


<!--循环遍历-->

<div th:each="pet: ${pets}" th:text="pet"></div>


SpringMVC 自动配置原理

SpringBoot 对 SpringMVC 默认配置

自动配置

ViewResolver 视图解析器
根据方法返回值的到视图对象(View)
视图对象决定如何渲染、转发、重定向
Converter 类型转换器
Formatter 格式化器
HttpMessageConverters 转换请求响应
MessageCodesResolver 定义错误代码生成规则
WebDataBinder 数据绑定器

修改 SpringBoot 默认配置

优先使用用户配置@Bean/@Component

如果没有才自动配置

有些组件可以有多个

eg: ViewResolver 将用户配置和默认配置组合起来

相关文章
|
2月前
|
前端开发 Java 应用服务中间件
《深入理解Spring》 Spring Boot——约定优于配置的革命者
Spring Boot基于“约定优于配置”理念,通过自动配置、起步依赖、嵌入式容器和Actuator四大特性,简化Spring应用的开发与部署,提升效率,降低门槛,成为现代Java开发的事实标准。
|
2月前
|
前端开发 Java 微服务
《深入理解Spring》:Spring、Spring MVC与Spring Boot的深度解析
Spring Framework是Java生态的基石,提供IoC、AOP等核心功能;Spring MVC基于其构建,实现Web层MVC架构;Spring Boot则通过自动配置和内嵌服务器,极大简化了开发与部署。三者层层演进,Spring Boot并非替代,而是对前者的高效封装与增强,适用于微服务与快速开发,而深入理解Spring Framework有助于更好驾驭整体技术栈。
|
2月前
|
搜索推荐 JavaScript Java
基于springboot的儿童家长教育能力提升学习系统
本系统聚焦儿童家长教育能力提升,针对家庭教育中理念混乱、时间不足、个性化服务缺失等问题,构建科学、系统、个性化的在线学习平台。融合Spring Boot、Vue等先进技术,整合优质教育资源,提供高效便捷的学习路径,助力家长掌握科学育儿方法,促进儿童全面健康发展,推动家庭和谐与社会进步。
|
2月前
|
算法 Java Go
【GoGin】(1)上手Go Gin 基于Go语言开发的Web框架,本文介绍了各种路由的配置信息;包含各场景下请求参数的基本传入接收
gin 框架中采用的路优酷是基于httprouter做的是一个高性能的 HTTP 请求路由器,适用于 Go 语言。它的设计目标是提供高效的路由匹配和低内存占用,特别适合需要高性能和简单路由的应用场景。
279 4
|
2月前
|
XML Java 应用服务中间件
【SpringBoot(一)】Spring的认知、容器功能讲解与自动装配原理的入门,带你熟悉Springboot中基本的注解使用
SpringBoot专栏开篇第一章,讲述认识SpringBoot、Bean容器功能的讲解、自动装配原理的入门,还有其他常用的Springboot注解!如果想要了解SpringBoot,那么就进来看看吧!
446 2
|
3月前
|
人工智能 Java 机器人
基于Spring AI Alibaba + Spring Boot + Ollama搭建本地AI对话机器人API
Spring AI Alibaba集成Ollama,基于Java构建本地大模型应用,支持流式对话、knife4j接口可视化,实现高隐私、免API密钥的离线AI服务。
2876 1
基于Spring AI Alibaba + Spring Boot + Ollama搭建本地AI对话机器人API
存储 JSON Java
540 0
|
3月前
|
人工智能 Java 开发者
【Spring】原理解析:Spring Boot 自动配置
Spring Boot通过“约定优于配置”的设计理念,自动检测项目依赖并根据这些依赖自动装配相应的Bean,从而解放开发者从繁琐的配置工作中解脱出来,专注于业务逻辑实现。
1409 0
|
6月前
|
缓存 JavaScript 前端开发
鸿蒙5开发宝藏案例分享---Web开发优化案例分享
本文深入解读鸿蒙官方文档中的 `ArkWeb` 性能优化技巧,从预启动进程到预渲染,涵盖预下载、预连接、预取POST等八大优化策略。通过代码示例详解如何提升Web页面加载速度,助你打造流畅的HarmonyOS应用体验。内容实用,按需选用,让H5页面快到飞起!