Javascript 如何在thymelaef中获取上下文路径
我用Thymeleaf模板开发Spring Boot应用程序。我想在下面的代码中添加前缀(服务器上下文路径),比如Javascript 如何在thymelaef中获取上下文路径,javascript,spring-boot,thymeleaf,Javascript,Spring Boot,Thymeleaf,我用Thymeleaf模板开发Spring Boot应用程序。我想在下面的代码中添加前缀(服务器上下文路径),比如'serverContext/user/'+user.id。我试图将$更改为@,但user.id已转换为字符串 多谢各位 <tr th:each="user : ${users}"> <td><a th:href="${ '/user/' + user.id}">View</a></td> </tr>
'serverContext/user/'+user.id
。我试图将$更改为@,但user.id
已转换为字符串
多谢各位
<tr th:each="user : ${users}">
<td><a th:href="${ '/user/' + user.id}">View</a></td>
</tr>
编辑:由
还有其他解决方案吗?在应用程序中。属性:
server.context-path=/test
服务类别:
@Component("helperService")
public class HelperService {
@Value("${server.context-path}")
private String contextPath;
public String getContextPath(){
return contextPath;
}
}
<span th:text="${@helperService.getContextPath()}"></span>
Html文件:
@Component("helperService")
public class HelperService {
@Value("${server.context-path}")
private String contextPath;
public String getContextPath(){
return contextPath;
}
}
<span th:text="${@helperService.getContextPath()}"></span>
的答案有效,但提供了一个不必要的附加bean。
只需插入ServerProperties
并调用getContextPath()
此bean作为@ConfigurationProperties
提供,因此已经可用。看
但是这解决了您的问题,但是如果您不使用绝对路径,您的问题就不是问题。为什么不使用这样的相对路径:
<td><a th:href="${ 'user/' + user.id}">View</a></td>
等等。不需要知道上下文路径。我将web应用作为war文件部署在Tomcat上,并使用context.xml文件设置上下文路径。当我将路径值从更改为时,href值不会更改。