What is the correct syntax for location.href

时间:2019-04-17 00:12:24

标签: javascript spring-boot thymeleaf

I've been trying to figure out how to pass a url to a spring controller method through a button via th:onclick. I've tried many different variations of these and can not figure out how many ' I am supposed to have or if I am escaping things correctly. Can someone please explain why this isn't working/what the correct way to use location.href is?

currently this is the error: Could not parse as expression: "'window.location.href=\'/removeContact''"

thank you!!

 <button type="button" th:onclick="'window.location.href=\'/removeContact''" name="removeContact" th:value="${stat.index}" class="btn btn-danger trashContact"><span class="fa fa-trash"></span></button>

1 个答案:

答案 0 :(得分:1)

${..} is variable expressions in Thymeleaf and is also spel.

So use two single quotes in spel expression for escape single quote.

th:onclick="'window.location.href=${'''/removeContact'''}'"

And use back slash and single quote in Thymeleaf standard expression text literal.

th:onclick="'window.location.href=\'/removeContact\''"