javascript 在页面加载后使用 f:event postAddToView 调用 JSF ManageBean
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3508320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling JSF ManageBean after PageLoad using f:event postAddToView
提问by lawardy
I am writing an app using JSF 2.0.
我正在使用 JSF 2.0 编写一个应用程序。
For one of the page, there is a section of the page that takes a long time to display.
对于其中一个页面,页面中有一段需要很长时间才能显示。
To improve the user experience, I am thinking to load the page first and then automatically do an Ajax call back to the JSF manage bean object once the page is loaded successfully after 1st load.
为了改善用户体验,我想先加载页面,然后在第一次加载后成功加载页面后自动执行 Ajax 回调到 JSF 管理 bean 对象。
I am thinking to use f:event with type postAddView.
我正在考虑使用类型为 postAddView 的 f:event。
<h:outputText id="dummyId">
<f:event type="postAddToView" listener="#{mngBean.doSomething}" />
</h:outputText>
However it seems like f:event postAddToViewis still being processed before the page is displayed for the first time.
但是,在第一次显示页面之前,似乎仍在处理f:event postAddToView。
The other options that I have explore is to create a hidden button and get javascript to trigger it. It works however I am just wondering if there is a nice JSF component/event that can do this instead of using java script.
我探索的其他选项是创建一个隐藏按钮并让 javascript 来触发它。它有效但是我只是想知道是否有一个很好的 JSF 组件/事件可以做到这一点而不是使用 java 脚本。
Thanks for your help.
谢谢你的帮助。
<h:commandButton id="dmyButton"
value="#{mngBean.getSomething}" actionListener="#{mngBean.doSomething}" style="display: none" type="submit">
value="#{mngBean.getSomething}" actionListener="#{mngBean.doSomething}" style="display: none" type="submit">
Java Script
Java脚本
<script language="JavaScript">
$(document).ready(function() {
if (document.getElementById('form:dmyButton').value == 'true') {
document.getElementById('form:dmyButton').click();
}
});
</script>
Thanks for all your help in advance
提前感谢您的所有帮助
回答by edburns
I think the PreRenderViewEvent is what you want.
我认为 PreRenderViewEvent 是你想要的。
http://javaserverfaces.java.net/nonav/docs/2.0/javadocs/javax/faces/event/PreRenderViewEvent.html
http://javaserverfaces.java.net/nonav/docs/2.0/javadocs/javax/faces/event/PreRenderViewEvent.html
Though the docs don't show it, the preRenderViewEvent does work with f:event. I'll fix the docs presently.
尽管文档没有显示它,但 preRenderViewEvent 确实适用于 f:event。我会立即修复文档。
回答by egbokul
PostAddToViewEvent is processed "During Restore View Phase, after a component has been added to a view." (according to Java Server Faces 2.0 - The Complete Reference) which means it is really early in the lifecycle (Restore View is the first lifecycle phase).
PostAddToViewEvent 被处理“在恢复视图阶段,在一个组件被添加到视图之后”。(根据Java Server Faces 2.0 - The Complete Reference)这意味着它确实处于生命周期的早期(恢复视图是生命周期的第一个阶段)。
I don't think that any PhaseListener will help you in this case, since they all run on the server side, not on the client side.
我认为在这种情况下任何 PhaseListener 都不会帮助您,因为它们都运行在服务器端,而不是客户端。
However, <f:ajax>can be applied to <h:body>, with event="load". I tried it, but it didn't really work, <f:ajax>wrapping <h:body>nothing happened, the other way around I got Unable to attach <f:ajax> to non-ClientBehaviorHolder parenterror.
但是,<f:ajax>可以应用于<h:body>, 与event="load"。我尝试过,但它并没有真正的工作,<f:ajax>包装<h:body>什么都没有发生,我周围的其他方式得到了Unable to attach <f:ajax> to non-ClientBehaviorHolder parent错误。

