【问题标题】:To pass JSF(primefaces) managed bean variable to javascript function将 JSF(primefaces) 托管 bean 变量传递给 javascript 函数
【发布时间】:2014-12-23 22:25:16
【问题描述】:

我有一个 JSF/primefaces 托管 bean。我正在尝试将托管 bean 变量传递给同一个托管 bean 中的 JavaScript 函数,但我没有成功。有人可以帮我完成这项任务吗?

我的托管 bean 代码 sn-p

reportName=report[0];
    url="http://cvgapp42q/Reports/Pages/Report.aspx?ItemPath=%2fLPSR%2f"+reportName;
    try {

        RequestContext.getCurrentInstance().execute("window.open('#{repBean.url}')");
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

url 是我试图在 javascript 函数 window.open()

中使用表达式 #{repBean.url} 引用的变量

提前致谢

【问题讨论】:

    标签: javascript jsf-2 primefaces


    【解决方案1】:

    使用

    @ManagedProperty(value="#{repBean}")
    RepBean repBean
    

    在你的方法中

    try {
        RequestContext.getCurrentInstance().execute("window.open('"+repBean.getUrl()+"')");
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    

    【讨论】:

    • ,感谢回复。repBean.getUrl() 获取当前页面的 url。但我正在打开一个新的 url cvgapp42q/Reports/Pages/Report.aspx..... 如问题中所述。我只是想知道如何将保存 url 值的变量 url 传递给 js 函数,以便可以检测到该值并打开新网页。
    • 我不知道我是否真的明白你想要什么(可能是因为我的英语不好)。如果你只想打开url 你可以使用`RequestContext.getCurrentInstance().execute("window.open('"+url+"')");
    【解决方案2】:

    只需将您的 javascript 代码放在页面上的面板中即可:

    <h:panelGroup id="scriptPanel">
        <script>
            window.open('#{repBean.url}');
        </script>
    </h:panelGroup>
    

    并在您的 ajax 请求后更新此面板,在相关的 primefaces 组件上使用 update="scriptPanel"

    【讨论】:

      猜你喜欢
      • 2017-02-07
      • 1970-01-01
      • 2015-12-02
      • 2015-01-07
      • 2012-03-03
      • 2019-09-27
      相关资源
      最近更新 更多