JSF bean属性未在外部JavaScript文件中评估 [英] JSF bean property not evaluated in external JavaScript file

查看:98
本文介绍了JSF bean属性未在外部JavaScript文件中评估的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想从JavaScript中评估一个JSF bean属性,我发现如果JavaScript代码片段在xhtml文件中,它可以工作,但是当JavaScript代码片段在一个单独的js文件中时,它不起作用。

If I want to evaluate a JSF bean property from within JavaScript, I see that it works if the JavaScript snippet is inside the xhtml file, but doesn't work when the JavaScript snippet is in a separate js file.

所以,这有效:

index.xhtml

...
<h:body>
    <script type="text/javascript" src="resources/Javascript/jquery/jquery-1.7.2.js" />
    <script type="text/javascript" >
        $(document).ready(function() {
            alert('#{myBean.myProperty}');
        });
    </script>        
</h:body>

但这不会评估ManagedBean的属性:

But this doesn't evaluate the ManagedBean's property:

index.xhtml

...
<h:body>
    <script type="text/javascript" src="resources/Javascript/jquery/jquery-1.7.2.js" />
    <script type="text/javascript" src="resources/Javascript/MyJS.js" />
</h:body>

MyJS.js

$(document).ready(function() {
    alert('#{myBean.myProperty}');
});

在第二种情况下,警告框包含未评估的字符串# {myBean.myProperty}

In this second case, the alert box contains the not-evaluated string #{myBean.myProperty}

如何从外部js文件中使用它?

How can I make it work from the external js file?

推荐答案

感谢@ Al-Mothafar的建议,我终于通过以下方式解决了我的问题:

Thanks to the suggestion by @Al-Mothafar, I have finally solved my issue in the following way:

index.xhtml

...
<h:body>
    <script type="text/javascript" src="resources/Javascript/jquery/jquery-1.7.2.js" />
    <script type="text/javascript" src="resources/Javascript/MyJS.js" />
    <script type="text/javascript" >
        var myBeanProperty = '#{myBean.myProperty}';
    </script>        
</h:body>

MyJS.js

$(document).ready(function() {
    alert(myBeanProperty);
});

这篇关于JSF bean属性未在外部JavaScript文件中评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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