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);
}
}
}
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...