从 javascript onload 事件执行 managebean 方法 [英] Execute managebean method from javascript onload event
问题描述
如何发出 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">
在 的末尾渲染
代码>.即将推出的 OmniFaces 2.2 将通过 新的
autorun>
属性消除这一需求.
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屋!