美文网首页Spring Framework
Spring Boot ——Web页面访问及模板引擎thymel

Spring Boot ——Web页面访问及模板引擎thymel

作者: 程序员方方土 | 来源:发表于2019-12-31 10:50 被阅读0次

写在前面:

根据之前介绍的,Spring Boot Web项目创建步骤:

  • 创建Spring Boot应用,选中我们项目中需要的模块

  • 项目创建完后,其基础配置就已经完成,我们只需要在配置文件(application.yml)中指定少量的配置就可以运行

  • 业务代码编写

一、SpringBoot对静态资源的映射

1)在Spring Boot中我们将所有的前端使用的类库,如:jQuery等 webjars,以jar包的方式放在pom.xml文件中,自动引入依赖,如:

<dependency> 
   <groupId>org.webjars</groupId>
   <artifactId>jquery</artifactId>
   <version>3.4.1</version>
</dependency>
 

webjars地址: https://www.webjars.org/

Spring Boot源码会依据类文件下的目录访问对应的文件

classpath:/META-INF/resources/webjars/

这里想要详细了解可以看源码中 WebMvcAutoConfiguration类

访问方式:

启动Spring Boot, 访问url:http://localhost:8080/webjars/jquery/3.4.1/jquery.js

2)如何访问自定义页面

通过查看Spring Boot的底层代码WebMvcAutoConfiguration类,得知自定义页面,可以存放在,以下文件夹下,即类路径下的文件夹,通过访问http://localhost:8080/dasda.html 即可,不需要我们关心,Springboot会自动去查找dasda.html页面。

private static final String[] CLASSPATH_RESOURCE_LOCATIONS =
new String[]{
"classpath:/META-INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"};
image.png

3)Spring Boot自定义首页

欢迎页;静态资源文件夹下的所有index.html页面;被"/**"映射;

源码:

private Resource getIndexHtml(String location) {
    return this.resourceLoader.getResource(location + "index.html");
}

在不要求访问哪个页面时,自定扫描是否含有index.html, 有就会直接访问index页面。

二、模板引擎

SpringBoot推荐的Thymeleaf;

语法简单,功能强大;

我们也可以在创建项目的时候选择Thymeleaf模块

1、引入thymeleaf;

1)引入thymeleaf

 <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
    <version>3.0.9.RELEASE</version>
 </dependency>
2、Thymeleaf使用
@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;
 

只要我们把HTML页面放在classpath:/templates/,thymeleaf就能自动渲染;

使用:

1)导入thymeleaf的名称空间

<html lang="en" xmlns:th="http://www.thymeleaf.org">

2)使用thymeleaf语法;

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
 <h1>展示</h1>
姓名:<span th:text="${name}"></span>
 口头语:<span th:text="${say}"></span>
</body>
</html>

3)controller层

@Controller
public class UserController {
    @RequestMapping(value = "user")
    public String UserContext(Map<String,String> user){
        user.put("name","黄晓明");
        user.put("say","我不要你觉得,我要我觉得");
        return "user";
    }
}
 

结果展示:

url: http://localhost:8080/user

image.png
3、语法规则

th:text;改变当前元素里面的文本内容;

th:任意html属性;来替换原生属性的值

下图显示其他对应jsp标签

image.png

上一篇:Spring Boot ——Spring Boot与日志

相关文章

网友评论

    本文标题:Spring Boot ——Web页面访问及模板引擎thymel

    本文链接:https://www.haomeiwen.com/subject/ptkboctx.html