Get Card Session

In my code, I have a simple 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; } } 

The problem is that when in an XHTML file, when I try to access a variable using

 FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("haveErrors") 

I have a null source and I don't understand why.

+4
source share
1 answer

try it

  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)); } 
0
source

Source: https://habr.com/ru/post/1446414/


All Articles