2

My FaceletContext I try to get with

FaceletContext faceletContext = (FaceletContext) context.getAttributes().get(FaceletContext.FACELET_CONTEXT_KEY);

is always null. What is going wrong here?

Actually what I try to do is to add a composite component dynamically from bean.

Update:

To be more clear. I have the following bean:

@Named
@SessionScoped
public class GroupComponentTreeBuilder implements Serializable {

public UIComponent getGroupTree() {

    UIComponent parent1 = UIComponent.getCurrentComponent(FacesContext.getCurrentInstance());

    return includeCompositeComponent(parent1, "group", "elementgroup.xhtml", "myNewSuperId");
}

public static UIComponent includeCompositeComponent(UIComponent parent, String libraryName, String resourceName, String id) {
    FacesContext context = Faces.getContext();
    Application application = context.getApplication();

    FaceletContext faceletContext = Faces.getFaceletContext();

    Resource resource = application.getResourceHandler().createResource(resourceName, libraryName);
    UIComponent composite = application.createComponent(context, resource);
    composite.setId(id);

    UIComponent implementation = application.createComponent(UIPanel.COMPONENT_TYPE);
    implementation.setRendererType("javax.faces.Group");
    composite.getFacets().put(UIComponent.COMPOSITE_FACET_NAME, implementation);

    parent.getChildren().add(composite);
    parent.pushComponentToEL(context, composite); // This makes #{cc}
                                                    // available.
    try {
        faceletContext.includeFacelet(implementation, resource.getURL());
    } catch (IOException e) {
        throw new FacesException(e);
    } finally {
        parent.popComponentFromEL(context);
    }

    return composite;
}
}

And I want to bind the loaded bean to a panelGroup :

<ui:fragment>
    <h:panelGroup binding="#{groupComponentTreeBuilder.groupTree}" />
</ui:fragment>

The problem is that the FacletContext is null, so i can not call includerFacelet ().

Any suggestions?

flosk8
  • 465
  • 1
  • 6
  • 17
  • possible duplicate of [FaceletContext is Null](http://stackoverflow.com/questions/10338643/faceletcontext-is-null) – Aritz Jul 22 '13 at 07:35
  • Im using JSF 2.2.0 I tried setting javax.faces.PARTIAL_STATE_SAVING in web.xml to false. I still get null. – flosk8 Jul 22 '13 at 09:51
  • It would be helpful if you could give an example on how and where (in the jsf lifesycle) I can access the FacletContext. – flosk8 Jul 22 '13 at 10:26

1 Answers1

3

The problem was that

FaceletContext.FACELET_CONTEXT_KEY

returned the wrong String key "com.sun.faces.facelets.FACELET_CONTEXT"

but I needed

javax.faces.FACELET_CONTEXT

Thanx Mat :-)

flosk8
  • 465
  • 1
  • 6
  • 17
  • Hi :) I have similar NPE issue I have imported `javax.faces.view.facelets.FaceletContext` so is it fine or? Seems like I have just const as `FaceletContext.FACELET_CONTEXT_KEY` :P; I have jsf 2.0; Please give more details; Thanks – user390525 Oct 16 '17 at 21:02