No end tag in Thymeleaf template using Spring Tool

2019-08-23 08:57发布

I've generated a Spring Boot web application using Spring Initializr, using embedded Tomcat + Thymeleaf template engine. I have this Thymeleaf template

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">

<body>

    <div th:fragment="common-navbar" class="navbar navbar-inverse navbar-fixed-top" role="navigation">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
            </div>
            <div class="collapse navbar-collapse">
                <ul class="nav navbar-nav">
                    <li class="active"><a th:text="#{navbar.home.text}" href="/"></a></li>
                    <li><a th:text="#{navbar.about.text}" href="/about"></a></li>
                    <li><a th:text="#{navbar.contact.text}" href="/contact"></a></li>
                </ul>

                <ul class="nav navbar-nav navbar-right">
                    <li th:if="${#authorization.expression('!isAuthenticated()')}">
                        <a th:href="@{/login}" th:text="#{navbar.login.text}" />
                    </li>
                    <li th:if="${#authorization.expression('isAuthenticated()')}">
                        <form id="f" th:action="@{/logout}" method="post" role="form" class="navbar-form">
                            <button type="submit" th:text="#{navbar.logout.text}" class="btn btn-primary" />
                        </form>
                    </li>
                </ul>
            </div><!--/.nav-collapse -->
        </div>
    </div>

</body>
</html>

But I have 2 warnings No end tag (</a>). & No end tag (</button>).

2条回答
beautiful°
2楼-- · 2019-08-23 09:45

Thymeleaf prevents rendering no-valid HTML. What you are doing, using a and button tags with self enclosing, is unvalid according to HTML5 standards.

You can check validation of HTML codes on W3 validator page : https://validator.w3.org

Try an HTML code with self enclosed a and button tags and see the result.

Thymeleaf is not that much tough with validation of HTML codes, though. You see W3 says a missing title attribute on a button element is an error. But thymeleaf doesn't give an error on that.

Nevertheless; it is important for Thymeleaf that your HTML code shouldn't have missing enclosing tags when it's required.

查看更多
【Aperson】
3楼-- · 2019-08-23 09:54

If you use spring-boot-starter-thymeleaf dependency, you should add

<properties>
    <thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
    <thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>
</properties>

to your pom.xml.

This forces maven to use Thymeleaf 3. The default Thymeleaf 2 doesn't support pure HTML5.

More informations here: https://docs.spring.io/spring-boot/docs/current/reference/html/howto-spring-mvc.html#howto-use-thymeleaf-3

查看更多
登录 后发表回答