2016-08-12 62 views
0

我對Thymeleaf有點新,所以如果這是newb語法錯誤,請致歉。 我正試圖讓Thymeleaf在同一個th:with屬性中執行多個語句。評估Thymeleaf屬性中的多個表達式

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

但是這不是正確的語法。

org.thymeleaf.exceptions.TemplateProcessingException:無法解析爲賦值序列:「url = $ {#httpServletRequest.getRequestURL()},url = url.substring(0,url.indexOf('error'))」 (錯誤/ 404:11)

這是可能的,如果是這樣,我想完成什麼正確的語法?

編輯:在我已經修復的部分中出現了一個語法錯誤,現在我得到了一個不同的錯誤。

嘗試將它分成兩部分,但是看起來url變量未在SpringEL表達式中正確填充。

<div th:with="url = ${#httpServletRequest.getRequestURL()}"> 
    <div th:with="url = ${url.substring(0, url.indexOf('error'))}"></div> 
</div> 
org.thymeleaf.exceptions.TemplateProcessingException:無法解析作爲分配序列: 「URL = $ {url.substring(0,url.indexOf( '錯誤')})」(錯誤/ 404: 12)

引起:java.lang.StringIndexOutOfBoundsException:字符串索引超出範圍:-1
+0

嘗試不帶空格也許:) – ACV

+0

你會得到什麼錯誤? – ACV

+1

我不認爲你的第二個例子有什麼問題。如果您的url變量中沒有字符串「error」,則返回-1,這會導致** url.substring **失敗,並顯示錯誤信息。 – Metroids

回答

0

我將引用第二個版本。

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

int indexOf(String str):返回此字符串中指定的子第一次出現的中的索引。如果它不作爲子字符串出現,則返回-1

所以我認爲error沒有發生。

然後你正在嘗試做的

url.substring(0,-1) 

,你會得到一個錯誤。