Dun*_*ken 21 javascript spring-mvc thymeleaf
我已经搜索了类似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>
Run Code Online (Sandbox Code Playgroud)
那么调用Thymeleaf上下文路径的语法是什么?
geo*_*and 40
在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>
Run Code Online (Sandbox Code Playgroud)
本/*[[- ]]*/语法使用Thymeleaf评估通过的Javascript使用的变量,而不会破坏脚本如果要静态加载的地方.有关更多详细信息,请查看此部分文档
在Thymeleaf中使用$ {#httpSerletRequest.getContextPath()}来编写元元素中的上下文路径:
<meta name="ctx" th:content="${#httpServletRequest.getContextPath()}" />
Run Code Online (Sandbox Code Playgroud)
在jQuery中,使用$ .ajaxPrefilter()来预先添加所有jQuery AJAX请求的上下文路径:
var _ctx = $("meta[name='_ctx']").attr("content");
// Prepend context path to all jQuery AJAX requests
$.ajaxPrefilter(function( options, originalOptions, jqXHR ) {
if (!options.crossDomain) {
options.url = _ctx + options.url;
}
});
Run Code Online (Sandbox Code Playgroud)
以防万一其他人偶然发现了这个问题,寻找我原来一直在想的...在Thymeleaf页面内为页面的根设置上下文路径变量以继承到外部JQuery页面。这就是它对我的工作原理...与上面的空白一样...
使用JSP的旧方法
<script >var contextRoot = "${pageContext.request.contextPath}"; </script>
Run Code Online (Sandbox Code Playgroud)
Thymeleaf的新方法
<script th:inline="javascript"> var contextRoot = /*[[@{/}]]*/ ''; </script>
Run Code Online (Sandbox Code Playgroud)
以及带有更多信息的链接... http://forum.thymeleaf.org/JSESSIONID-in-td3386826.html
(同样取决于IDE,我将脚本设置在两行以上,而不是代码号的同一行。)
| 归档时间: |
|
| 查看次数: |
26579 次 |
| 最近记录: |