java - setTemplateMode ("LEGACYHTML5") 不适用于 thymeleaf-spring-4

标签 java html spring maven thymeleaf

我有一个简单的 HTML5 登录页面:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
    <body>
       <form th:action="@{/login}" method="post">
           <fieldset style="text-align:center;">
                <input type="text" id="username" name="username" autofocus>
                <input type="password" id="password" name="password">
                <input type="submit" name="submit" value="Login">
           </fieldset>
       </form>
    </body>
</html>

在我的 templateResolver 配置中,我将其设置为“LEGACYHTML5”,如下所示:

@EnableAutoConfiguration
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    @Bean public ViewResolver viewResolver() {
        ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
        templateResolver.setTemplateMode("LEGACYHTML5");
        templateResolver.setPrefix("templates/");
        templateResolver.setSuffix(".html");
        SpringTemplateEngine engine = new SpringTemplateEngine();
        engine.setTemplateResolver(templateResolver);

        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
        viewResolver.setTemplateEngine(engine);
        return viewResolver;
    }

}

在 Maven 上加载 thymeleaf-spring-3 时效果很好,但现在我已将其更改为 thymeleaf-spring-4,但出现解析错误:

org.thymeleaf.exceptions.TemplateInputException: Exception parsing document: template="login", line 35 - column 39
    at org.thymeleaf.templateparser.xmlsax.AbstractNonValidatingSAXTemplateParser.parseTemplateUsingPool(AbstractNonValidatingSAXTemplateParser.java:166)
    at org.thymeleaf.templateparser.xmlsax.AbstractNonValidatingSAXTemplateParser.parseTemplate(AbstractNonValidatingSAXTemplateParser.java:116)
    at org.thymeleaf.TemplateRepository.getTemplate(TemplateRepository.java:278)
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1104)
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1060)
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1011)
    at org.thymeleaf.spring4.view.ThymeleafView.renderFragment(ThymeleafView.java:335)
    at org.thymeleaf.spring4.view.ThymeleafView.render(ThymeleafView.java:190)
...
Caused by: org.xml.sax.SAXParseException: The element type "input" must be terminated by the matching end-tag "</input>".
    at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanEndElement(Unknown Source)
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
...

澄清一下,第一个错误指向输入元素上的 autofocus 属性。我也试过将 templateMode 设置为“HTML5”,但没有结果。

从 thymeleaf-spring-3 到 thymeleaf-spring-4 有什么变化吗?我错过了什么吗?有没有我不知道的可以使用的另一种模式(thymeleaf-spring-4 的文档不是很清楚)?

最佳答案

spring.thymeleaf.mode=LEGACYHTML5 添加到 application.properties 文件,而不是使用基于 Java 的配置,解决了这个问题。 这需要 NekoHTML类路径中的版本 1.9.15 或更高版本。

关于java - setTemplateMode ("LEGACYHTML5") 不适用于 thymeleaf-spring-4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31857943/

相关文章:

java - 尝试实现 Comparable 时出错

html - 使用 css 在 View 中 move 元素

javascript - jQuery - 如何在条件为真时取消表单提交以防止默认

jquery - 使用 CSS3 动画无限滚动带环绕的 Div

java - 如何从 Swagger UI 隐藏存储库 Controller

java - 带有 Keycloak 的 Spring 应用程序返回 401 错误

java - ThrowableCaptor 可在 Eclipse 中运行,但不能在 Netbeans 中运行

java - 解码时找不到类 : android. support.v7.widget.Toolbar$SavedState

java - spring boot 集成测试 - 数据库未使用 @WebMvcTest Autowiring

java - 如何使用Jboss7设置hibernate3?