thymeleaf-接上一篇的mvc项目

          thymeleaf中在html页面如果想要使用js函数,而且想传入java对象中的值,首先th:表签必须要有,否则html页面是无法正确获取到相应的java数据的,其次thymeleaf中对字符串的处理也有很多种方式,最简单的就是可以使用+直接相加来拼凑,但是因为在html页面中和.java环境下写代码还是有区别的,所以在处理js函数的时候也是需要小心处理,下面的例子可以正确的调用js函数,并将后台传入的值送入js函数中:

 其中转义字符是双引号的写法。

 字符串和[[${*}]]thymelaf表达式是可以直接写在一起的,这样写并不需要th表签。

另外,thymeleaf中的th:if表签,在false的情况下,html元素其实是不存在的,就是说它和css中的display="none"是不一样的,当为false的情况下,所有以内的元素其实是相当于消失的,这种情况下在js中调用这里面的元素js会报错。

****************

2021年10月7日21点44分:

<th><input type="button" th:onclick="'submit2(&quot;'+${mongor.uuid}+'&quot;);'" value="选择"/></th>
当按照上面的形式的时候,是会报如下错误,这是因为thymeleaf防止注入攻击做了安全升级。因此当从对象中获取属性值进行填充的时候,需要使用[[]]进行获取值。

Only variable expressions returning numbers or booleans are allowed in this context, any other datatypes are not trusted in the context of this expression, including Strings or any other object that could be rendered as a text literal. A typical case is HTML attributes for event handlers (e.g. "onload"), in which textual data from variables should better be output to "data-*" attributes and then read from the event handler. (template: "title" - line 21, col 38)

改成:<th><input type="button" th:onclick="submit2([[${mongor.uuid}]])" value="选择"/></th> 

-----------------------

2021年10月27日11点16分

今天发现一样的错误

 我一直以为是我写js方法的时候又写错了,但是无论怎么改都是一样的报错,后来发现数据库中有一条数据很奇怪,这一条数据存在,但是每个字段的数据都是null,导致thymeleaf在解析对象属性值的时候,得到了一个空值,然后再往js方法中传值的过程中传入了null,然后就报了这样的错误,然后把那条错误的数据删除,或者对关键字段做判断即可解决此问题。

----------------------------
2021年10月28日 10点42分

上面的错误的解决办法也可以采用下面的办法

原文地址:https://www.cnblogs.com/YsirSun/p/15191534.html