项目中使用模板引擎thymeleaf,遇到动态传参问题,使用th:attr来设置属性的值,现进行汇总,有问题及时讨论:
一、事件绑定JS且带参数
1、th:onclick示例:
th:onclick="'doSendMsg("'+${userInfo.mobile}+'")'"
th:onclick="'javascript:doSendMsg(\''+${userInfo.mobile}+'\',\''+${userInfo.name}+'\')'"
二、属性动态传参
用“+”符号,也可以用“|”符号
1、th:id示例:
th:id="'my'+${userInfo.name}"
2、th:text示例:
th:text="'早上好,'+${userInfo.name}"
th:text="|早上好, ${userInfo.name}|"
3、th:src示例:
<img th:src="@{/img/research/{filename}(filename=${research.filename})}">
4、th:action示例:
<form th:action="@{'/userAction/'+${userInfo.uid}}"></form>
<form th:action="@{/userAction/{uid}(uid=${userInfo.uid})}"></form>
5、th:href示例:
<a th:href="@{'https://'+${url.urlAddr}}" th:text="${url.urlName}"></a>