我怀疑您的代码中一定发生了一些奇怪的事情,例如您的更新逻辑没有在 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 没有在面孔请求后面使用。
好心人。