如何通过SpringBoot和Thymeleaf模板引擎访问静态HTML文件?
- 内容介绍
- 文章标签
- 相关推荐
本文共计688个文字,预计阅读时间需要3分钟。
最近要做一个Java Web项目,因为页面不是很多,所以没有前后端分离,前后端都写在一起。这时候就用到thymeleaf了。以下是不动脑瓜子的thymeleaf教程。
一、创建Spring Boot的Web项目
1. 创建一个新的Spring Boot项目,选择Web作为项目类型。
2.添加必要的依赖,如Spring Web、Thymeleaf等。
二、配置Thymeleaf
1. 在application.properties或application.yml中添加以下配置:
spring.thymeleaf.prefix=classpath:/templates/spring.thymeleaf.suffix=.spring.thymeleaf.mode=HTML2. 创建一个名为`templates`的文件夹,并在该文件夹下创建HTML模板文件。
三、编写Thymeleaf模板
1. 在`templates`文件夹下创建一个名为`index.`的文件,并编写以下内容:
Thymeleaf教程 欢迎来到Thymeleaf教程
这是一个简单的Thymeleaf模板。
2. 在控制器中注入`Model`对象,并将数据传递给模板:
java@Controllerpublic class IndexController {
@GetMapping(/) public String index(Model model) { model.addAttribute(title, Thymeleaf教程); model.addAttribute(content, 这是一个简单的Thymeleaf模板。); return index; }}
四、运行项目
1. 运行Spring Boot项目。
2.打开浏览器,访问`http://localhost:8080/`,即可看到Thymeleaf模板渲染的页面。
以上就是使用Thymeleaf进行Java Web开发的简单教程。希望对你有所帮助!
最近要做一个java web项目,因为页面不是很多,所以就没有前后端分离,前后端写在一起,这时候就用到thymeleaf了,以下是不动脑式的傻瓜教程。。。。。
一:创建spring boot的web项目,过程略;
二:依赖如下:
<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> <version>2.1.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency>
三:配置文件:application.properties
#端口号 server.port=8099 # 配置 #thymeleaf spring.thymeleaf.cache=false spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.check-template-location=true spring.thymeleaf.suffix=.html spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.servlet.content-type=text/html spring.thymeleaf.mode=HTML
四:项目的templates文件夹下新建页面success.html,如下
五:controller
import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; /** * @author liuhongyang * @2020/10/20 14:35 * 文件说明: */ @Controller public class FirstTestController { @RequestMapping(value = "hello") public String hello(ModelMap modelMap) { modelMap.put("hei", "thymeleaf"); return "success"; } }
六:访问如下,完成
到此这篇关于SpringBoot使用Thymeleaf模板引擎访问静态html的过程的文章就介绍到这了,更多相关SpringBoot Thymeleaf模板访问静态html内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!
本文共计688个文字,预计阅读时间需要3分钟。
最近要做一个Java Web项目,因为页面不是很多,所以没有前后端分离,前后端都写在一起。这时候就用到thymeleaf了。以下是不动脑瓜子的thymeleaf教程。
一、创建Spring Boot的Web项目
1. 创建一个新的Spring Boot项目,选择Web作为项目类型。
2.添加必要的依赖,如Spring Web、Thymeleaf等。
二、配置Thymeleaf
1. 在application.properties或application.yml中添加以下配置:
spring.thymeleaf.prefix=classpath:/templates/spring.thymeleaf.suffix=.spring.thymeleaf.mode=HTML2. 创建一个名为`templates`的文件夹,并在该文件夹下创建HTML模板文件。
三、编写Thymeleaf模板
1. 在`templates`文件夹下创建一个名为`index.`的文件,并编写以下内容:
Thymeleaf教程 欢迎来到Thymeleaf教程
这是一个简单的Thymeleaf模板。
2. 在控制器中注入`Model`对象,并将数据传递给模板:
java@Controllerpublic class IndexController {
@GetMapping(/) public String index(Model model) { model.addAttribute(title, Thymeleaf教程); model.addAttribute(content, 这是一个简单的Thymeleaf模板。); return index; }}
四、运行项目
1. 运行Spring Boot项目。
2.打开浏览器,访问`http://localhost:8080/`,即可看到Thymeleaf模板渲染的页面。
以上就是使用Thymeleaf进行Java Web开发的简单教程。希望对你有所帮助!
最近要做一个java web项目,因为页面不是很多,所以就没有前后端分离,前后端写在一起,这时候就用到thymeleaf了,以下是不动脑式的傻瓜教程。。。。。
一:创建spring boot的web项目,过程略;
二:依赖如下:
<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> <version>2.1.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency>
三:配置文件:application.properties
#端口号 server.port=8099 # 配置 #thymeleaf spring.thymeleaf.cache=false spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.check-template-location=true spring.thymeleaf.suffix=.html spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.servlet.content-type=text/html spring.thymeleaf.mode=HTML
四:项目的templates文件夹下新建页面success.html,如下
五:controller
import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; /** * @author liuhongyang * @2020/10/20 14:35 * 文件说明: */ @Controller public class FirstTestController { @RequestMapping(value = "hello") public String hello(ModelMap modelMap) { modelMap.put("hei", "thymeleaf"); return "success"; } }
六:访问如下,完成
到此这篇关于SpringBoot使用Thymeleaf模板引擎访问静态html的过程的文章就介绍到这了,更多相关SpringBoot Thymeleaf模板访问静态html内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

