0
点赞
收藏
分享

微信扫一扫

SpringBoot的模板引擎、application.properties常用配置、controller层必记知识

影子喵喵喵 2022-03-19 阅读 22
spring

一:springboot的模板引擎thymeleaf

1.thymeleaf在pom.xml配置

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

2.模板引擎的作用
模板引擎的作用就是我们来写一个页面模板,比如有些值呢,是动态的,我们写一些表达式。而这些值,从哪来呢,我们来组装一些数据,我们把这些数据找到。然后把这个模板和这个数据交给我们模板引擎,模板引擎按照我们这个数据帮你把这表达式解析、填充到我们指定的位置,然后把这个数据最终生成一个我们想要的内容给我们写出去,这就是我们这个模板引擎。
在themplates目录下的所有页面,只能通过controller来跳转。
这个只能通过thymeleaf跳转。
3.在html文件中配置

<html lang="en" xmlns:th="http://www.thymeleaf.org">
   Variable Expressions变量:${}
   Selection Variable Expressions选择表达式:*{}
   Message Expressions信息:#{}
   Link URL Expressionsurl:@{}
   Fragment Expressions片段表达:~{}

样例:
Controller层

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

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

templates

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div th:text="${msg}"></div>

</body>
</html>

4.thymeleaf语法
在这里插入图片描述

二:springboot中application.properties常用配置和必记知识

1.application.propertries

/#关闭默认图标
spring.mvc.favicon.enabled=false
#关闭模板引擎的缓存
spring.thymeleaf.cache=false

2.使用@Controller注解跳转页面必须增加thymeleaf配置

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

3.@RestController和@Controller的区别
(1).@RestController无法返回页面,@Controller可以
(2).@RestController可以直接返回数据,可以自己在方法李确定返回数据的类型@Controller必须借助@RequestBody

举报

相关推荐

0 条评论