Spring.Boot Web 模板引擎和首页为什么默认的是index.html页面呀《课时十》

简介: Spring.Boot Web 模板引擎和首页为什么默认的是index.html页面呀《课时十》

Spring.Boot Web 模板引擎和首页为什么默认的是index.html页面呀!

本博文重点解决的是两个问题:

问题一:为什么SpringBoot定制的首页是index.html页面?

问题二:什么是模板引擎,以及模板引擎的语法结构级如何使用?


在解决上面两个问题在下面的表中推荐四个官网便于各位读者在后面自己深入的学习:

image.png

解决问题一:为什么SpringBoot定制的首页是index.html页面?在Idea中ctrl+N搜索下面图片的内容

上面的字母又是搜索的目标信息

 

在打开上面的类:按住 Ctrl+R 键搜索getIndexHtml(Resource location)

 private Resource getIndexHtml(String location) {
            return this.getIndexHtml(this.resourceLoader.getResource(location));
        }
        private Resource getIndexHtml(Resource location) {
            try {
                Resource resource = location.createRelative("index.html");
                if (resource.exists() && resource.getURL() != null) {
                    return resource;
                }
            } catch (Exception var3) {
            }
            return null;
        }

结论一: Resource resource = location.createRelative("index.html");

从SpringBoot源码中得出结论SpringBoot默认页面是index.html

接下来测试以下:

看图中的代码等下运行结果出来后可以解释为什么SpringBoot默认的页面是index.html页面

package com.spring.springboot0907web;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//深入源码
@SpringBootApplication
public class Springboot0907WebApplication {
    public static void main(String[] args) {
        //调用  SpringApplication.run 来启用
        SpringApplication.run(Springboot0907WebApplication.class, args);
    }
}

这第一个问题已经得到了解决哦在学习SpringBoot中多看源码的内容介绍

 

问题二:什么是模板引擎,以及模板引擎的语法结构级如何使用?

先思考这个问题为什么要学习模板引擎?

前端交给我们的页面,是htmI页面。 如果是我们以前开发,我们需要把他们转成jsp页面,jsp好处就是当我们查出一-些数据转发到JSP页面以后,

我们可以用jsp轻松实现数据的显示,及交互等。jsp支持非常强大的功能,包括能写Java代码,但是呢,我们现在的这种情况,

SpringBoot这个项目首先是以jar的方式,不是war,我们用的还是嵌入式的Tomcat,所以呢,他现在默认是不支持jsp的。

那不支持jsp,如果我们直接用纯静态页面的方式,那给我们开发会带来非常大的麻烦,那怎么办呢,SpringBoot推荐你可以来使用模板引擎。

那么这模板弓|擎,我们其实大家听到很多,其实jsp就是一个模板弓|擎,还有以用的比较多的

freemarker,包括SpringBoot给我们推荐的Thymeleaf,模板弓|擎有非常多,

但再多的模板引擎,他们的思想都是一样的。


所以学习这项技术的目的是什么呢?


为了是解决SpringBoot这项技术默认是不支持jsp的,那接下来看我如何实现。

第一步:导入依赖 去官网找到对应的依赖

 <!--Thymeleaf  说明基于3.0.1 模板引擎-->
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring5</artifactId>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-java8time</artifactId>
        </dependency>
    </dependencies>

第二部分:到官网中学习


https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#messages

进入官网去学习

<!--语法格式:xmlns:th="http://www.thymeleaf.org">引入链接 -->

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
</html>

模板引擎放入的文件夹

下面是Thymeleaf的html放在的文件夹在哪里

@ConfigurationProperties(
    prefix = "spring.thymeleaf"
)
public class ThymeleafProperties {
    private static final Charset DEFAULT_ENCODING;
    public static final String DEFAULT_PREFIX = "classpath:/templates/";
    public static final String DEFAULT_SUFFIX = ".html";
    private boolean checkTemplate = true;
    private boolean checkTemplateLocation = true;
    private String prefix = "classpath:/templates/";
    private String suffix = ".html";
    private String mode = "HTML";
    private Charset encoding;
    private boolean cache;
    private Integer templateResolverOrder;
    private String[] viewNames;
    private String[] excludedViewNames;
    private boolean enableSpringElCompiler;
    private boolean renderHiddenMarkersBeforeCheckboxes;
    private boolean enabled;
    private final ThymeleafProperties.Servlet servlet;
    private final ThymeleafProperties.Reactive reactive;
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
  <head>
    <title>Good Thymes Virtual Grocery</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" type="text/css" media="all" 
          href="../../css/gtvg.css" th:href="@{/css/gtvg.css}" />
  </head>
  <body>
    <p th:text="#{home.welcome}">Welcome to our grocery store!</p>
  </body>
</html>

官网案例:

<html xmlns:th="http://www.thymeleaf.org">
  • 简单的表达:
  • 变量表达式:${...}
  • 选择变量表达式:*{...}
  • 消息表达式:#{...}
  • 链接 URL 表达式:@{...}
  • 片段表达式:~{...}


  • 字面量
  • 文本字面量:'one text', 'Another one!',…
  • 数字文字:0, 34, 3.0, 12.3,...
  • 布尔文字:true,false
  • 空文字:null
  • 文字标记:one, sometext, main,…


  • 文字操作:
  • 字符串连接:+
  • 字面替换:|The name is ${name}|
  • 算术运算:
  • 二元运算符:+, -, *, /,%
  • 减号(一元运算符):-
  • 布尔运算:
  • 二元运算符:and,or
  • 布尔否定(一元运算符)!:,not


比较和平等:

比较器:>, <, >=, <=( gt, lt, ge, le)

等式运算符:==, !=( eq, ne)

条件运算符:

如果-那么:(if) ? (then)

如果-那么-否则:(if) ? (then) : (else)

默认:(value) ?: (defaultvalue)

特殊代币:

无操作:_

上面是Thymeleaf学习的基本的语法结构:

th:元素名


总结:<!--所有的Html元素都可以被thymeleaf 替换接管: th元素名-->

 

官网的内容介绍

<!DOCTYPE html>
<!--语法格式:xmlns:th="http://www.thymeleaf.org">引入链接 -->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>滴滴</title>
</head>
<body>
<h1>thymeleaf 模板学习</h1>
<!--所有的Html元素都可以被thymeleaf 替换接管: th元素名-->
<!--/*@thymesVar id="msg" type="java"*/-->
<div th:text="${msg}"></div>
<!--不转义-->
<div th:utext="${msg}"></div>
<!--遍历数据-->
<!--th:each每次遍历都会生成当前这个标签:官网#9-->
<!--/*@thymesVar id="users" type="java"*/-->
<!--th:each 遍历元素-->
<h4 th:each="user :${users}" th:text="${user}"></h4>
<!--/*@thymesVar id="dogs" type="java"*/-->
<h4 th:each="dog :${dogs}" th:text="${dog}"></h4>
<!--<h4>-->
<!--    &lt;!&ndash;行内写法:官网#12&ndash;&gt;-->
<!--    <span th:each="user:${users}">[[${user}]]</span>-->
<!--</h4>-->
</body>
</html>
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.7.3)
2022-09-12 21:26:55.519  INFO 27412 --- [           main] c.s.s.Springboot0907WebApplication       : Starting Springboot0907WebApplication using Java 1.8.0_171 on HELLOWWORLD with PID 27412 (D:\maven-workspace\spaceVideospringboot\springboot0907-web\target\classes started by MZFAITHDREAM in D:\maven-workspace\spaceVideospringboot\springboot0907-web)
2022-09-12 21:26:55.519  INFO 27412 --- [           main] c.s.s.Springboot0907WebApplication       : No active profile set, falling back to 1 default profile: "default"
2022-09-12 21:26:56.271  INFO 27412 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2022-09-12 21:26:56.271  INFO 27412 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-09-12 21:26:56.271  INFO 27412 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.65]
2022-09-12 21:26:56.521  INFO 27412 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-09-12 21:26:56.521  INFO 27412 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 971 ms
2022-09-12 21:26:56.774  INFO 27412 --- [           main] o.s.b.a.w.s.WelcomePageHandlerMapping    : Adding welcome page: class path resource [public/index.html]
2022-09-12 21:26:56.932  INFO 27412 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2022-09-12 21:26:56.941  INFO 27412 --- [           main] c.s.s.Springboot0907WebApplication       : Started Springboot0907WebApplication in 1.722 seconds (JVM running for 2.646)

运行结果

package com.spring.springboot0907web.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Arrays;
//在templates下的只能controller
//需要模板引擎 thymeleof
@Controller
public class IndexController {
    @RequestMapping("/test")
    public String index(Model model){
        model.addAttribute("msg","<h2>Hellow Mode SpringBoot</h2>");
        model.addAttribute("msg","<h1>Hellow Mode SpringBoot</h1>");
        model.addAttribute("users", Arrays.asList("小明","小王","消费"));
        model.addAttribute("dogs",Arrays.asList("效果","小明","王大陆","王晓蓉"));
        return "test";
    }
}

使用一

<!--所有的Html元素都可以被thymeleaf 替换接管: th元素名-->
<!--/*@thymesVar id="msg" type="java"*/-->
<div th:text="${msg}"></div>
<!--不转义-->
<div th:utext="${msg}"></div>

使用二

<!--遍历数据-->
<!--th:each每次遍历都会生成当前这个标签:官网#9-->
<!--/*@thymesVar id="users" type="java"*/-->
<!--th:each 遍历元素-->
<h4 th:each="user :${users}" th:text="${user}"></h4>

使用三

<!--/*@thymesVar id="dogs" type="java"*/-->
<h4 th:each="dog :${dogs}" th:text="${dog}"></h4>

使用四

<!--<h4>-->
<!--    &lt;!&ndash;行内写法:官网#12&ndash;&gt;-->
<!--    <span th:each="user:${users}">[[${user}]]</span>-->
<!--</h4>-->
相关文章
|
前端开发 Java 数据库
微服务——SpringBoot使用归纳——Spring Boot集成Thymeleaf模板引擎——Thymeleaf 介绍
本课介绍Spring Boot集成Thymeleaf模板引擎。Thymeleaf是一款现代服务器端Java模板引擎,支持Web和独立环境,可实现自然模板开发,便于团队协作。与传统JSP不同,Thymeleaf模板可以直接在浏览器中打开,方便前端人员查看静态原型。通过在HTML标签中添加扩展属性(如`th:text`),Thymeleaf能够在服务运行时动态替换内容,展示数据库中的数据,同时兼容静态页面展示,为开发带来灵活性和便利性。
622 0
|
11月前
|
缓存 安全 Java
《深入理解Spring》过滤器(Filter)——Web请求的第一道防线
Servlet过滤器是Java Web核心组件,可在请求进入容器时进行预处理与响应后处理,适用于日志、认证、安全、跨域等全局性功能,具有比Spring拦截器更早的执行时机和更广的覆盖范围。
|
存储 安全 Java
如何在 Spring Web 应用程序中使用 @SessionScope 和 @RequestScope
Spring框架中的`@SessionScope`和`@RequestScope`注解用于管理Web应用中的状态。`@SessionScope`绑定HTTP会话生命周期,适用于用户特定数据,如购物车;`@RequestScope`限定于单个请求,适合无状态、线程安全的操作,如日志记录。合理选择作用域能提升应用性能与可维护性。
466 1
|
缓存 JSON 前端开发
第07课:Spring Boot集成Thymeleaf模板引擎
第07课:Spring Boot集成Thymeleaf模板引擎
1076 0
第07课:Spring Boot集成Thymeleaf模板引擎
超好看的404提示页面HTML源码
超好看的404提示页面HTML源码
801 77
|
存储 NoSQL Java
探索Spring Boot的函数式Web应用开发
通过这种方式,开发者能以声明式和函数式的编程习惯,构建高效、易测试、并发友好的Web应用,同时也能以较小的学习曲线迅速上手,因为这些概念与Spring Framework其他部分保持一致性。在设计和编码过程中,保持代码的简洁性和高内聚性,有助于维持项目的可管理性,也便于其他开发者阅读和理解。
318 0
|
前端开发 Java API
Spring Cloud Gateway Server Web MVC报错“Unsupported transfer encoding: chunked”解决
本文解析了Spring Cloud Gateway中出现“Unsupported transfer encoding: chunked”错误的原因,指出该问题源于Feign依赖的HTTP客户端与服务端的`chunked`传输编码不兼容,并提供了具体的解决方案。通过规范Feign客户端接口的返回类型,可有效避免该异常,提升系统兼容性与稳定性。
938 0
简约404错误页面HTML源码
简约404错误页面HTML源码
527 12
|
人工智能 程序员 UED
【01】完成新年倒计时页面-蛇年新年快乐倒计时领取礼物放烟花html代码优雅草科技央千澈写采用html5+div+CSS+JavaScript-优雅草卓伊凡-做一条关于新年的代码分享给你们-为了C站的分拼一下子
【01】完成新年倒计时页面-蛇年新年快乐倒计时领取礼物放烟花html代码优雅草科技央千澈写采用html5+div+CSS+JavaScript-优雅草卓伊凡-做一条关于新年的代码分享给你们-为了C站的分拼一下子
1062 21
【01】完成新年倒计时页面-蛇年新年快乐倒计时领取礼物放烟花html代码优雅草科技央千澈写采用html5+div+CSS+JavaScript-优雅草卓伊凡-做一条关于新年的代码分享给你们-为了C站的分拼一下子
|
前端开发 JavaScript
【02】v1.0.1更新增加倒计时完成后的放烟花页面-优化播放器-优化结构目录-蛇年新年快乐倒计时领取礼物放烟花html代码优雅草科技央千澈写采用html5+div+CSS+JavaScript-优雅草卓伊凡-做一条关于新年的代码分享给你们-为了C站的分拼一下子
【02】v1.0.1更新增加倒计时完成后的放烟花页面-优化播放器-优化结构目录-蛇年新年快乐倒计时领取礼物放烟花html代码优雅草科技央千澈写采用html5+div+CSS+JavaScript-优雅草卓伊凡-做一条关于新年的代码分享给你们-为了C站的分拼一下子
984 14
【02】v1.0.1更新增加倒计时完成后的放烟花页面-优化播放器-优化结构目录-蛇年新年快乐倒计时领取礼物放烟花html代码优雅草科技央千澈写采用html5+div+CSS+JavaScript-优雅草卓伊凡-做一条关于新年的代码分享给你们-为了C站的分拼一下子