SpringBoot项目访问HTML页面

引入相关依赖

<!--支持跳转,springboot推荐使用thymeleaf模板引擎-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <!--指定themleaft版本-->
    <thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
    <thymeleaf-layout-dialect.version>2.0.5</thymeleaf-layout-dialect.version>
</properties>

增加springboot配置项

#thymeleaf模版前缀
spring.thymeleaf.prefix=classpath:/templates/

在 src/main/resources 目录下新建 static 目录和 templates 目录。 static存放静态文件,templates 存放静态页面(thymeleaf 模版)


在控制器中写明跳转模版方法


Handler访问映射地址跳转模版成功

总结这次遇到的问题:

1. Error resolving template template might not exist or might not be accessible

控制器方法返回的模版名称没有前缀/,可手动添加/或添加springboot配置项

2. org.xml.sax.SAXParseException: 元素类型 “link” 必须由匹配的结束标记 “” 终止,org.xml.sax.SAXParseException: 元素类型 “meta” 必须由匹配的结束标记 “” 终止

开发工具生成的html页面元素有的没有终止符/,thymeleaf模板引擎默认是Template modes:HTML5解析的,解析比较严格。

需要手动添加/或指定引入的thymeleaf版本号


猜你喜欢

转载自blog.csdn.net/s573626822/article/details/80853752