从 javascript onload 事件执行 managebean 方法 [英] Execute managebean method from javascript onload event

查看:25
本文介绍了从 javascript onload 事件执行 managebean 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何发出 ajax 请求来更新来自 javascript 的 <h:dataTable>?我目前正在使用 @Postconstruct 加载初始数据,但这显着延迟了初始页面加载.

How can I make an ajax request that updates a <h:dataTable> from javascript? I am currently loading the initial data using @Postconstruct but that is significantly delaying the initial page load.

我正在考虑使用 HTML 标记的 onload 事件来触发请求并更新数据表.

I am thinking about using onload event of <body> HTML tag to fire the request and update the datatable.

推荐答案

理论中应该做到以下几点.

In theory the following should do it.

<h:body>
    <f:ajax event="load" listener="#{bean.onload}" />
</h:body>

public void onload(AjaxBehaviourEvent event) {
    // ...
}

但是,由于某种原因,这不受支持.我曾经发布过关于此的问题报告.

However, this is not supported for some reason. I've ever posted an issue report about that.

以下有效,但本质上是一种黑客行为.

The following works, but it's in essence a hack.

<h:head>
    <title>JSF 2.0 onload hack</title>
    <script>
        window.onload = function() {
            document.getElementById('hidden:link').onclick();
        }
    </script>
</h:head>
<h:body>
    <h:form id="hidden" style="display:none">
        <h:commandLink id="link">
            <f:ajax event="click" listener="#{bean.onload}" />
        </h:commandLink>
    </h:form>
</h:body>

如果你碰巧使用 PrimeFaces,那么你可以使用它的 <p:remoteCommand>autoRun 设置为 true.

If you happen to use PrimeFaces, then you can use its <p:remoteCommand> with autoRun set to true.

<h:body>
    <h:form>
        <p:remoteCommand name="onload" action="#{bean.onload}" autoRun="true" />
    </h:form>
</h:body>

或者,如果您使用的是 OmniFaces,那么您可以使用它的 <o:commandScript>

Or if you're using OmniFaces, then you can use its <o:commandScript>

<h:body>
    <h:form>
        <o:commandScript name="onload" action="#{bean.onload}" />
        <h:outputScript target="body">onload()</h:outputScript>
    </h:form>
</h:body>

<h:outputScript target="body"> 的末尾渲染

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