0
votes

I am a bit new to Thymeleaf, so apologies if this is a newb syntax mistake. I am trying to have Thymeleaf execute multiple statements in the same th:with attribute.

<div th:with="url = ${#httpServletRequest.getRequestURL()}, url = ${url.substring(0, url.indexOf('error'))}"></div>

But this is not the correct syntax.

org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as assignation sequence: "url = ${#httpServletRequest.getRequestURL()}, url = url.substring(0, url.indexOf('error'))" (error/404:11)

Is this possible, and if so what is the correct syntax for what I'm trying to accomplish?

EDIT: There was a syntax error in the part below that I have fixed, and now I'm getting a different error.

Tried breaking it into two, but then it appears that the url variable is not populated correctly in the SpringEL expression.

<div th:with="url = ${#httpServletRequest.getRequestURL()}">
    <div th:with="url = ${url.substring(0, url.indexOf('error'))}"></div>
</div>
org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as assignation sequence: "url = ${url.substring(0, url.indexOf('error')})" (error/404:12)

Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: -1
1
try without spaces maybe :) - ACV
what error are you getting? - ACV
I don't think there is anything wrong with your second example. If your url variable doesn't have the string "error" in it, it returns -1 which causes url.substring to fail with the error you are seeing. - Metroids

1 Answers

0
votes

I will refer to the second version.

http://www.tutorialspoint.com/java/java_string_indexof.htm

int indexOf(String str): Returns the index within this string of the first occurrence of the specified substring. If it does not occur as a substring, -1 is returned.

So i think that error did not occur.

Then you are trying to do

url.substring(0,-1)

and you get an error.