Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access application.properties value in thymeleaf template

Tags:

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

Instead of actual value I am getting the same text. Can someone please help me on this. Appreciate your help.

like image 463
Java_the_great Avatar asked May 12 '19 18:05

Java_the_great


People also ask

How do you get session value in Thymeleaf?

Using session Object${session.name} will return the value of the name attribute stored in the current session. If no attribute with the specified name is found in the session, it will return null .

How do I pass the selected dropdown value to a controller in Thymeleaf?

You just change @RequestParam to @Valid in your controller then change your select name to "task" in Thymeleaf and it should be into the form wrapper.

How do you assign a value to a variable in Thymeleaf?

We can use the th:with attribute to declare local variables in Thymeleaf templates. A local variable in Thymeleaf is only available for evaluation on all children inside the bounds of the HTML tag that declares it.


2 Answers

You have to use

${@environment.getProperty('css.specific.name')} this will fetch css.specific.name property from the application.properties file.

like image 71
Ashwini Jha Avatar answered Sep 20 '22 22:09

Ashwini Jha


Map your prop value in your controller, and call it directly from Thymeleaf template.

@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;
    }
}

In your html normally call it like this

<html>
    <body>
        <span th:text="#{urlValue}"></span>
    </body>
</html>
like image 34
Mohamed Sweelam Avatar answered Sep 17 '22 22:09

Mohamed Sweelam