SpringBoot之thymeleaf入门

首先简单介绍如何使用图标。先添加配置spring.mvc.favicon.enabled=true,开启图标使用,然后再在static下放置图标文件favicon.ico,命名最好不要改,这样首页图标就是下图:

theymeleaf入门:

SpringBoot的前端页面一般放在templates目录下,即模板,其中的页面不再为SpringMVC的jsp页面,而是html页面,Controller类中的方法返回的字符串名,即为页面名。导入thymeleaf依赖:

<!-- thymeleaf相关jar包 -->
<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf-spring5</artifactId>
    <version>3.0.10.RELEASE</version>
</dependency>

<dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-java8time</artifactId>
    <version>3.0.0.RELEASE</version>
</dependency>

在html文件中导入thymeleaf的命名空间:

<html lang="en" xmlns:th="http://www.thymeleaf.org">

然后html中的所有元素即可被thymeleaf接管,使用方法为 th:属性名,以下是使用thymeleaf接管div的text属性:

<div th:text="${msg}"></div>

其中msg是后端传来的数据(使用$符号获取数据的方式与SpringMVC一样):

@RequestMapping("/index")
public String toMainPage(Model model) {
    model.addAttribute("msg", "Hello, SpringBoot!");
    return "index";
}

下面是使用thymeleaf循环遍历列表的例子 :

<div th:each="user:${users}" th:text="${user}"></div>

再谈谈简化Controller:有时候实现一个特定请求的跳转页面很麻烦,要写一个Controller一个方法,返回页面名,为了简化,可以自定义配置类继承WebMvcConfigurer,覆写addViewControllers方法:

@Configuration
public class MyConfig implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
    }
}

上述方法表示"/"请求,直接跳转到index.html页面,即首页,而且可以继续配置其他特定请求跳转到特定页面,解决了每新增一个请求就要多写一个方法的问题。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用Spring BootThymeleaf来实现邮箱验证登录功能。下面是一些基本的步骤: 1. 添加所需的依赖:在您的项目的pom.xml文件中添加Spring BootThymeleaf的依赖项。 ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> ``` 2. 创建一个包含登录表单的HTML页面。您可以使用Thymeleaf模板引擎来渲染页面和处理表单提交。 ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Login</title> </head> <body> <h1>Login</h1> <form action="/login" method="post"> <input type="email" name="email" placeholder="Email" required/> <input type="password" name="password" placeholder="Password" required/> <button type="submit">Login</button> </form> </body> </html> ``` 3. 创建一个控制器类来处理登录请求和验证逻辑。 ```java @Controller public class LoginController { @GetMapping("/login") public String showLoginForm() { return "login"; } @PostMapping("/login") public String login(@RequestParam("email") String email, @RequestParam("password") String password) { // 进行邮箱验证和登录逻辑的处理 // ... return "redirect:/home"; } @GetMapping("/home") public String home() { return "home"; } } ``` 4. 创建一个用于显示登录成功页面的HTML模板。 ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Home</title> </head> <body> <h1>Welcome!</h1> </body> </html> ``` 这只是一个基本的示例,您可能还需要添加更多的逻辑来实现完整的邮箱验证和登录功能。您可以使用Spring Security来处理用户认证和授权,以及使用Java Mail API来发送验证邮件。希望这个简单的示例能帮助您入门

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值