首页 > 编程语言 >springboot 分析源码欢迎页和图标-> thymeleaf模板引擎常用语法->扩展

springboot 分析源码欢迎页和图标-> thymeleaf模板引擎常用语法->扩展

时间:2023-05-04 19:22:45浏览次数:49  
标签:springboot thymeleaf 源码 th 跳转 WebMvcAutoConfiguration public

欢迎页:

 

icon:

 注意点:

 thymeleaf模板引擎

1.使用thymeleaf模板引擎前要导入对应依赖包

2.阅读源码:

根据源码说明我们可以将html文件放置在templates目录下,然后通过controller进行跳转即可

 controller类:

//在templates下的东西需要通过controller类来跳转,
// 需要导入thymeleaf对应的依赖包
@Controller
public class IndexController {
    //跳转到templates目录下的测试页面
    @RequestMapping("/t")
    public String test1(){
        return "test";
    }
}
View Code

 

结果:

 thymeleaf语法介绍

这里测试一下在controller类设置一个属性给前端接收并展示,

首先要搞清楚在thymeleaf中是如何在前端页面展示接收到的数据的,查看源码发现标签th:text和th:each

html代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>test thymeleaf!!</h1>
<div th:text="${msg}"></div>
<div th:each="user:${users}" th:text="${user}"></div>
</body>
</html>
View Code

controller测试代码:

//在templates下的东西需要通过controller类来跳转,
// 需要导入thymeleaf对应的依赖包
@Controller
public class IndexController {
    //跳转到templates目录下的测试页面
    @RequestMapping("/t1")
    public String test1(){
        return "test";
    }
    @RequestMapping("/t2")
    public String test2(Model model){
        //在这里设置一个属性给前端接收并展示
        model.addAttribute("msg","test2---hello,thymeleaf!");
        return "test";

    }
    @RequestMapping("/t3")
    public String test3(Model model){
        //在这里设置一个属性给前端接收并展示
        model.addAttribute("msg","<h1>test3---hello,thymeleaf!</h1>");
        model.addAttribute("users", Arrays.asList("lian","xiaoming","xiaohong"));
        return "test";
    }
}
View Code

 

 运行结果(部分):

总结一些常用的thymeleaf标签:

th:text="${emp.getId()}" 将数据以文本形式展示在前端

th:each="emp:${emps}"遍历列表emps中的每一项

th:checked="${emp.getGender()==0}"判断,暂时不清楚

th:value="${#dates.format(emp.getBirth(),'yyyy-MM-dd')}" 暂时不太清楚,暂且理解的是取出数据传递给后端

 

扩展-> springmvc配置原理源码:

注意点:springmvc的配置在springboot也要用到自动装配类WebMvcAutoConfiguration

我们可以选择直接继承类WebMvcConfigurer并重写其中的方法

或者自己重新自定义thymeleaf也有自定义的视图解析器,但是如果需要自定义一些功能,可以写组件继承类WebMvcConfiguer

@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
   // WebMvcAutoConfiguration
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/lian").setViewName("test");//视图跳转,相当于请求转发
    }
    //ViewResolver实现了视图解析器接口的类,我们就可以把它看成视图解析器
    @Bean
    public ViewResolver viewResolver(){
        return new MyViewResolver();
    }

    //自定义一个自己的视图解析器MyViewResolver
    public static class MyViewResolver implements ViewResolver{
        @Override
        public View resolveViewName(String s, Locale locale) throws Exception {
            return null;
        }
    }
}
View Code

官方doc指出的问题:

 这是因为:

我们diy的自动配置类也是需要通过WebMvcAutoConfiguration类将我们diy的自动配置类给springboot托管的,WebMvcAutoConfiguration类中有个静态内部类WebMvcAutoConfigurationAdapter,这个类导入了EnableWebMvcConfiguration.class

 这个EnableWebMvcConfiguration类继承了DelegatingWebMvcConfiguration

 然而在DelegatingWebMvcConfiguration类中我们发现它继承了 WebMvcConfigurationSupport类

 

而在我们最开始的要保证类失效,必须满足WebMvcAutoConfiguration类能正常生效,但是WebMvcAutoConfiguration类要求不存在WebMvcConfigurationSupport类,这与我们上面说的冲突了,所以官方要求不加@EnableWebMvc

 =====================================分割线===================================================

标签:springboot,thymeleaf,源码,th,跳转,WebMvcAutoConfiguration,public
From: https://www.cnblogs.com/MyBlogs-joyiyii/p/17367618.html

相关文章

  • 从案例中详解go-errgroup-源码
    一、背景某次会议上发表了errorgroup包,一个g失败,其他的g会同时失败的错误言论(看了一下源码中的一句话Thefirstcalltoreturnanon-nilerrorcancelsthegroup,没进一步看其他源码,片面理解了)。//Thefirstcalltoreturnanon-nilerrorcancelsthegroup'scontext......
  • JUC并发编程原理精讲(源码分析)
    1.JUC前言知识JUC即java.util.concurrent涉及三个包:java.util.concurrentjava.util.concurrent.atomicjava.util.concurrent.locks普通的线程代码:ThreadRunnable没有返回值、效率相比入Callable相对较低!Callable有返回值!【工作常用】1.1进程和线程进程:是......
  • SpringBoot定义优雅全局统一Restful API 响应框架三
    我们目前已经设计出了,包含全局响应,异常错误响应进行了统一返回。但是错误内容我们设计的比较模糊统一,还可以进行细化这样更有利于定位错误当我们需要调用Http接口时,无论是在Web端还是移动端,都有可能遇到各种错误,例如参数缺失、类型错误、系统错误等。为了规范错误信息的返回,我们......
  • 互联网医院系统源码:数据安全与隐私保护问题如何解决?
    当下,互联网医院系统源码已经走进了很多人的视野中,它的作用和好处小编就不用强调了,今天我们来聊另一个话题——隐私与数据安全。在智慧医疗行业,安全问题更是重中之重,这也自然而然成为了老生常谈的一个问题。本文小编将从互联网医院系统源码的数据安全与隐私保护的意义、当前面临的挑......
  • springboot与mongodb之事务管理(二)
    一、事务说明1、在4.0版本中,MongoDB支持副本集上的多文档事务,分片集群是不支持事务的,会报以下异常TransactionsarenotsupportedbytheMongoDBclustertowhichthisclientisconnected2、在版本4.2中,MongoDB引入了分布式事务,在副本集或分片集群上都是支持事务的。3......
  • springboot单元测试中添加了@Autowired任然报错nullpoint(已解决)
     使用了autowired还是报错空指针异常说明就是包没有导入,springboot导包的操作在于启动application类,所以问题就在于没有单元测试时没有启动主类,解决方法:@RunWith(SpringJUnit4ClassRunner.class)@SpringBootTest(classes=SaTokenDemoApplication.class)加上如上的注解,@runwi......
  • 【SpringBoot】【一】 加载初始化器、监听器详解
    1 前言本节主要讲下SpringBoot启动的时候,加载初始化器、监听器的过程哈。2 加载时机我们先来看下加载的时机,也就是什么时候加载的呢,就是我们SpringBoot启动的时候,创建SpringApplication的时候就会去加载的,我们看下:@SpringBootApplicationpublicclassDemoApplicati......
  • springboot与mongodb之整合(一)
    一、添加maven依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-mongodb</artifactId><version>2.6.7</version></dependency>二、配置properties文件1、mongodb无......
  • springboot 切面注解方式 记录日志
    1.定义GateOpLogimportjava.lang.annotation.*;/***操作日志记录*@authorcodefulture*/@Target({ElementType.METHOD,ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Documentedpublic@interfaceGateOpLog{/***说明*/Strin......
  • 在线直播系统源码,默认倒计时,自定义输入时间倒计时
    在线直播系统源码,默认倒计时,自定义输入时间倒计时html部分代码 <divid="app">  <inputtype="num"v-model="time">  <inputtype="button" @click="click_input(time)"value="点击">  <div>{{get_cod......