Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Syntax to get Thymeleaf ${pageContext.request.contextPath}

I have searched hard for a syntax like the JSTL one ${pageContext.request.contextPath},

I did made a javascript code to change the action attribute on the form to call the edit method on the spring controller so, the problem is the below code dont work without calling the Context first like ${pageContext.request.contextPath}/edit.html

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

so what is the Syntax to call Thymeleaf context path?

like image 224
Dunken Avatar asked Jun 16 '14 11:06

Dunken


People also ask

What is${ pageContext request contextPath in JSP?

${pageContext. request. contextPath} is an EL expression equivalent to the JSP expression <%= request. getContextPath() %> . It is recommended to use ${pageContext.

How to use pageContext request contextPath in JSP?

To get the context path we can utilize the pageContext , it is an implicit object that available on every JSP pages. From this object you can get access to various object such as: servletContext. session.

What is request getContextPath() in JSP?

request. getContextPath()- returns root path of your application, while ../ - returns parent directory of a file. You use request. getContextPath(), as it will always points to root of your application. If you were to move your jsp file from one directory to another, nothing needs to be changed.

How do I get the context path in Thymeleaf?

properties using @ConfigurationProperties with a new class for properties like AppProperties. java. You can then autowire this properties class in the class where you will be setting ThymeLeaf parameters and then set this parameter in Thymeleaf context and fetch it accordingly in the html file ..


3 Answers

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 :

<script th:inline="javascript">
    function edit() {
        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

like image 89
geoand Avatar answered Oct 28 '22 21:10

geoand


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;
    }
});
like image 36
Sutra Avatar answered Oct 28 '22 21:10

Sutra


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.)

like image 27
ClickerTweeker Avatar answered Oct 28 '22 22:10

ClickerTweeker