Je l'implémenterais personnellement d'une autre manière.
Définir un dans votre faces-config.xml
comme suit usine de gestionnaire d'exception:
<factory>
<exception-handler-factory>
com.package.faces.FullAjaxExceptionHandlerFactory
</exception-handler-factory>
</factory>
Créer l'usine de traitement d'exception qui s'étend javax.faces.context.ExceptionHandlerFactory
. Il devrait renvoyer votre propre implémentation de l'exceptionHandler. Cela pourrait être un exemple:
import javax.faces.context.ExceptionHandler;
import javax.faces.context.ExceptionHandlerFactory;
public class FullAjaxExceptionHandlerFactory extends ExceptionHandlerFactory {
private ExceptionHandlerFactory wrapped;
/**
* Construct a new full ajax exception handler factory around the given wrapped factory.
* @param wrapped The wrapped factory.
*/
public FullAjaxExceptionHandlerFactory(ExceptionHandlerFactory wrapped) {
this.wrapped = wrapped;
}
/**
* Returns a new instance of {@link FullAjaxExceptionHandler} which wraps the original exception handler.
*/
@Override
public ExceptionHandler getExceptionHandler() {
return new FullAjaxExceptionHandler(wrapped.getExceptionHandler());
}
/**
* Returns the wrapped factory.
*/
@Override
public ExceptionHandlerFactory getWrapped() {
return wrapped;
}
}
En fin de compte, étendre la javax.faces.context.ExceptionHandlerWrapper
à gérer toutes les exceptions. Un exemple est le suivant:
public class FullAjaxExceptionHandler extends ExceptionHandlerWrapper {
private ExceptionHandler wrapped;
public FullAjaxExceptionHandler(ExceptionHandler wrapped) {
this.wrapped = wrapped;
}
private static Throwable extractCustomException(Throwable ex) {
Throwable t = ex;
while (t != null) {
if (t instanceof YourOwnExceptionInterface) {
return t;
}
t = t.getCause();
}
return ex;
}
private static String extractMessage(Throwable t) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
t.printStackTrace(pw);
return matchJmillErrorTag(sw.toString());
}
public static boolean handleException(Throwable original) {
Throwable ex = extractCustomException(original);
if (ex instanceof ViewExpiredException) {
// redirect to login page
return false;
} else if (ex instanceof YourOwnExceptionInterface) {
((YourOwnExceptionInterface) ex).handle();
return true;
} else if (ex instanceof NonexistentConversationException) {
FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
// redirect to login page
return false;
} else {
String message = extractMessage(ex);
final FacesContext fc = FacesContext.getCurrentInstance();
original.printStackTrace();
// redirect to error page
fc.responseComplete();
return true;
}
}
@Override
public void handle() throws FacesException {
final Iterator<ExceptionQueuedEvent> i = getUnhandledExceptionQueuedEvents().iterator();
FacesContext facesContext = FacesContext.getCurrentInstance();
if (Redirector.isRedirectingToLogin(facesContext)) {
return;
}
while (i.hasNext()) {
ExceptionQueuedEvent event = i.next();
ExceptionQueuedEventContext context = (ExceptionQueuedEventContext) event.getSource();
i.remove();
if (!handleException(context.getException())) {
return;
}
}
getWrapped().handle();
}
@Override
public ExceptionHandler getWrapped() {
return wrapped;
}
}
Vérifiez la public static boolean handleException(Throwable original)
dans la classe précédente. Vous pouvez l'utiliser pour gérer toutes vos exceptions.
À un moment donné, j'ai mis une condition là YourOwnExceptionInterface
qui est une interface avec une méthode handle()
que j'implémenterais par exemple par une sorte d'exception NotAuthorizedException
. Dans ce cas, dans la méthode handle()
du NotAuthorizedException
, j'avertirais l'utilisateur qu'il ne peut pas effectuer une certaine opération via un composant p: growl, par exemple. Je l'utiliserais dans mes beans comme throw new NotAuthorizedException("message");
La classe d'exception personnalisée devrait bien sûr étendre le RuntimeException
.
'other.xhtml' est-il une page protégée qui nécessite une connexion? Et pourquoi testez-vous la plate-forme. NB Cela n'a rien à voir avec JSF. Il est appliqué par Tomcat ou quel que soit votre conteneur Servlet. – EJP
@EJP: admin.xhtml est la page que j'affiche après une connexion réussie. Je viens d'ajouter un lien pour vérifier si le délai de session fonctionne ou pas sur admin.xhtml. Ce n'est pas une sorte de page protégée. –