Springboot视图解析与模板引擎~

简介: Springboot视图解析与模板引擎~

视图解析:

springboot默认不支持JSP,需要引入第三方模板引擎技术实现页面渲染

视图处理方式:转发,重定向,自定义视图

thymeleaf的使用:

1:引入starter

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

2:自动配置好了Thymeleaf,查看ThymeleafAutoConfiguration源码如下所示:

@AutoConfiguration(
    after = {WebMvcAutoConfiguration.class, WebFluxAutoConfiguration.class}
)
@EnableConfigurationProperties({ThymeleafProperties.class})
@ConditionalOnClass({TemplateMode.class, SpringTemplateEngine.class})
@Import({TemplateEngineConfigurations.ReactiveTemplateEngineConfiguration.class, TemplateEngineConfigurations.DefaultTemplateEngineConfiguration.class})
public class ThymeleafAutoConfiguration {

自动装配的策略:

1:所有thymeleaf的配置值都在ThymeleafProperties

2:配置好了SpringTemplateEngine

3:配置好了ThymeleafViewResolver

public static final String DEFAULT_PREFIX = "classpath:/templates/";
public static final String DEFAULT_SUFFIX = ".html";

开发:

package com.example.demo.Controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class ViewControllerTest {
    @GetMapping("/hello")
    public String show(Model model){
        model.addAttribute("msg","你好,springboot");
        model.addAttribute("link","http://www.baidu.com");
        return "success";
    }
}

必须在html页面中引入thymeleaf取值空间xmlns:th="http://www.thymeleaf.org

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1 th:text="${msg}" >哈哈</h1>
<h2>
<!--它是一个服务器端的变量表达式,需要在服务器端进行解析和替换,所以需要使用Thymeleaf来解析。这种方式可以实现动态跳转,因为${link}可以在服务器端根据条件动态生成不同的URL -->
  <a href="www.hello.com" th:href="${link}" >去百度1</a>
<!--它是Thymeleaf的URL语法,会根据应用程序的上下文自动解析和替换URL。这种方式适用于在模板文件中直接写死跳转的URL,不能实现动态跳转 -->
  <a href="www.hello.com" th:href="@{/link}" >去百度1</a>
</h2>
</body>
</html>

springboot启动项目:

package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Demo3Application {
    public static void main(String[] args) {
        SpringApplication.run(Demo3Application.class, args);
    }
}
相关文章
|
11天前
|
缓存 Java 开发者
10个点介绍SpringBoot3工作流程与核心组件源码解析
Spring Boot 是Java开发中100%会使用到的框架,开发者不仅要熟练使用,对其中的核心源码也要了解,正所谓知其然知其所以然,V 哥建议小伙伴们在学习的过程中,一定要去研读一下源码,这有助于你在开发中游刃有余。欢迎一起交流学习心得,一起成长。
|
15天前
|
Java Spring 容器
SpringBoot自动装配原理之@Import注解解析
SpringBoot自动装配原理之@Import注解解析
53 0
|
15天前
|
canal 缓存 关系型数据库
Spring Boot整合canal实现数据一致性解决方案解析-部署+实战
Spring Boot整合canal实现数据一致性解决方案解析-部署+实战
|
18天前
|
JSON Java Maven
Javaweb之SpringBootWeb案例之 SpringBoot原理的详细解析
Javaweb之SpringBootWeb案例之 SpringBoot原理的详细解析
45 0
Javaweb之SpringBootWeb案例之 SpringBoot原理的详细解析
|
18天前
|
Java 数据库连接 容器
SpringBoot之IOC&DI的详细解析
SpringBoot之IOC&DI的详细解析
10 0
SpringBoot之IOC&DI的详细解析
|
3天前
|
Java Android开发
Android12 双击power键启动相机源码解析
Android12 双击power键启动相机源码解析
12 0
|
3天前
|
分布式计算 Java API
Java8 Lambda实现源码解析
Java8的lambda应该大家都比较熟悉了,本文主要从源码层面探讨一下lambda的设计和实现。
|
4天前
|
算法 Java Go
ArrayList源码解析
ArrayList源码解析
9 1
|
4天前
|
存储 安全 Java
【HashMap源码解析(一)(佬你不来看看?)】
【HashMap源码解析(一)(佬你不来看看?)】
10 1
|
15天前
|
SQL 缓存 Java

推荐镜像

更多