如何使用 javascript 变量访问模型属性 [英] How do you access a model attribute with javascript variable

查看:29
本文介绍了如何使用 javascript 变量访问模型属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在春天向我的 ModelAndView 添加一个属性,然后我将它转发到我的 thymeleaf 视图.

I'm adding an attribute to my ModelAndView in spring and after this i forwarding it to my thymeleaf view.

在视图中我有以下代码:

In the view i have the following code:

<script th:inline="javascript">
        /*<![CDATA[*/
            var applicationName = /*[[${T(com.sample.constants.ApplicationConstants).MODEL_ATTR_COLLECTED_VALUES}]]*/ "Test";
            var collectedValueJson = [[${collectedValues}]];
            console.log(collectedUserJson);
        /*]]>*/
</script>

结果是

var applicationName = 'collectedValues';
var collectedUserJson = '[{\"givenname\":\"Muster\",\"surname\":\"Peter\"}]';

没关系.现在我的愿望是,我可以使用 var application 并使用此变量访问 modelattribute,但这不起作用.

That's fine. Now my wish for this is, that i can take the var application and access with this variable the modelattribute, but that's not working.

结果如下:

var tmp2 = ${applicationName};

另一个尝试是,我可以从第一次尝试使用语法 /*[[ ]]*/ 访问模型属性:

An other try was, that i have access to the modelattribute with the syntax /*[[ ]]*/ from the first try:

var applicationName = ${/*[[${T(com.sample.constants.ApplicationConstants).MODEL_ATTR_COLLECTED_VALUES}]]*/};

但结果将是:

var tmp = ${'collectedValues'

我不知道我可以尝试什么.

i have no idea what can i try.

还有其他建议吗?

提前致谢.

推荐答案

您无法访问模型属性,因此它们位于服务器端,并且在将 jsp 渲染为 HTML 时丢失了它们

there is no way you can access model atributes hence they are on server side and they where lost when rendering jsp into HTML

这篇关于如何使用 javascript 变量访问模型属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆