首页>>帮助中心>>香港云服务器的springboot整合thymeleaf的方法是什么

香港云服务器的springboot整合thymeleaf的方法是什么

2024/2/24 98次

香港云服务器的springboot整合thymeleaf的方法

首先,在pom.xml文件中添加Thymeleaf的依赖:

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-thymeleaf</artifactId>

</dependency>

复制代码

确保在application.properties文件中配置Thymeleaf的模板路径:

spring.thymeleaf.prefix=classpath:/templates/

spring.thymeleaf.suffix=.html

复制代码

创建一个Controller类,用于处理页面请求,并返回Thymeleaf模板:

@Controller

public class MyController {

@RequestMapping("/myPage")

public String myPage(Model model) {

model.addAttribute("message", "Hello, Thymeleaf!");

return "myPage";

}

}

复制代码

创建一个Thymeleaf模板文件(myPage.html),并在文件中使用Thymeleaf的语法进行页面渲染:

<!DOCTYPE html>

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

<head>

<title>My Page</title>

</head>

<body>

<h1 th:text="${message}"></h1>

</body>

</html>

复制代码

启动Spring Boot应用程序,并访问http://localhost:8080/myPage,即可看到页面上显示"Hello, Thymeleaf!"

通过以上步骤,就可以实现Spring Boot整合Thymeleaf的功能。

一诺网络香港免备案专区,提供「香港增强云服务器」和「香港特惠云服务器」两种类型的高可用弹性计算服务,搭载新一代英特尔®至强®铂金处理器,接入CN2低延时高速回国带宽线路,网络访问顺滑、流畅。机房网络架构采用了BGP协议的解决方案可提供多线路互联融合网络,使得不同网络运营商线路的用户都能通过最佳路由实现快速访问。香港云服务器低至29/月,购买链接:https://www.enuoidc.com/vps.html?typeid=2