0

I am working on a page with a session-scoped managed bean. On the page I have a preRenderComponent:

<f:metadata> 
        <f:event listener="#{pageBean.init}" type="preRenderComponent"></f:event>
</f:metadata>

This page is a template client, that shares a template with some other pages. The template contains a side navigation bar with links to each of the template client pages.

And the page bean:

@Named
@Default
@SessionScoped
public class pageBean  implements Serializable {
 @PostConstruct
 public void init(){
   System.out.println("Page Bean init.");
   //call methods that populate data on the page
 }

}

And the issue occurs as follows:

  1. If I removed the line on the page with preRenderComponent, the pageBean init() method will still be called when the page is accessed.

  2. If I kept the line said above, the init() method will be called when accessed, but it will also be called whenever I clicked on the side navigation bar and accessed another page which uses the same template.

I have referred to this question:CDI bean constructor and @PostConstruct called multiple times and made sure I was not mixing JSF with CDI, yet this issue still occurs. Though it seems I could resolve this problem by just simply removing the preRenderComponent line, I really wish to understand what is going on here and figure out a way to avoid it in the future.

Any information is much appreciated.

Community
  • 1
  • 1
Rui Chen
  • 41
  • 1
  • 10
  • Why are there two annotations on the managed bean, `@Default` and `@SessionScoped` and why is the post construct method bound to `` through `listener="#{pageBean.init}"`? – Tiny Jun 01 '15 at 17:39
  • @Tiny the default annotation is a common practice in the code base so I followed it, not sure why though. As for the f:event, isn't that the normal way to use preRenderComponent? – Rui Chen Jun 01 '15 at 18:23
  • Which JSF impl/version? – BalusC Jun 01 '15 at 18:42
  • @BalusC I cannot tell from the MANIFEST.mf file since it just said MANIFEST-VERSION 1.0, from the faces-config file it stated version 2.0, so I believe it is JSF 2.0 – Rui Chen Jun 01 '15 at 19:09
  • This is not helpful in order to be able to reproduce your problem based on the information provided so far when starting from scratch. And, which manifest file exactly are you talking about? Which JAR exactly did you extract? Just telling/looking the JAR filenames or even only the Java EE appserver impl/version would possibly have been sufficient. Otherwise there's always the programmatic way as hinted in http://stackoverflow.com/questions/13125782/how-to-detect-current-jsf-version/13125869#13125869 – BalusC Jun 01 '15 at 19:11
  • @BalusC Thank you! I could not find other manifest file in the project or any other related jars. I tried the method in your link and it returned 2.1.18.Final. – Rui Chen Jun 01 '15 at 19:57

0 Answers0