获取Thymeleaf $ {pageContext.request.contextPath}的语法是什么


问题内容

我一直在努力寻找像JSTL这样的语法${pageContext.request.contextPath}

我做了一个javascript代码来更改表单上的action属性以调用spring控制器上的edit方法,因此,问题是下面的代码在没有先调用Context的情况下无法工作喜欢${pageContext.request.contextPath}/edit.html

<script th:inline="javascript">
    function edit() {
        document.getElementById("user_form").action = "/edit.html";
    }
</script>

那么调用 Thymeleaf上下文路径 的语法是什么?


问题答案:

在Thymeleaf中,相当于JSP的${pageContext.request.contextPath}/edit.html@{/edit.html}

查看Thymeleaf文档的部分以获取更多详细信息

您的情况是:

<script th:inline="javascript">
    function edit() {
        var link = /*[[@{/edit.html}]]*/ 'test';
        document.getElementById("user_form").action = link;
    }
</script>

/*[[-
]]*/语法使用Thymeleaf评估通过的Javascript使用的变量,而不会破坏脚本如果将静态加载地方。查看文档的部分以获取更多详细信息