JSF preRenderComponent event won't trigger

Go To StackoverFlow.com

0

I've encountered a problem while trying to set a beans variable and calling the method to set the user trough a event. The problem is that the viewParam works (i can request the value on the page and its set in the bean) but the event preRenderComponent (nor preRenderView) isnt fired. The strange thing is that if i annotate the initUser method with @PostConstruct the event does trigger but it triggers also for the @PostConstruct which isnt what i want. But if i remove the annotation it doesn't enter the method at all.

Below you will find my .xhtml and below that my bean. I'm using netbeans and a Glassfish (3.0) server. What am i doing wrong here?

XHTML:

    <f:metadata>
        <f:viewParam name="userId" value="#{tweetBean.userId}" />
        <f:event listener="#{tweetBean.initUser}" type="preRenderComponent" />
        <f:event listener="#{tweetBean.initUser}" type="preRenderView" />
    </f:metadata>

BEAN:

    @Named
    @RequestScoped
    public class tweetBean implements Serializable
    {
        @Inject
        private KwetterService service;
        private String userId; // Has Getters + setters

        public void initUser()
        {
            System.out.println("test");

            for (User u : service.findAll())
            {
                if (u.getName().equals(userId))
                {
                    user = u;
                    System.out.println("User found");
                }
            }

            if (user == null)
            {
                System.out.println("no user found with this name");
                user = service.find(0);
            }
        }
    }
2012-04-04 08:22
by user1312260
how bout changing the scope to ViewScoped - Daniel 2012-04-04 08:29
I'm afraid that doesn't work, neither does SessionScoped. But i prefer to keep using RequestScope and it should work in RequestScope - user1312260 2012-04-04 08:32
haven't notice it before... try to make it ManagedBean bean instead of Named.. - Daniel 2012-04-04 08:36
Thanks again for your fast anwser i just tried it but i'm afraid it doesnt solve it. Also we are encouraged to use CDI over managed beans - user1312260 2012-04-04 08:41
What if you remove the preRenderComponent event, so there's only preRenderView? Also, maybe other code is intervering somehow. Create a new project with just 1 page and one simple bean. Check if it works there. If it does, work back - Mike Braun 2012-04-04 08:57
Thanks Mike, i'll try it in a new project! To clarify I've been using just one of the events, i just put them there both to test it. I have one commented out in my code but it doesnt matter, doesn't work either way - user1312260 2012-04-04 09:04
Thanks Mike! I guess my project was bugged... The exact same code works perfectly in a new project - user1312260 2012-04-04 09:11


0

Try that:

<f:metadata>
   <f:event type="javax.faces.event.PreRenderViewEvent" listener="#{tweetBean.initUser}" />
</f:metadata>

BTW: I reckon that if you use @PostConstruct the normal lifecycle-callback is executed by the container, but the event still is not firing...

2012-04-04 09:02
by jan groth
Thanks for the anwser, aperently my project had some issues, the exact same code works in a new project - user1312260 2012-04-04 09:13
Ads