对于application.properties这个文件配置
// 设置静态资源的路径为 classpath:/static/
spring.web.resources.static-locations=classpath:/static/
// 设置Thymeleaf模板的路径为 classpath:/templates/
spring.thymeleaf.prefix=classpath:/templates/
// 启用Thymeleaf模板引擎
spring.thymeleaf.enabled=true
// 设置Thymeleaf模板文件的后缀为 .html
spring.thymeleaf.suffix=.html
// 允许Bean的定义覆盖
spring.main.allow-bean-definition-overriding=true
// 配置数据库连接的URL、用户名和密码
spring.datasource.url=jdbc:mysql://localhost:3306/ol?useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
// 配置JPA属性
spring.jpa.show-sql=true // 是否显示SQL语句
spring.jpa.hibernate.ddl-auto=update // 自动更新数据库结构,但不删除数据
创建一个config的包,在下面建造一个ThymeleafConfig类
// 声明当前类是一个配置类,由Spring容器进行管理
@Configuration
public class ThymeleafConfig {
// 声明一个Bean,用于解析Thymeleaf模板的视图
@Bean
public ViewResolver thymeleafViewResolver() {
// 创建一个Thymeleaf视图解析器
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
// 配置模板引擎
resolver.setTemplateEngine(thymeleafTemplateEngine());
// 配置字符编码
resolver.setCharacterEncoding("UTF-8");
// 返回视图解析器
return resolver;
}
// 声明一个Bean,用于创建Thymeleaf模板引擎
@Bean
public SpringTemplateEngine thymeleafTemplateEngine() {
// 创建一个Spring模板引擎
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
// 配置模板解析器
templateEngine.setTemplateResolver(thymeleafTemplateResolver());
// 返回模板引擎
return templateEngine;
}
// 声明一个Bean,用于创建Thymeleaf模板解析器
@Bean
public ITemplateResolver thymeleafTemplateResolver() {
// 创建一个基于类加载器的模板解析器
ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
// 配置模板文件路径前缀
templateResolver.setPrefix("templates/");
// 配置模板文件后缀
templateResolver.setSuffix(".html");
// 配置模板文件类型
templateResolver.setTemplateMode(TemplateMode.HTML);
// 配置字符编码
templateResolver.setCharacterEncoding("UTF-8");
// 返回模板解析器
return templateResolver;
}
}
porn.xml基本配置
<?xml version="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.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.11</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo2</name>
<description>demo2</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--web依赖:tomcat,dispatcherServlet,xml....-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--spring-boot-starter所有的springboot依赖都是使用这个开头的-->
<!--单元测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.11</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_2.11</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-mllib_2.11</artifactId>
<version>2.4.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
demo包的主程序类代码情况
@Controller
@SpringBootApplication(scanBasePackages = "repository")
@Import(config.ThymeleafConfig.class)
@EnableJpaRepositories(basePackages = "repository")
public class Demo2Application {
@RequestMapping("/login")
public String index(Model model){
model.addAttribute("message", "欢迎!");
return "login";
}
@RequestMapping("/index")
public String index01(Model model){
model.addAttribute("message", "欢迎!");
return "index";
}