JSESSIONID in @{/}

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

JSESSIONID in @{/}

bdavid
I'm getting the root context path and assigning it to a JavaScript variable so that value can be used in JavaScript.

        <script th:inline="javascript"> var contextPath = /*[[@{/}]]*/ ''; </script>

Browsers under certain conditions will put JSESSIONID in the URL, and the JSESSION is included in the value returned by @{/}. I don't want to JSESSIONID to be included. My fix right now is an extra JavaScript call to remove JSESSIONID. Is there a better way? Thanks.
Reply | Threaded
Open this post in threaded view
|

Re: JSESSIONID in @{/}

Zemi
Administrator
You can get the Context Path using the IWebContext interface, like
    #ctx.contextPath

Try
    <script th:inline="javascript"> var contextPath = /*[[#ctx.contextPath]]*/ ''; </script>
Reply | Threaded
Open this post in threaded view
|

Re: JSESSIONID in @{/}

bdavid
I'm using the SpringStandard Dialect. How do I access IWebContext? I saw examples on the Standard Dialect but not on SpringStandard. Thanks Zemi.

Reply | Threaded
Open this post in threaded view
|

Re: JSESSIONID in @{/}

Zemi
Administrator
Sorry, the above code is incorrect, I typed it on the fly. Try this one:

        < script th:inline="javascript" >
              var contextPath = /*[[${#ctx.contextPath}]]*/ '';
        < /script >

Additionally, I've found that it does not work if the whole code is in a single line, I'll file it on the issue tracker.

Reply | Threaded
Open this post in threaded view
|

Re: JSESSIONID in @{/}

Zemi
Administrator
> I saw examples on the Standard Dialect but not on SpringStandard.

By the way, most (if not all) examples should work with the SpringStandard dialect, try them without modifications. If you have problems, you always could request help from this forums.



Reply | Threaded
Open this post in threaded view
|

Re: JSESSIONID in @{/}

bdavid
In reply to this post by Zemi
Works now. Thanks Zemi!

I don't think the single line issue you mentioned is a bug. I do think this is a gotcha when it comes to working with Thymeleaf. The docs says:

Thymeleaf will execute the expression and insert the result, but it will also remove all the code in the line after the inline expression itself (the part that is executed when displayed statically).
Reply | Threaded
Open this post in threaded view
|

Re: JSESSIONID in @{/}

danielfernandez
Administrator
In reply to this post by bdavid
Hi

First of all: please note that the "contextPath" attribute in #ctx will be deprecated in 1.1.2, so I do not recommend using that option.

As for the reasons JSESSIONID is added: the aim of the @{...} syntax is rewriting URLs so that they can be used as a part of a working web application, and this includes adding the contextPath to them, and also the JSESSIONID.

Before adding the JSESSIONID, nevertheless, the HttpServletRequest is asked whether the current session ID can be obtained via cookies or not. If it cannot be --or if it is not known yet whether it can be--, the JESSIONID fragment is added to the URL (if we already know cookies are working, it will not be added).

What I mean is: adding this ID is a core part of the URL rewriting process in order for it to work properly, so there is no way to avoid it.

BUT: you can take a different path. Zemi has already gone in the right direction, but as I said #ctx.contextPath is just about to be deprecated. Instead, since 1.1.2 you will have the possibility of accessing the entire HttpServletRequest object with #httpServletRequest, so you will able to access:

#httpServletRequest.serverName
#httpServletRequest.serverPort
#httpServletRequest.contextPath
etc.

...and this way you'll be able to reconstruct your URL up to the exact point you want, in the same way you'd do it in usual Java code.

Latest snapshot of 1.1.2, which you can use as version 1.1.2-SNAPSHOT, already includes this feature. You can learn how to use a snapshot version here: http://www.thymeleaf.org/faq.html#use-snapshot

Regards,
Daniel.
Reply | Threaded
Open this post in threaded view
|

Re: JSESSIONID in @{/}

bdavid
Got it. I'll change the code when I move to 1.1.2. Thanks Daniel.