I want to do a redirect in the case a validation failure occurs in one of my
view params. I also would like to use a FacesMessage to report what
happend to the user. I can get the redirect to work, but the FacesMessage
is not maintained across the redirect. I'm using the code below:
public void preRenderView(ComponentSystemEvent event) { FacesContext fc =
FacesContext.getCurrentInstance(); boolean getMethod = ((HttpServletRequest)
fc.getExternalContext().getRequest()).getMethod().equals("GET") ? true :
false; boolean ajaxRequest = fc.getPartialViewContext().isAjaxRequest();
boolean validationFailed = fc.isValidationFailed(); if (getMethod &&
!ajaxRequest && validationFailed) { NavigationHandler nav =
fc.getApplication().getNavigationHandler(); Flash flash =
fc.getExternalContext().getFlash(); flash.setKeepMessages(true);
fc.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "My error
message goes here", "")); nav.handleNavigation(fc, null,
"/index?faces-redirect=true"); fc.renderResponse(); } else { loadEntity(); }
}
I am using extremely similar code in my custom exception handler and it works
as expected (the FacesMessage is maintained across the redirect). Is this a
known issue with the preRenderView system event? The custom exception
handler code looks like:
@Override public void handle() throws FacesException { for(Iterator i =
getUnhandledExceptionQueuedEvents().iterator(); i.hasNext();) {
ExceptionQueuedEvent event = i.next(); ExceptionQueuedEventContext context =
(ExceptionQueuedEventContext)event.getSource(); Throwable t =
context.getException(); if(t instanceof ViewExpiredException) { FacesContext
fc = FacesContext.getCurrentInstance(); NavigationHandler nav =
fc.getApplication().getNavigationHandler(); Flash flash =
fc.getExternalContext().getFlash(); try { flash.setKeepMessages(true);
fc.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Your
session has expired", "")); nav.handleNavigation(fc, null,
"/index?faces-redirect=true"); fc.renderResponse(); } finally { i.remove(); }
} } getWrapped().handle(); }
--
[Message sent by forum member 'slominskir']
View Post: http://forums.java.net/node/718010