SpringBoot集成Thymeleaf模板引擎

传统的JSP+JSTL组合是已经过去了,Thymeleaf是Java服务端的模板引擎,与传统的JSP不同,Thymeleaf可以使用浏览器直接打开,因为可以忽略掉拓展属性,相当于打开原生页面,给前端人员也带来一定的便利。我的这个博客网站也是SpringBoot+Thymeleaf搭建的。

1. 依赖导入

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

2. Thymeleaf相关配置

因为Tymeleaf默认是开启页面缓存的,所以在开发的时候,需要关闭这个页面缓存,其他基本不用怎么配置。

server:
  port: 8088
spring:
  thymeleaf:
    cache: false #关闭缓存

3. 测试页面访问

3.1 访问静态页面

这个和Thymeleaf没啥关系,我也把它一并写到这里,一般做网站的时候,都会自己做一个404页面和500页面,为了给用户一个友好的展示。SpringBoot中会自动识别模板目录下的404.html和500.html,所以我们在templare/error下新建两个html页面,然后分别打印些信息。以404为例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    这是404页面
</body>
</html>

再写一个controller测试一下:

@Controller
public class TestController {
    @RequestMapping("/test")
    public String test404() {
        return "index";
    }

    @RequestMapping("/test505")
    public String test505() {
        int i = 1 / 0;
        return "index";
    }
}

当我们在浏览器中输入localhost:8088/hello时,找不到对应的方法,就会跳转到404.html显示。
当我们在浏览器中输入localhost:8088/test505时,会抛出异常,然后会自动跳转到500.html显示。

【注】使用模板引擎时,Controller层就不能用@RestController注解了,否则会把返回的String当作json解析了,直接返回了,就不会去找页面,所以使用模板时要用@Controller注解。

3.2 Thymeleaf中处理对象

假如我们给前端传一个对象,如下:

public class Blogger {
    private Long id;
    private String name;
    private String pass;
    // 省去set和get
}

然后在controller层中初始化一下:

@RequestMapping("/blogger")
public String testObject(Model model) {
    Blogger blogger = new Blogger();
    blogger.setId(1L);
    blogger.setName("倪升武");
    blogger.setPass("12345");
    model.addAttribute("blogger", blogger);
    return "blogger";
}

再写一个blogger.html来渲染一下:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>博主信息</title>
</head>
<body>
<form action="" th:object="${blogger}" >
    用户编号:<input name="id" th:value="${blogger.id}"/><br>
    用户姓名:<input type="text" name="username" th:value="${blogger.getName()}" /><br>
    登陆密码:<input type="text" name="password" th:value="*{pass}" />
</form>
</body>
</html>

可以看出,使用th:object="${}"来获取对象信息,然后在表单里面可以有三种方式获取对象属性。

使用 th:value="*{属性名}" 使用 th:value="${对象.属性名}" 使用 th:value="${对象.get方法}"

可以看出,在Thymeleaf中可以像写java一样写代码,很方便。

3.3 Thymeleaf中处理List

List的话,就需要在前Thymeleaf中进行遍历了。先模拟一个List。

@RequestMapping("/list")
public String testList(Model model) {
    List<Blogger> bloggerList = new ArrayList<>();
    Blogger blogger = new Blogger();
    blogger.setId(1L);
    blogger.setName("倪升武");
    blogger.setPass("12345");
    bloggerList.add(blogger);

    Blogger blogger2 = new Blogger();
    blogger2.setId(2L);
    blogger2.setName("ITcodai");
    blogger2.setPass("12345");
    bloggerList.add(blogger2);

    model.addAttribute("list", bloggerList);
    return "list";
}

然后list.html中来遍历这个list。

<form action="" th:each="blogger : ${list}" >
    用户编号:<input name="id" th:value="${blogger.id}"/>
    用户姓名:<input type="text" name="password" th:value="${blogger.name}"/><br>
    登录密码:<input type="text" name="username" th:value="${blogger.getPass()}"/>
</form>

其实和JSTL差不多,Thymeleaf使用th:each进行遍历,表单里面可以直接使用${对象.属性名},也可以使用${对象.get方法},但是不能使用*{属性名},模板获取不到。

Thymeleaf还有很多其他用法,这里就不总结了,具体的可以参考Thymeleaf的官方文档(v3.0)

猜你喜欢

转载自www.cnblogs.com/yuxiang1/p/9178611.html