What Is The Syntax To Get Thymeleaf ${pagecontext.request.contextpath}
Solution 1:
In Thymeleaf the equivalent of JSP's ${pageContext.request.contextPath}/edit.html
would be @{/edit.html}
Check out this part of the Thymeleaf documentation for more details
In your case you would write :
<scriptth:inline="javascript">functionedit() {
var link = /*[[@{/edit.html}]]*/'test';
document.getElementById("user_form").action = link;
}
</script>
The /*[[
- ]]*/
syntax is used by Thymeleaf to evaluate variables used by Javascript, without breaking the script if that where to be statically loaded. Check out this part of the documentation for more details
Solution 2:
My solution for Thymeleaf and jQuery is below.
Use ${#httpServletRequest.getContextPath()} in Thymeleaf to write the context path in the meta element:
<meta name="ctx" th:content="${#httpServletRequest.getContextPath()}" />
and in jQuery, use $.ajaxPrefilter() to prepend context path to all jQuery AJAX requests:
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;
}
});
Solution 3:
Just in case anybody else stumbles upon this question looking for what I originally had been...setting a context path variable for the root of the page inside the Thymeleaf page to carry over to an external JQuery page. Here is how it worked for me...same as above just left blank...
Old way with JSP
<script >var contextRoot = "${pageContext.request.contextPath}"; </script>
New way with Thymeleaf
<script th:inline="javascript"> var contextRoot = /*[[@{/}]]*/ ''; </script>
and a link with more information... http://forum.thymeleaf.org/JSESSIONID-in-td3386826.html
(also depending on the IDE, I set the script over two+ lines as opposed to the same line of the code number.)
Solution 4:
Try this:
varBASE_CONTEXT_PATH = $('meta[name=context-path]').attr("content");
BASE_CONTEXT_PATH = BASE_CONTEXT_PATH.substr(0, BASE_CONTEXT_PATH.length - 1);
<metaname="context-path"th:content="@{/}"/>
Post a Comment for "What Is The Syntax To Get Thymeleaf ${pagecontext.request.contextpath}"