SpringBoot返回html界面的使用示例
作者:@ljn
本文主要介绍了SpringBoot返回html界面的使用示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
方法一:
(1)html文件要放在resource下的static目录下(没有static 自己就创建一个文件夹)
(2)在application.yml 中配置视图解析器
spring:
mvc:
view:
prefix: /
suffix: .html(3)写web层
@RequestMapping("/show")
public String show(){
return "defain";
}注意:类前别是:@RestController 用@Controller 因为返回是界面 无需增强
方法二
使用Thymeleaf模板引擎:Thymeleaf是Spring Boot官方支持的一种模板引擎。它可以方便地和Spring Boot项目一起使用。我们可以用Thymeleaf编写HTML模板,然后在后端填充模板里的数据,这时Spring Boot就会自动把渲染好的HTML页面发送给浏览器。
(1)将html文件放在resource下的templates目录下
(2)在application.yml 中配置视图解析器
thymeleaf:
cache: false
prefix:
classpath: /templates(3)添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.5.0</version>
</dependency>(4)书写web层
@RequestMapping("/show")
public String show(){
return "defain";
}到此这篇关于SpringBoot返回html界面的使用示例的文章就介绍到这了,更多相关SpringBoot返回html界面内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
