SpringBoot DAY05 静态资源和模板引擎thymeleaf

目录

1.静态资源

 2.模板引擎

2.1.Thymeleaf

2.2 thymeleaf语法

2.3.基本表达式


​​​​​​​

1.静态资源

看了视频分析了一波源码,可以直接写结论了,源码我也看不懂,咱也不分析了

总结:

1.在SpringBoot中,我们可以使用以下方式来处理静态资源

1)webjars localhost:8080/webjars/

2)在resource下建立文件夹 名字为public或者resources或者static

三个的优先级为resources>static(默认)>public

这个一般是用于首页以及首页上的小图标的

比如先建立文件夹:

这样的话三个文件夹下文件的的优先级为resources>static(默认)>public,然后可以在文件夹里面放入静态网页之类的,我在public下面放了个静态网页和.ico类型的图片,目的是把网页的标签图片改一下,但是失败了,好像因为Spring Boot得最新版本把下面的功能给砍了(图片的名字和格式是固定的,也就是favicon.ico)

所以就没了,不过好像前端也能该网页标签的图像,这里我就不再纠结了

网页标签图:

 就记着三个的优先级为resources>static(默认)>public就行了

 2.模板引擎

模板引擎(这里特指用于Web开发的模板引擎)是为了使用户界面与业务数据(内容)分离而产生的,它可以生成特定格式的文档,用于网站的模板引擎就会生成一个标准的HTML文档。(百度)

前端交给我们的页面时HTML页面,我们在学javaweb、spring、springmvc、mybatis、ssm整合时用的都是把他们转为jsp页面,jsp来显示数据的显示。但是SpringBoot的项目是以jar的方式,而且用的还是嵌入式的tomcat,所以springboot不支持jsp。

所以springboot就推荐使用thymeleaf模板引擎(jsp也是模板引擎)。

模板引擎的作用:

就是我们来写一个页面模板,比如有些值呢,是动态的,我们写一些表达式。而这些值,从哪来呢,我们来组装一些数据,我们把这些数据找到。然后把这个模板和这个数据交给我们模板引擎,模板引擎按照我们这个数据帮你把这表达式解析、填充到我们指定的位置,然后把这个数据最终生成一个我们想要的内容给我们写出去,这就是我们这个模板引擎,不管是jsp还是其他模板引擎,都是这个思想。只不过呢,就是说不同模板引擎之间,他们可能这个语法有点不一样。其他的我就不介绍了,我主要来介绍一下SpringBoot给我们推荐的Thymeleaf模板引擎,这模板引擎呢,是一个高级语言的模板引擎,他的这个语法更简单。台且呢,功能更强大。

2.1.Thymeleaf

要使用它首先要先引入Thymeleaf,直接可以在pom.xml中引入依赖

<!--thymeleaf-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

然后就交给maven了。

使用

我们只需要把我们的html页面放在类路径下的templates下,thymeleaf就可以帮我们自动渲染了。使用thymeleaf什么都不需要配置,只需要将他放在指定的文件夹下即可!

测试:

1.编写一个testcontroller

package com.example.springboot.controller;

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

@Controller
public class testcontrol {
    @RequestMapping("/t1")
    public String test() {
        return "test";
    }
}

这个和写springmvc一样的

2.在templates下写个test.xml文件

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>测试页面</title>
</head>
<body>
<h1>测试</h1>
</body>
</html>

然后启动就行了

这里启动时我遇到了个问题就是项目启动了可是tomcat老是启动不成功,然后我的看了有二十分钟最后发现我之前不是写了一个dog类和person类,然后我把他们要使用的properties文件和yaml文件都注释了,所以这两个类一直报错使tomcat一直启动不成。然后我把他们连个给删了,发现我的静态资源的图片也能用了,就是那个张伟图片的页面标签,而且根本不用在application.properties中写spring.mvc.favicon.enabled=false,这样我不仅解决了上面的问题还把这第一个给启动了。原来不是版本的问题,还是怪那两个类!!!淦!!让我找半天,还以为idea坏了!!!!

张伟镇楼别再错了(文件目录也在下图,之前还有一个pojo包,dog和person类就在那个包下,然后我把pojo也删了,气死我了!!)

 这样完成了第一个thymeleaf

2.2 thymeleaf语法

我们做个最简单的练习 :我们需要查出一些数据,在页面中展示(我会在下面把语法写出来)

1、修改测试请求,增加数据传输;

package com.example.springboot.controller;

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

@Controller
public class testcontrol {
    @RequestMapping("/t1")
    public String test(Model model) {
        model.addAttribute("msg","hello thymeleaf");
        return "test";
    }
}

2、我们要使用thymeleaf,需要在html文件中导入命名空间的约束,方便提示。

我们可以去官方文档的#3中看一下命名空间拿来过来:

xmlns:th="http://www.thymeleaf.org"

3.、我们去编写下前端页面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>测试页面</title>
</head>
<body>
<h1>测试</h1>
<!--th:text就是将div中的内容设置为它指定的值,和之前学习的Vue一样-->
<div th:text="${msg}"></div>
</body>
</html>

4.测试

这样没错误真是轻松多了

2.3.基本表达式

Simple expressions:(表达式语法)
Variable Expressions: ${...}:获取变量值;OGNL;
    1)、获取对象的属性、调用方法
    2)、使用内置的基本对象:#18
         #ctx : the context object.
         #vars: the context variables.
         #locale : the context locale.
         #request : (only in Web Contexts) the HttpServletRequest object.
         #response : (only in Web Contexts) the HttpServletResponse object.
         #session : (only in Web Contexts) the HttpSession object.
         #servletContext : (only in Web Contexts) the ServletContext object.

    3)、内置的一些工具对象:
      #execInfo : information about the template being processed.
      #uris : methods for escaping parts of URLs/URIs
      #conversions : methods for executing the configured conversion service (if any).
      #dates : methods for java.util.Date objects: formatting, component extraction, etc.
      #calendars : analogous to #dates , but for java.util.Calendar objects.
      #numbers : methods for formatting numeric objects.
      #strings : methods for String objects: contains, startsWith, prepending/appending, etc.
      #objects : methods for objects in general.
      #bools : methods for boolean evaluation.
      #arrays : methods for arrays.
      #lists : methods for lists.
      #sets : methods for sets.
      #maps : methods for maps.
      #aggregates : methods for creating aggregates on arrays or collections.
==================================================================================

  Selection Variable Expressions: *{...}:选择表达式:和${}在功能上是一样;
  Message Expressions: #{...}:获取国际化内容
  Link URL Expressions: @{...}:定义URL;
  Fragment Expressions: ~{...}:片段引用表达式

Literals(字面量)
      Text literals: 'one text' , 'Another one!' ,…
      Number literals: 0 , 34 , 3.0 , 12.3 ,…
      Boolean literals: true , false
      Null literal: null
      Literal tokens: one , sometext , main ,…
      
Text operations:(文本操作)
    String concatenation: +
    Literal substitutions: |The name is ${name}|
    
Arithmetic operations:(数学运算)
    Binary operators: + , - , * , / , %
    Minus sign (unary operator): -
    
Boolean operations:(布尔运算)
    Binary operators: and , or
    Boolean negation (unary operator): ! , not
    
Comparisons and equality:(比较运算)
    Comparators: > , < , >= , <= ( gt , lt , ge , le )
    Equality operators: == , != ( eq , ne )
    
Conditional operators:条件运算(三元运算符)
    If-then: (if) ? (then)
    If-then-else: (if) ? (then) : (else)
    Default: (value) ?: (defaultvalue)
    
Special tokens:
    No-Operation: _

练习:

1.testcontroller类(还是哪个类不过又写了写东西)

package com.example.springboot.controller;

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

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Map;

@Controller
public class testcontrol {
    @RequestMapping("/t1")
    public String test(Model model) {
        model.addAttribute("msg","hello thymeleaf");
        return "test";
    }
    @RequestMapping("/t2")
    public String test1(Map<String,Object> map){
        map.put("msg","<h1>hello</h1>");
        map.put("users",  Arrays.asList("fbw","james"));
        return "test";
    }
}

2.html页面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>测试页面</title>
</head>
<body>
<h1>测试</h1>
<!--th:text就是将div中的内容设置为它指定的值,和之前学习的Vue一样-->
<div th:text="${msg}"></div>

<!--不转义 也就是html中的标签不会转义-->
<div th:utext="${msg}"></div>

<!--遍历数据-->
<!--th:each每次遍历都会生成当前这个标签:官网#9
也就是user遍历users里的内容-->
<h3 th:each="user :${users}" th:text="${user}"></h3>
<!--行内写法:官网#12-->
<h2>
   <span th:each="user:${users}">
       [[${user}]]
   </span>
</h2>
</body>
</html>

3.输出

写这些东西就不能错一点,错一点就完蛋,写那个ssm整合的时候也是这样,明明觉得写的逻辑没问题可是就是会报错,然后发现xml文件得注释是 <!---->,而我写成了//,还有实现接口的方法时名字不一致、属性名字写错....,这次又是因为类的原因,唉,还是要细心啊!也算是长了波经验吧。

对了今天是教员的诞辰128周年 缅怀致敬。前几天看了四渡赤水,真是觉得教员真是军事天才,那中绝境下还可以把敌人当狗溜,自己还可以全身而退,真是传奇。

2021/12/26 20:00

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值