简体   繁体   中英

Thymeleaf insert text in html code?

I want to insert an Attribute into the html Code. I tried this, but it's not working:

<div id="${var}"> ... </div>

I think you know what I mean. The attribute 'var' should be the id. I didn't find a solution...

You just need to use the th:attr attribute. It is explained in the reference documentation 5.1:

Enter then the th:attr attribute, and its ability to change the value of attributes of the tags it is set in :

 <form action="subscribe.html" th:attr="action=@{/subscribe}"> <fieldset> <input type="text" name="email" /> <input type="submit" value="Subscribe!" th:attr="value=#{subscribe.submit}"/> </fieldset> </form> 

The concept is quite straightforward: th:attr simply takes an expression that assigns a value to an attribute. Having created the corresponding controller and messages files, the result of processing this file will be:

 <form action="/gtvg/subscribe"> <fieldset> <input type="text" name="email" /> <input type="submit" value="¡Suscríbe!"/> </fieldset> </form> 

用这个

<div th:attr="id=${var}"> ... </div>

Thymeleaf only evaluates attributes that are prefixed with th: . Here is a list of the attributes that are evaluated:

http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#setting-value-to-specific-attributes

In your case, th:id is already built in so you can simply do <div th:id="${var}"> ... </div> and it will work. th:attr , is used to define attributes that thymeleaf doesn't normally support.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM