FaceletContext is Null

假装没事ソ 提交于 2020-01-02 13:31:46

问题


I have a JSF 2.0 project that I am unable to obtain a FaceletContext from.

Here is the setup from my web.xml:

<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
</context-param>

<context-param>
    <param-name>facelets.DEVELOPMENT</param-name>
    <param-value>true</param-value>
</context-param>

<context-param>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>server</param-value>
</context-param>

<context-param>
    <param-name>javax.faces.PARTIAL_STATE_SAVING</param-name>
    <param-value>false</param-value>
</context-param>

<context-param>
    <param-name>javax.faces.FULL_STATE_SAVING_VIEW_IDS</param-name>
    <param-value>/index.xhtml</param-value>
</context-param>
<context-param>
    <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
    <param-value>.xhtml</param-value>
</context-param>

<context-param>
    <param-name>javax.faces.application.CONFIG_FILES</param-name>
    <param-value>/WEB-INF/faces-config.xml</param-value>
</context-param>

<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
</servlet-mapping>

I can obtain the FacesContext, but not the FaceletContext. The following lines of code are where the issue lies:

            FacesContext fctx = FacesContext.getCurrentInstance(); //works
    //FaceletContext f2ctx = (FaceletContext) fctx.getAttributes().get(FaceletContext.FACELET_CONTEXT_KEY);
    FaceletContext f2ctx = (FaceletContext) fctx.getELContext().getContext(FaceletContext.class);

Can anyone assist?

Update:

The above was a snippet from my overall goal: loading a facelet file containing a dialog on demand, then add it to a parent to display. The code below is incomplete, but should explain what I'm getting at. Note that the app uses the Primefaces library, hence the RequestContext object.

public void launch() {

    form.getChildren().clear();

    FacesContext fctx = FacesContext.getCurrentInstance();
    for (Object o: fctx.getAttributes().keySet()) {
        System.out.println(o.toString());
    }

    FaceletContext f2ctx = (FaceletContext) fctx.getAttributes().get(FaceletContext.FACELET_CONTEXT_KEY);
    //FaceletContext f2ctx = (FaceletContext) fctx.getELContext().getContext(FaceletContext.class);
    dialog = new Dialog();

    try {
        f2ctx.includeFacelet(dialog, "/WEB-INF/facelets/dialog/manager.xhtml");
        form.getChildren().add(dialog);
    } catch (IOException ex) {
        log.debug(ex.toString());
    }

    RequestContext.getCurrentInstance().update("mainPageCenterForm");
}

回答1:


It will be null when you try to access it before the view is ever been built. For example, during the beforePhase of RESTORE_VIEW phase. Rearrange your code logic. It's not clear what's the functional requirement is and where exactly you're trying to access it, so I can't give a more suited answer.


Unrelated to the concrete problem, the javax.faces.FULL_STATE_SAVING_VIEW_IDS context param is ignored when the partial state saving is disabled. Perhaps that was not your intent? See also this related answer Should PARTIAL_STATE_SAVING be set to false?



来源:https://stackoverflow.com/questions/10338643/faceletcontext-is-null

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!