最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Calling JSF ManageBean after PageLoad using f:event postAddToView - Stack Overflow

programmeradmin2浏览0评论

I am writing an app using 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.

I am thinking to use f:event with type postAddView.

<h:outputText id="dummyId">
    <f:event type="postAddToView" listener="#{mngBean.doSomething}" />
</h:outputText>

However it seems like f:event postAddToView is still being processed before the page is displayed for the first time.

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 ponent/event that can do this instead of using java script.

Thanks for your help.

<h:mandButton id="dmyButton" 

value="#{mngBean.getSomething}" actionListener="#{mngBean.doSomething}" style="display: none" type="submit">

Java Script

<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

I am writing an app using 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.

I am thinking to use f:event with type postAddView.

<h:outputText id="dummyId">
    <f:event type="postAddToView" listener="#{mngBean.doSomething}" />
</h:outputText>

However it seems like f:event postAddToView is still being processed before the page is displayed for the first time.

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 ponent/event that can do this instead of using java script.

Thanks for your help.

<h:mandButton id="dmyButton" 

value="#{mngBean.getSomething}" actionListener="#{mngBean.doSomething}" style="display: none" type="submit">

Java Script

<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

Share Improve this question asked Aug 18, 2010 at 1:29 lawardylawardy 1532 gold badges3 silver badges10 bronze badges 1
  • 1 Very good question. Would like to see a ponent for this (javascript firing an event after the page is loaded) in Primefaces... I think it does not exist at the moment. I'm going to link your question in the support forum. – egbokul Commented Aug 18, 2010 at 5:53
Add a ment  | 

2 Answers 2

Reset to default 5

I think the PreRenderViewEvent is what you want.

http://javaserverfaces.java/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.

PostAddToViewEvent is processed "During Restore View Phase, after a ponent 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).

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.

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 parent error.

发布评论

评论列表(0)

  1. 暂无评论