1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Thymeleafで開発中にHTML内のth属性にwarningがついてしまう

Last updated at Posted at 2020-03-07

現象

eclipseやSTSでSpring boot + Thymeleafで開発しているとき、HTML内のth属性にwarningがついてウザい。
warning.PNG

環境

Spring Tool Suite 3

原因

htmlの属性にxmlns:th="http://www.thymeleaf.org"を書き忘れている。

sample.html
<!DOCTYPE html>
<html>
  <head>
    <title>Hello</title>
    <meta charset="utf-8" />
  </head>
  <body>
    <h1>Hello SpringBoot Sample</h1>
    <h1 th:text="${message}"></h1>
  </body>
</html>

対策

パターン1:HTML属性にxmlnsを書く

HTMLタグのxmlns属性を書いて、th属性があることをエディタに知らせる。

sample.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
  <head>
    <title>Hello</title>
    <meta charset="utf-8" />
  </head>
  <body>
    <h1>Hello SpringBoot Sample</h1>
    <h1 th:text="${message}"></h1>
  </body>
</html>

パターン2:th属性ではなく、data属性形式で書く

th属性ではなくdata属性の記述方式で書く
HTML5対応したい場合はこちら。個人的にはこちらを押したい。

sample.html
<!DOCTYPE html>
<html>
  <head>
    <title>Hello</title>
    <meta charset="utf-8" />
  </head>
  <body>
    <h1>Hello SpringBoot Sample</h1>
    <h1 data-th-text="${message}"></h1>
  </body>
</html>
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?