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 whether 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.