如何获得百里香的网址

时间:2017-03-02 03:24:42

标签: thymeleaf

当前页面网址为http://localhost:8080/operator/list.html。此页面中的Ajax应为http://localhost:8080/operator/somerequest.html 如果ctx = "http://localhost:8080/operator/"然后ajax可以工作,但我想用百里香内置的符号替换它,那么如何做到这一点?我尝试了var ctx = [[${#httpServletRequest.getContextPath()}]]var ctx = [[@{/}]],甚至是这两个的总和,但没有......

<script th:inline="javascript">
    /*<![CDATA[*/
    var ctx = ?; // what thymeleaf annotation
    /*]]>*/
</script>

我已阅读this

1 个答案:

答案 0 :(得分:0)

在这种情况下,你真的应该使用相对网址。假设您的应用程序的上下文是/运算符,百万美元生成的URL应该是:

var url = [[@{/somerequest.html}]];

这将为您提供/operator/somerequest.html的正确网址。调用ajax(例如$.post(url))时,假定为localhost:8080。据我所知,thymeleaf没有提供任何能力来获取网址的“localhost:8080”。