知识点:
项目结构:
1.新建项目选择模板thymeleaf起步依赖和web起步依赖
pom.xml文件
- "1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0modelVersion>
- <parent>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-parentartifactId>
- <version>2.7.3version>
- <relativePath/>
- parent>
- <groupId>com.itgroupId>
- <artifactId>028-thymeleaf-courseartifactId>
- <version>0.0.1-SNAPSHOTversion>
-
- <properties>
- <java.version>1.8java.version>
- properties>
- <dependencies>
-
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-thymeleafartifactId>
- dependency>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
- dependency>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-testartifactId>
- <scope>testscope>
- dependency>
- dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-maven-pluginartifactId>
- plugin>
- plugins>
- build>
-
- project>
2.springboot的核心配置文件application.properties文件
- server.port=9001
- server.servlet.context-path=/myboot
-
- #在开发阶段,关闭模板缓存,让修改立即生效
- spring.thymeleaf.cache=false
-
- #编码格式
- spring.thymeleaf.encoding=UTF-8
-
- #模板的类型(默认是html)
- spring.thymeleaf.mode=HTML
-
- #模板的前缀:类路径的classpath:/templates/
- spring.thymeleaf.prefix=classpath:/templates/
-
- #后缀
- spring.thymeleaf.suffix=.html
3.新建实体类对象
Dog
- package com.it.entity;
-
- public class Dog {
- private String name;
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- @Override
- public String toString() {
- return "Dog{" +
- "name='" + name + '\'' +
- '}';
- }
- }
Cat
- package com.it.entity;
-
- public class Cat {
- private String name;
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- @Override
- public String toString() {
- return "Cat{" +
- "name='" + name + '\'' +
- '}';
- }
-
- }
Zoo
- package com.it.entity;
-
- public class Zoo {
- private Cat cat;
- private Dog dog;
-
- @Override
- public String toString() {
- return "Zoo{" +
- "cat=" + cat +
- ", dog=" + dog +
- '}';
- }
-
- public Cat getCat() {
- return cat;
- }
-
- public void setCat(Cat cat) {
- this.cat = cat;
- }
-
- public Dog getDog() {
- return dog;
- }
-
- public void setDog(Dog dog) {
- this.dog = dog;
- }
- }
SysUser
- package com.it.entity;
-
- public class SysUser {
- private Integer id;
- private String name;
- private String sex;
- private Integer age;
-
- public SysUser() {
- }
-
- public SysUser(Integer id, String name, String sex, Integer age) {
- this.id = id;
- this.name = name;
- this.sex = sex;
- this.age = age;
- }
-
- @Override
- public String toString() {
- return "SysUser{" +
- "id=" + id +
- ", name='" + name + '\'' +
- ", sex='" + sex + '\'' +
- ", age=" + age +
- '}';
- }
-
- public Integer getId() {
- return id;
- }
-
- public void setId(Integer id) {
- this.id = id;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public String getSex() {
- return sex;
- }
-
- public void setSex(String sex) {
- this.sex = sex;
- }
-
- public Integer getAge() {
- return age;
- }
-
- public void setAge(Integer age) {
- this.age = age;
- }
- }
4.resources目录下的static目录下的index.html文件
5.resources/templates目录下的文件
utilobject.html文件
6.进入主函数入口类ThymeleafApplication,启动项目进行测试
- package com.it;
-
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
-
- @SpringBootApplication
- public class ThymeleafApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(ThymeleafApplication.class, args);
- }
-
- }
测试结果:
#dates的测试结果
现在把dog的对象设置为null,在dog对象后面加下?,查看是否还会发生500错误