JSF ViewHandlerWrapper causes NullPointerException
I have a little issue with a JSF ViewHandlerWrapper that I've coded. It works fine most of the times, but at times I will get a NullPointerException from the very core of Mojarra and thus started to wonder whet开发者_开发知识库her I implemented my ViewHandlerWrapper correctly.
public class TokenViewHandler extends ViewHandlerWrapper {
private ViewHandler parent;
public TokenViewHandler(ViewHandler parent) {
this.parent = parent;
}
@Override
public ViewHandler getWrapped() {
return parent;
}
@Override
public void renderView(FacesContext context, UIViewRoot viewToRender) throws IOException, FacesException {
final String token = UUID.randomUUID().toString();
findAndModifyForms(viewToRender, token, context);
getWrapped().renderView(context, viewToRender);
}
private void findAndModifyForms(final UIComponent component, final String token, final FacesContext ctx) {
if (component instanceof UIForm) {
final HtmlInputHidden hidden = (HtmlInputHidden) ctx.getApplication().createComponent(HtmlInputHidden.COMPONENT_TYPE);
hidden.setValue(token);
component.getChildren().add(hidden);
} else {
for (UIComponent tempComponent : component.getChildren()) {
findAndModifyForms(tempComponent, token, ctx);
}
}
}
}
From the code you quickly realize that I want to add a inputHidden-component with a UUID value to each form on the view.
As I haven't found any good examples for ViewHandlerWrappers I assumed that it should look like a ExceptionHandlerWrapper but since I get the occassional NPE using my ViewHandler, I assume that something is wrong and I can't seem to see it.
This seems to be related to a bug in partial state saving, see issue 1414.
The IceFaces guys encountered a similar problem and they got it (temporarily) fixed by adding the following line:
facesContext.getViewRoot().addComponentResource(facesContext, new UIOutput(), "head");
Give it a try. Do it before rendering the view.
精彩评论