简体   繁体   中英

Thymeleaf construct URL with dynamic values from messages.properties file

I am trying to construct dynamic url using thymeleaf using messages.properties file but is not working. I am trying to construct below URL by using content present in messages.properties file.

<a href="https://10.247.30.152/utility" target="_blank">Click Here</a>

messages.properties

dev.node=10.247.30.152
fs.node = 10.245.39.153

I want to use only IP's for different different href and rest of the content should remain as it is in href like

<a href="https://#{dev.node}/utility" target="_blank">Click Here</a>
<a href="https://#{fs.node}/utility" target="_blank">Click Here</a>

Using thymeleaf we can able to fetch data from messages.properties file and add it to href like this but its not working.

<a th:href="@{'https://'+ #{dev.node}+ '/utility'}" target="_blank">Click Here</a>

Any other way of doing it?

There are a couple of ways to handle this.

First, with very little modification to your templates, use ${@environment.getProperty('dev.node')} .

The second, and my preferred choice, is to add the property to the model in your controller, then you can just access the attribute in your template. Here's an example assuming your view is named someView.html

@Value("${dev.node}")
private String devNode;

@GetMapping
public String getView(ModelAndView modelAndView) {
    modelAndView.addObject("devnode", devNode);
    return "someView";
}

Then you can simply access the attribute using the ${} syntax, eg ${devnode}

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