Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
210 views
in Technique[技术] by (71.8m points)

html - Thymeleaf: Access Environment bean

I want to access the environment properties:

<h1 th:text="${@environment.getProperty('site.name1')}">
                                    <span th:text="${@environment.getProperty('site.name2')}"></span>
                                </h1>

but I don't get anything for site.name2 even it exists in application.property file

here my application.properties file:

spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.h2.console.settings.trace=false
spring.h2.console.settings.web-allow-others=false

site.name1=plats
site.name2=bruts


spring.messages.encoding=UTF-8
spring.thymeleaf.encoding=UTF-8

spring.servlet.multipart.max-file-size=1000MB
spring.servlet.multipart.max-request-size=1000MB
spring.servlet.multipart.enabled=true

server.port=8080

This is how it looks using:

<h1>Plats
<span class="muellerhoff">Bruts</span>
</h1>

enter image description here

and with:

    <h1>
<span th:text="${@environment.getProperty('site.name1')}"></span>
                                        <br/>
<span class="muellerhoff"  th:text="${@environment.getProperty('site.name2')}"></span>
</h1>

enter image description here

question from:https://stackoverflow.com/questions/65541130/thymeleaf-access-environment-bean

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The end goal is HTML as follows:

<h1>Plats
    <span class="muellerhoff">Bruts</span>
</h1>

You can use <th:block> in this case, to handle the name1 value. The block tag will not appear in the final HTML.

<h1> 
    <th:block th:text="${@environment.getProperty('site.name1')}"></th:block>
    <span class="muellerhoff" th:text="${@environment.getProperty('site.name2')}"></span>
</h1>

More info about the th:block tag can be found here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...