thymeleaf标签html5中无效,解析springboot使用thymeleaf时报html没有结束标签

在使用springboot的过程中,如果使用thymeleaf作为模板文件,则要求HTML格式必须为严格的html5格式,必须有结束标签,否则会报错!解决办法如下:

1、你可以使用严格的标签,也就是每个标签都有结束标签,这种可能比较麻烦

2、在application.properties中增加spring.thymeleaf.mode=LEGACYHTML5,即声明thymeleaf使用非严 格的html。启动之后访问页面会报如下错误:

org.thymeleaf.exceptions.ConfigurationException: Cannot perform conversion to XML from legacy HTML: The nekoHTML library is not in classpath. nekoHTML 1.9.15 or newer is required for processing templates in "LEGACYHTML5" mode [http://nekohtml.sourceforge.net]. Maven spec: "net.sourceforge.nekohtml::nekohtml::1.9.15". IMPORTANT: DO NOT use versions of nekoHTML older than 1.9.15.

at org.thymeleaf.templateparser.html.AbstractHtmlTemplateParser.parseTemplate(AbstractHtmlTemplateParser.java:90) ~[thymeleaf-2.1.5.RELEASE.jar:2.1.5.RELEASE]

at org.thymeleaf.TemplateRepository.getTemplate(TemplateRepository.java:278) ~[thymeleaf-2.1.5.RELEASE.jar:2.1.5.RELEASE]

at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1104) ~[thymeleaf-2.1.5.RELEASE.jar:2.1.5.RELEASE]

at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1060) ~[thymeleaf-2.1.5.RELEASE.jar:2.1.5.RELEASE]

上面的异常已经说的很清楚了,需要依赖nekoHTML 1.9.15 or newer的版本。maven依赖如下

net.sourceforge.nekohtml

nekohtml

1.9.22

现在就可以正常访问了。

以上就是解析springboot使用thymeleaf时报html没有结束标签的详细内容,更多请html教程其它相关文章!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot使用Thymeleaf模板引擎访问静态HTML的过程如下: 1. 在Spring Boot工程创建一个静态HTML文件,例如index.html。 2. 在application.properties文件添加以下配置: ``` spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html ``` 这个配置告诉Thymeleaf模板引擎,它应该在classpath:/templates/目录下查找HTML模板文件,后缀为.html。 3. 创建一个Controller,用于处理请求并将数据传递给模板: ```java @Controller public class HomeController { @GetMapping("/") public String home(Model model) { model.addAttribute("message", "Hello, world!"); return "index"; } } ``` 这个Controller处理根路径的GET请求,并将一个名为message的属性添加到Model。然后,它返回index作为视图的名称。 4. 在index.html使用Thymeleaf模板引擎来呈现message属性: ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Home</title> </head> <body> <h1 th:text="${message}"></h1> </body> </html> ``` 这个HTML文件使用Thymeleaf的th:text属性来呈现message属性。 5. 启动Spring Boot应用程序,并访问http://localhost:8080/,应该看到Hello, world!。 注意:在上述步骤,我们将HTML文件放在了classpath:/templates/目录下,这是因为Thymeleaf默认会在这个目录下查找模板文件。如果你想将HTML文件放在其他地方,可以在application.properties文件通过配置spring.thymeleaf.prefix属性来指定模板文件的路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值