Thymeleaf Petclinic 项目教程
1. 项目的目录结构及介绍
Thymeleaf Petclinic 项目的目录结构如下:
thymeleafexamples-petclinic/
├── src/
│ ├── main/
│ │ ├── java/
│ │ │ └── org/
│ │ │ └── thymeleaf/
│ │ │ └── examples/
│ │ │ └── petclinic/
│ │ │ ├── Application.java
│ │ │ ├── config/
│ │ │ ├── controller/
│ │ │ ├── model/
│ │ │ ├── repository/
│ │ │ └── service/
│ │ ├── resources/
│ │ │ ├── application.properties
│ │ │ ├── log4j.properties
│ │ │ └── messages/
│ │ └── webapp/
│ │ ├── WEB-INF/
│ │ │ └── views/
│ │ └── resources/
│ └── test/
│ └── java/
│ └── org/
│ └── thymeleaf/
│ └── examples/
│ └── petclinic/
└── pom.xml
目录结构介绍
src/main/java/
:包含项目的所有 Java 源代码。org/thymeleaf/examples/petclinic/
:项目的主要包。Application.java
:项目的启动类。config/
:包含项目的配置类。controller/
:包含项目的控制器类。model/
:包含项目的模型类。repository/
:包含项目的仓库类。service/
:包含项目的服务类。
src/main/resources/
:包含项目的资源文件。application.properties
:项目的配置文件。log4j.properties
:日志配置文件。messages/
:包含国际化消息文件。
src/main/webapp/
:包含 Web 应用的资源文件。WEB-INF/views/
:包含 Thymeleaf 模板文件。resources/
:包含静态资源文件,如 CSS、JavaScript 等。
src/test/java/
:包含项目的测试代码。pom.xml
:Maven 项目的配置文件。
2. 项目的启动文件介绍
项目的启动文件是 Application.java
,位于 src/main/java/org/thymeleaf/examples/petclinic/
目录下。
Application.java 文件内容
package org.thymeleaf.examples.petclinic;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
启动文件介绍
@SpringBootApplication
:这是一个组合注解,包含了@Configuration
、@EnableAutoConfiguration
和@ComponentScan
注解,用于简化 Spring Boot 应用的配置。main
方法:这是应用的入口点,通过调用SpringApplication.run
方法来启动 Spring Boot 应用。
3. 项目的配置文件介绍
项目的配置文件是 application.properties
,位于 src/main/resources/
目录下。
application.properties 文件内容
# ===============================
# = THYMELEAF
# ===============================
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.cache=false
# ===============================
# = DATASOURCE
# ===============================
spring.datasource.url=jdbc:hsqldb:mem:petclinic
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class