如何调用使用本地JavaScript代码的HTML DOM事件JSF托管bean? [英] How to invoke a JSF managed bean on a HTML DOM event using native JavaScript?
问题描述
我需要执行一个JSF管理用ajax bean的操作方法中的HTML DOM 负荷
事件,类似jQuery的 $(文件)。就绪(函数(){$。阿贾克斯(...)})
。我只可以使用JSF这个项目所产生的的JavaScript。有没有办法做到在本地JSF?哪个事件,我可以使用或我可以使用的JSF AJAX功能?
I need to execute a JSF managed bean action method using ajax during HTML DOM load
event, similar to jQuery's $(document).ready(function() { $.ajax(...) })
. I can only use the JavaScript generated by JSF in this project. Is there a way to do it in native JSF? Which event can I use or which JSF ajax function can I use?
我使用的是JSF 2.0,Facelets的和PrimeFaces。
I'm using JSF 2.0, Facelets and PrimeFaces.
推荐答案
几个方面。
-
使用隐形式招(实际上,黑客被赋予了丑陋更好的写法)。
Use the "hidden form" trick (actually, "hack" is given the ugliness a better wording).
<h:outputScript target="body">
document.getElementById("form:button").onclick();
</h:outputScript>
<h:form id="form" style="display:none;">
<h:commandButton id="button" action="#{bean.onload}">
<f:ajax render=":results" />
</h:commandButton>
</h:form>
<h:panelGroup id="results">
...
</h:panelGroup>
注意&LT; H:outputScript目标=体与GT;
自动将&LT;脚本&GT;
在&LT结束;身体GT;
,因而 $(文件)。就绪()
或在window.onload
包装是不必要的。还要注意触发的onclick的重要性()
而不是点击()
的情况下&LT ; H:的commandButton&GT;
。该的onclick()
立即调用了的onclick
功能,而点击()
只是触发因素,不支持在IE的点击事件。
Note that <h:outputScript target="body">
automatically puts the <script>
in end of <body>
, thus a $(document).ready()
or window.onload
wrapper is unnecessary. Also note the importance of triggering onclick()
instead of click()
in case of <h:commandButton>
. The onclick()
immediately invokes the onclick
function while the click()
only triggers the "click" event on the element, which is not supported in IE.
正如你已经在使用PrimeFaces,只需使用其 &LT;电话号码:remoteCommand&GT;
,提供的 AUTORUN =真正的
。
As you're already using PrimeFaces, just use its <p:remoteCommand>
with autoRun="true"
.
<h:form>
<p:remoteCommand name="onload" action="#{bean.onload}" autoRun="true" update=":results" />
</h:form>
<h:panelGroup id="results">
...
</h:panelGroup>
然而,这并不使用JSF本地 jsf.ajax.request()
,而是使用PrimeFaces本地的jQuery(你知道,PrimeFaces是jQuery的/ UI之上的JSF组件库)。
This however doesn't use JSF native jsf.ajax.request()
, instead it uses PrimeFaces native jQuery (you know, PrimeFaces is a JSF component library on top of jQuery/UI).
创建一个自定义的 UIComponent
延伸 UICommand
并生成必要的JSF本地 jsf.ajax.request()
通话。你那么在本质上重塑&LT;电话号码:remoteCommand&GT;
来使用本地JSF AJAX来代替jQuery的。 JSF的工具库 OmniFaces 有一个&LT;○:commandScript&GT;
组件这正是这么做的。又见展示和<一href="https://github.com/omnifaces/omnifaces/blob/master/src/main/java/org/omnifaces/component/script/CommandScript.java"相对=nofollow>来源$ C $ C 。
Create a custom UIComponent
which extends UICommand
and generates the necessary JSF native jsf.ajax.request()
call. You're then in essence reinventing the <p:remoteCommand>
to use native JSF ajax instead of jQuery. The JSF utility library OmniFaces has a <o:commandScript>
component which does exactly that. See also the showcase and the source code.
<h:outputScript target="body">
onload();
</h:outputScript>
<h:form>
<o:commandScript name="onload" action="#{bean.onload}" render=":results" />
</h:form>
<h:panelGroup id="results">
...
</h:panelGroup>
这篇关于如何调用使用本地JavaScript代码的HTML DOM事件JSF托管bean?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!