Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在thymelaef中获取上下文路径_Javascript_Spring Boot_Thymeleaf - Fatal编程技术网

Javascript 如何在thymelaef中获取上下文路径

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>

我用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>

编辑:


还有其他解决方案吗?

应用程序中。属性

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值不会更改。