带变量的Thymeleaf内联javascript表达式
我将Spring Boot与Thymeleaf一起使用,我知道我可以通过以下方式在javascript中使用控制器中的变量:带变量的Thymeleaf内联javascript表达式,javascript,spring-boot,thymeleaf,Javascript,Spring Boot,Thymeleaf,我将Spring Boot与Thymeleaf一起使用,我知道我可以通过以下方式在javascript中使用控制器中的变量: <script th:inline="javascript"> /*<![CDATA[*/ var username = [[${#authentication.principal.person.isSubscribedTo("random string")}]]; /*]]>*/ </script> /**/ 现在我尝试在C
<script th:inline="javascript">
/*<![CDATA[*/
var username = [[${#authentication.principal.person.isSubscribedTo("random string")}]];
/*]]>*/
</script>
/**/
现在我尝试在CDATA注释之外使用一个局部变量,如下所示。我希望我能在这个方法中使用它
var randomString = "can i use this?";
/*<![CDATA[*/
var username = [[${#authentication.principal.person.isSubscribedTo(randomString)}]];
/*]]>*/
var randomString=“我能用这个吗?”;
/**/
这不起作用,我无法测试它,因为我的调试器无法获取该方法,并且不会返回任何错误
如何在thymeleaf javascript表达式中使用本地javascript变量?您对何时何地发生的事情感到有点困惑 首先,服务器使用Thymeleaf为页面生成HTML和动态Javascript。在这个过程中,正如您所说的,Thymeleaf可以在SpringBean在服务器上运行时调用它 然后,一旦HTML和动态Javascript被发送到浏览器,它就会在客户端运行Javascript 唯一真正的方法是:
randomString
我认为您必须将变量
randomString
写入CDATA部分,就在您的username
变量之前。在[CDATA]标记中初始化变量没有害处。:)