0


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";
    }
标签: spring boot java spring

本文转载自: https://blog.csdn.net/m0_62567916/article/details/132481013
版权归原作者 @ljn 所有, 如有侵权,请联系我们删除。

“SpringBoot 怎么返回html界面”的评论:

还没有评论