FaceletContext是Null

时间:2012-04-26 17:45:23

标签: jsf jsf-2 facelets

我有一个JSF 2.0项目,我无法从中获取FaceletContext。

以下是我的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>

我可以获取FacesContext,但不能获取FaceletContext。以下代码行是问题所在:

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

有人可以帮忙吗?

更新

以上是我总体目标的片段:按需加载包含对话框的facelet文件,然后将其添加到父级进行显示。下面的代码不完整,但应该解释我的目标。请注意,该应用程序使用Primefaces库,因此使用RequestContext对象。

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 个答案:

答案 0 :(得分:1)

在您构建视图之前尝试访问它时,它将是null。例如,在beforePhaseRESTORE_VIEW期间。重新排列代码逻辑。目前尚不清楚功能需求是什么以及您在何处尝试访问它,所以我无法给出更合适的答案。


对具体问题

无关,禁用部分状态保存后,{strong}忽略上下文 。也许这不是你的意图?另请参阅此相关答案Should PARTIAL_STATE_SAVING be set to false?