-
1. How to walk the viewroot before render response?
tomba Feb 4, 2011 4:17 AM (in response to tomba)No straightforward solution then?
-
2. Re: How to walk the viewroot before render response?
boy18nj Feb 5, 2011 3:01 PM (in response to tomba)Hi Steven,
If you want to walk the viewroot before rendering response, you need to use system event, do something like this-
<f:view>
<f:event type="preRenderView" listener="#{user.changeLocale}"/>
<h:head>
<title>...</title>
</h:head>
<h:body>
...
</h:body>
</f:view>
public String changeLocale(ComponentSystemEvent event) {
FacesContext context = FacesContext.getCurrentInstance();
context.getViewRoot().setLocale(new Locale(languageCode));
return null;
}
Note that system events are available only in JSF 2.0
-
3. How to walk the viewroot before render response?
nbelaevski Feb 6, 2011 7:05 AM (in response to tomba)Hi Steven,
Try specifying phaseListener via view root attribute (f:view tag). This will work in JSF 1.2
-
4. How to walk the viewroot before render response?
tomba Feb 6, 2011 6:21 PM (in response to nbelaevski)Aman, I'm aware of the system but I'm stuck with JSF1.2 for now.
Anyway, in my case context.getViewRoot() is not null either, so I could do a setLocale() without problems. However, context.getViewRoot().getChildren() is empty in beforephase(RENDER) when it's a regular GET request. And my problem is exactly that I need to loop the children in the beforephase or RENDER_RESPONSE...
Nick, will a phaselistener on f:view make a difference from one specified in faces-config? I would find that very odd, but I can try...
Thanks
-
5. How to walk the viewroot before render response?
nbelaevski Feb 6, 2011 7:26 PM (in response to tomba)Yes, different. As a complete solution, you can use PhaseListener declared in faces-config.xml to add PhaseListener to created view root,
-
6. Re: How to walk the viewroot before render response?
tomba Feb 7, 2011 5:14 AM (in response to nbelaevski)Nick, I'm stunned.
Previously I had my listener added to the faces-config.xml, but my viewroot had 0 children every time I was in beforephase (render) on a GET request.
Now, by adding <f:phaseListener type="...."/> within the <f:view>...</f:view>, things are now working properly.
I'm still curious to know why this is. Can you explain that?
It's also annoying, as this would require me to add this <f:phaseListener> element to each of my pages
-
7. Re: How to walk the viewroot before render response?
nbelaevski Feb 7, 2011 5:28 AM (in response to tomba)Steven,
This is how JSF works - see details in spec.
And once again:
>> As a complete solution, you can use PhaseListener declared in faces-config.xml to add PhaseListener to created view root/
-
8. Re: How to walk the viewroot before render response?
tomba Apr 29, 2011 11:43 AM (in response to nbelaevski)After running this solution for a bit, I have bumped into a strange problem where ui:repeat appears not to be expanded when my phaselistener in before RENDER_RESPONSE kicks in. Hence, at the time I run through the components in the phaselistener, I have only one component in the repeat, whereas on screen I get 8 elements in the repeat (the latter being correct as I have 8 elements in the list).
Still trying to work out why exactly, but do you have any idea why ui:repeat seems to behave differently from other JSF components?
-
9. How to walk the viewroot before render response?
nbelaevski Apr 29, 2011 12:02 PM (in response to tomba)Steven,
ui:repeat is one of so-called 'stamped' components - only one instance of component (with children components) is present in component tree, and nested components are processed inside iteration.