Execute managebean method from javascript onload event

转载 2015年11月20日 22:56:25

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>

相关文章推荐

Unable to execute dex: method ID not in [0, 0xffff]: 65536

Unable to execute dex: method ID not in [0, 0xffff]: 65536 方法数超过65536

Unable to execute dex: method ID not in [0, 0xffff]: 65536问题及解决办法

目录(?)[+] 預防 Android Dex 64k Method Size Limit 08 September 2014 如果你有 Android App 持續開發一年以上,那...
  • asdf717
  • asdf717
  • 2016年05月24日 11:17
  • 770

Android应用打破65K方法数限制Conversion to Dalvik format failed: Unable to execute dex: method ID not in 65536

Android应用打破65K方法数限制近日,Android Developers在Google+上宣布了新的Multidex支持库,为方法总数超过65K的Android应用提供了官方支持。如果你是一名...

备份Mysql数据库时,报:Couldn't execute 'show fields from的一种问题解决方案

因为数据库的表出现问题了,通过命令:     myisamchk -r -q *.MYI     执行了修复,可是通过mysqldump数据的时候,老是报Couldn't execute 'sho...

ERP SOFTWARE FROM SAP A FOUNDATION TO EXECUTE YOUR BUSINESS STRATEGY

ERP SOFTWARE FROM SAP A FOUNDATION TO EXECUTE YOUR BUSINESS STRATEGY From: http://www.sap.co...

document.ready和onload的区别 ——JavaScript文档加载完成事件

document.ready和onload的区别 ——JavaScript文档加载完成事件页面加载完成有两种事件 一是ready,表示文档结构已经加载完成(不包含图片等非文字媒体文件) 二是on...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Execute managebean method from javascript onload event
举报原因:
原因补充:

(最多只允许输入30个字)