Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

spring boot - org.thymeleaf.exceptions.TemplateProcessingException: Only variable expressions returning numbers or booleans are allowed in this context

I have been using thymeleaf th:onclick attribute to call javascript function with parameters as below

th:onclick="|myFunction('${parameter1}')|"

But with thymeleaf 3.1.10 this has been removed. and they are suggesting to use th:data attribute.

I however found workaround on as below and both of them are working perfectly.

  1. th:attr="onclick=|myFunction('${parameter1}')|"
  2. th:onclick="@{myFunction('${parameter1}')}">

Now i am not sure if these workarounds are correct way to do things and if yes which one is the better way.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The first will work like you want -- however, you are bypassing the the security restriction and now your pages are vulnerable to javascript injection (which is the original reason this change was made).

The second one just plain doesn't work. It doesn't expand out the variable ${parameter1}, instead just encoding it as a url like this:

onclick="myFunction?$%7Bparameter1%7D"

You really should be doing it as shown on the page.

th:data-parameter1="${parameter1}" onclick="myFunction(this.getAttribute('data-parameter1'));"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...