Access application.properties value in thymeleaf template

Jav*_*eat 4 thymeleaf

I have one of my spring boot application and inside my application.properties there is one of the property is url=myurl.net. In the same application I have one thyme leaf html template. I wanted to get the url value into that template. I am using the following code inside the thymeleaf html template <font face=arial size=2 > access the url : </font> ${environment.getProperty(‘url’)}

Output I am getting :

access the url : $(environment.getProperty(‘url’)}

Output I am expecting:

access the url : myurl.net

我得到的不是实际值,而是相同的文本。有人可以帮我吗 感谢你的帮助。

Ash*_*Jha 27

你必须使用

${@environment.getProperty('css.specific.name')}这将从文件中获取css.specific.name属性application.properties


Moh*_*lam 5

在控制器中映射您的prop值,然后直接从Thymeleaf调用它。

@Controller
public class XController {

    @Value("${pcn.app.url}")
    private String url;     // Directly instead of using envireonment

    @RequestMapping(value = "form-show", method = RequestMethod.GET)
    public ModelAndView showForm() {
        ModelAndView model = new ModelAndView();
        model.setViewName("your-view");

        // The key which will look in your HTML with.
        model.addObject("urlValue", url);
        return model;
    }
}
Run Code Online (Sandbox Code Playgroud)

在您的html中通常这样称呼它

<html>
    <body>
        <span th:text="#{urlValue}"></span>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)