tags:

views:

24

answers:

1

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.

A: 

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.

BalusC
The addComponentResource()-call did not help me per sé, however, the link you provided gave an indication that the problem would be solved in Mojarra 2.0.3. After updating my libraries, it worked immediately.
sicn
Cheer! Glad you got it to work.
BalusC