Html 如何在属性文件中存储URL,并使用Spring和Thymeleaf在模板中访问URL
我正在尝试使用Spring Boot和Thymeleaf创建一个简单的导航栏,其中包含指向其他网站/服务器的链接。我想将这些URL存储在我的“application.properties”中,并通过th:href访问它们。当我尝试访问它们时,它不会将我重定向到此URL 以下是HTML:Html 如何在属性文件中存储URL,并使用Spring和Thymeleaf在模板中访问URL,html,spring,spring-boot,thymeleaf,Html,Spring,Spring Boot,Thymeleaf,我正在尝试使用Spring Boot和Thymeleaf创建一个简单的导航栏,其中包含指向其他网站/服务器的链接。我想将这些URL存储在我的“application.properties”中,并通过th:href访问它们。当我尝试访问它们时,它不会将我重定向到此URL 以下是HTML: <li class="dropdown-submenu"> <a tabindex="-1" href="#">Menu</a> <ul class="d
<li class="dropdown-submenu">
<a tabindex="-1" href="#">Menu</a>
<ul class="dropdown-menu">
<li><a th:href="#{foo.bar}">Selection 1</a></li>
/#{foo.bar}应该指向MessageSource中的一个值,该值不由配置(即Spring Boot中的application.properties)填充,而是查看MessageSource文件(即Spring Boot中的messages.properties)。这将使模板保持不变
否则,如果配置中有foo.bar,并且不希望复制该属性,则可以像这样注入foo.bar
//在控制器类中
@Value("${foo.bar}")
String myUrl;
@RequestMapping("/mine")
public ModelAndView viewWithFooBarLink() {
ModelAndView mav = new ModelAndView("templateWithFooBarReference"):
mav.put("linkRef", myUrl);
return mav;
}
你的观点
<li class="dropdown-submenu">
<a tabindex="-1" href="#">Menu</a>
<ul class="dropdown-menu">
<li><a th:href="${linkRef}">Selection 1</a></li>
这是一个很好的参考资料。
事实证明,这种符号是有效的:
<li class="dropdown-submenu">
<a tabindex="-1" href="#">Menu</a>
<ul class="dropdown-menu">
<li><a th:href="@{${@environment.getProperty('foo.bar')}}>Selection 1</a></li>
也是我今天工作的${@environment.getProperty('foo.bar')}
。
<li class="dropdown-submenu">
<a tabindex="-1" href="#">Menu</a>
<ul class="dropdown-menu">
<li><a th:href="@{${@environment.getProperty('foo.bar')}}>Selection 1</a></li>