gpt4 book ai didi

jsf - 获取 session map

转载 作者:行者123 更新时间:2023-12-04 14:45:44 26 4
gpt4 key购买 nike

在我的代码中,我有一个简单的 Phase Listener .

public class PhaseTracker implements PhaseListener {
private static final Logger LOGGER = LoggerFactory.getLogger(PhaseTracker.class);

@Override
public void afterPhase(PhaseEvent pe) {
LOGGER.debug("afterPhase " + pe.getPhaseId());
LOGGER.debug(""+FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("haveErrors"));

}

@Override
public void beforePhase(PhaseEvent pe) {
LOGGER.debug("beforePhase " + pe.getPhaseId());
boolean error = false;
Iterator<FacesMessage> messageIterator = pe.getFacesContext().getMessages();
while (messageIterator.hasNext()) {
FacesMessage message = messageIterator.next();
if (message.getSeverity().equals(FacesMessage.SEVERITY_ERROR)) {
LOGGER.debug("beforePhase severity is error");
error = true;
}

}

FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("haveErrors", error);
LOGGER.debug(""+FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("haveErrors"));
}

@Override
public PhaseId getPhaseId() {
LOGGER.debug("getPhaseId");
return PhaseId.ANY_PHASE;
}
}

问题是当在 XHTML 中时当我尝试访问变量时的文件
FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("haveErrors")

我有一个 null来源,我不明白为什么。

最佳答案

像这样尝试

 public synchronized void beforePhase(PhaseEvent event) {

FacesContext context = event.getFacesContext();
ExternalContext ex = context.getExternalContext();

String viewId = "/index.xhtml";

if (context.getViewRoot() != null && context.getViewRoot().getViewId() != null) {
viewId = context.getViewRoot().getViewId();

}

String localeCode = (String) ex.getSessionMap().get("localeCode");

if (localeCode == null) {
ex.getSessionMap().put("localeCode", "en");
context.getViewRoot().setLocale(new Locale("en"));
// System.out.println("locale code " +"En?");
} else if (localeCode != null) {
//System.out.println("locale code " +localeCode);
ex.getSessionMap().put("localeCode", localeCode);
context.getViewRoot().setLocale(new Locale(localeCode));
}

关于jsf - 获取 session map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13414258/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com