Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
156 views
in Technique[技术] by (71.8m points)

java - Execute managebean method from javascript onload event

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.

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

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In theory the following should do it.

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

with

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>

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>

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>

The <h:outputScript target="body"> renders the <script> in the end of the <body>. The upcoming OmniFaces 2.2 will remove this need by new autorun attribute.

<h:body>
    <h:form>
        <o:commandScript name="onload" action="#{bean.onload}" autorun="true" />
    </h:form>
</h:body>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...