【问题标题】:Cannot resolve Constants.REQUEST_CONTEXT_ATTR in PrimeFaces 4.0无法解析 PrimeFaces 4.0 中的 Constants.REQUEST_CONTEXT_ATTR
【发布时间】:2015-02-07 01:55:59
【问题描述】:

我需要更新 GUI。它正在显示计时器。计时器工作正常。但是包含计时器的页面或组件必须始终更新。然后我可以显示实时计时器。 所以我需要更新组件。但是RequestContext.getCurrentInstance() 指向NullPointerException。我在下面搜索并找到了。

((RequestContext) FacesContext.getCurrentInstance().getAttributes().get(Constants.REQUEST_CONTEXT_ATTR);)

但是Constants.REQUEST_CONTEXT_ATTR 没有解决。另一方面,红色 REQUEST_CONTEXT_ATTR 我使用的是 PrimeFaces 4.0 JSF 2.0。我该如何决定,请建议我。

【问题讨论】:

  • 如果我没记错的话,org.primefaces.util.Constants.REQUEST_CONTEXT_ATTR 自 PrimeFaces 3.5 发布以来一直存在,并在 PrimeFaces 4.0 中被删除。
  • 那我需要做什么

标签: jsf primefaces


【解决方案1】:

我怀疑您的代码中一定发生了一些奇怪的事情,例如您的更新逻辑没有在 faces servlet 后面运行。

RequestContext.getCurrentInstance() 是正确的 API,如果您希望获取对正在进行的传入 JSF 请求处于活动状态的 primefaces RequestContext 实例。

如果您在那里收到 NullPointer 异常,则看起来好像 FacesContext 没有使用 primefaces 中的 RequestContext 丰富。

我认为这不应该发生。您应该交叉检查您使用的 Primefaces 版本与您正在运行的容器的兼容性。 例如,primefaces 3.5 适用于 JEE6,primefaces 4.0 我不知道。

Primefaces 6.0 适用于 JEE7 等...

如果您执行 FacesCONtext.getCurrentInstance() 之类的操作,您会崩溃吗? 如果是这样,这将表明您正在尝试执行更新逻辑,而无需为线程上下文激活面上下文。 例如。您无法从 Mdb 访问面孔上下文。 或计时器轮询()。小心点。

最后,我相信您发布的常量已从 primefaces 的常量中删除。

他们已将其封装到 RequestContext 对象本身中。

这是我现在正在查看的来自 primefaces 6.0 的代码片段:

/*
 * Copyright 2009-2014 PrimeTek.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.primefaces.context;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.primefaces.component.api.AutoUpdatable;
import org.primefaces.util.AjaxRequestBuilder;
import org.primefaces.util.CSVBuilder;
import org.primefaces.util.StringEncrypter;
import org.primefaces.util.WidgetBuilder;

/**
 * A RequestContext is a helper class consisting of several utilities.
 * RequestContext is thread-safe and scope is same as FacesContext.
 * Current instance can be retrieved as;
 * <blockquote>
 *  RequestContext.getCurrentInstance();
 * </blockquote>
 */
public abstract class RequestContext {

    private static final ThreadLocal<RequestContext> INSTANCE = new ThreadLocal<RequestContext>();

    public static final String INSTANCE_KEY = RequestContext.class.getName();

    public static RequestContext getCurrentInstance() {

        RequestContext context = INSTANCE.get();

        // #6503 - it's valid that a FacesContext can be released during the request
        // Our PrimeFacesContext therefore will only release our ThreadLocal cache
        // The RequestContext will be destroyed automatically if the FacesContext and it's attributes will be destroyed
        if (context == null) {
            FacesContext facesContext = FacesContext.getCurrentInstance();
            if (facesContext != null && !facesContext.isReleased()) {
                context = (RequestContext) facesContext.getAttributes().get(INSTANCE_KEY);
                if (context != null) {
                    INSTANCE.set(context);
                }
            }
        }

        return INSTANCE.get();
    }

所以结论是,他们仍然将 RequestCONtext 放入 FacesContext 属性中,但是他们现在用来读取人脸上下文属性的常量是这个 INSTANCE_KEY。

facesContext.getAttributes().get(INSTANCE_KEY)

问题不是要使用什么常量,而是为什么你的 getCurrentInstance() 会中断。 这通常表明 JSF API 没有在面孔请求后面使用。

好心人。

【讨论】:

  • 感谢您的广泛回答,但我个人会问FacesContext.getCurrentInstance() 是否有效。据我们所知,OP 正试图在 jsf 请求之外访问它,并且在旧版本中,然后 6.0 使用 RequestContext 会超过 NPE
  • 您好,我同意。但我确实问过是否可以获取 FacesContext。引用上面的帖子:“如果您执行 FacesCONtext.getCurrentInstance() 之类的操作,您会爆炸吗?...”我
  • 对不起 ;-) 错过了那部分
猜你喜欢
  • 2014-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-20
  • 2019-02-18
  • 2013-05-21
  • 1970-01-01
相关资源
最近更新 更多