SpringBoot+thymeleaf配置

由于近排项目的需要,就用SpringBoot进行开发。于是就涉及到thymeleaf的使用。这里就不作详细介绍,thymeleaf的介绍可参考这里:thymeleaf介绍

1.准备

创建一个SpringBoot项目,以下为目录结构:
在这里插入图片描述

2.配置

创建成功后在pom.xml中如下引入依赖:

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

之后在springboot的配置文件(我这里命名为application.properites)中添加如下配置信息:

# templates文件夹的路径
spring.thymeleaf.prefix=classpath:/templates/
# templates中的所有文件后缀名,如/templates/main.html
spring.thymeleaf.suffix=.html

在templates文件夹中创建一个测试页面:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
Hello world
</body>
</html>

Controller页面:

@Controller
public class TestController {

    @RequestMapping(value = {"/indexPage"})
    public String indexData(){
        return "main";
    }
}

3.测试

在浏览器输入http://localhost:8080/indexPage,结果如下:
在这里插入图片描述
thymeleaf的用处不止如此,例如导入外部JS或CSS文件(注意的是这种js和css文件或文件夹必须在文件夹static中),可以这样导入:

<!-- JS导入 -->
<script th:src="@{/js/vue.js}"></script>
<!-- CSS导入 -->
<link th:href="@{/css/element-index.css}" rel="stylesheet" />

4.结尾

thymeleaf的用处不仅仅如此,还有很多方面暂时没有。以后会更加深入的研究。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的Spring Boot + Thymeleaf登录功能实现的示例。 1. 首先,我们需要在pom.xml文件中添加以下依赖项: ```xml <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. 在application.properties文件中添加如下配置: ```properties # 数据库配置 spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver # Thymeleaf配置 spring.thymeleaf.cache=false spring.thymeleaf.check-template=true spring.thymeleaf.check-template-location=true spring.thymeleaf.enabled=true spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html spring.thymeleaf.template-resolver-order=1 spring.thymeleaf.view-names=*.html ``` 3. 创建一个用户实体类User.java: ```java public class User { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } } ``` 4. 创建一个登录控制器LoginController.java: ```java @Controller public class LoginController { @GetMapping("/login") public String login() { return "login"; } @PostMapping("/login") public String login(@ModelAttribute User user, Model model) { if ("admin".equals(user.getUsername()) && "123456".equals(user.getPassword())) { return "success"; } else { model.addAttribute("error", "用户名或密码错误"); return "login"; } } } ``` 5. 在src/main/resources/templates目录下创建一个名为login.html的登录页面: ```html <!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>登录页面</title> </head> <body> <h1>登录页面</h1> <form th:action="@{/login}" method="post"> <p> <label for="username">用户名:</label> <input type="text" id="username" name="username" required> </p> <p> <label for="password">密码:</label> <input type="password" id="password" name="password" required> </p> <p> <button type="submit">登录</button> </p> <p th:text="${error}" style="color:red"></p> </form> </body> </html> ``` 6. 在src/main/resources/templates目录下创建一个名为success.html的登录成功页面: ```html <!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>登录成功</title> </head> <body> <h1>登录成功</h1> </body> </html> ``` 7. 启动应用程序,并访问http://localhost:8080/login进行登录。 以上就是一个简单的Spring Boot + Thymeleaf登录功能实现的示例。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值