从javascript onload事件执行managebean方法 [英] Execute managebean method from javascript onload event
问题描述
如何从javascript发出更新< h:dataTable>
的ajax请求?我目前正在使用 @Postconstruct
加载初始数据,但这显着延迟了初始页面加载。
我是考虑使用 onload
事件< body>
HTML标记来触发请求并更新数据表。
在理论中,以下内容应该这样做。
< pre class =lang-html prettyprint-override>
< h:body>
< f:ajax event =loadlistener =#{bean.onload}/>
< / h:body>
with
public void onload(AjaxBehaviourEvent event){
// ... ...
}
但是,出于某种原因不支持此功能。我曾经发布过问题报告。
以下是有效的,但它本质上是一个黑客。
< H:头部>
< title> JSF 2.0 onload hack< / title>
< script>
window.onload = function(){
document.getElementById('hidden:link')。onclick();
}
< / script>
< / h:head>
< h:body>
< h:form id =hiddenstyle =display:none>
< h:commandLink id =link>
< f:ajax event =clicklistener =#{bean.onload}/>
< / h:commandLink>
< / h:form>
< / h:body>
如果您碰巧使用PrimeFaces,那么您可以使用其 < p:remoteCommand>
autoRun
设置为 true
。
< h体:>
< h:form>
< p:remoteCommand name =onloadaction =#{bean.onload}autoRun =true/>
< / h:form>
< / h:body>
或者如果您使用的是OmniFaces,那么您可以使用其< o:commandScript>
< h:body>
< h:form>
< o:commandScript name =onloadaction =#{bean.onload}/>
< h:outputScript target =body> onload()< / h:outputScript>
< / h:form>
< / h:body>
< h:outputScript target =body>
在< body>
的末尾呈现< script>
。即将推出的OmniFaces 2.2将通过新 autorun
属性。
< h:body>
< h:form>
< o:commandScript name =onloadaction =#{bean.onload}autorun =true/>
< / h:form>
< / h:body>
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.
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>
这篇关于从javascript onload事件执行managebean方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!