2017-07-05 2 views
0

Je suis nouveau sur JSF 2.0/PrimeFaces et j'ai créé une webapp en utilisant JSF 2.0 + Spring 4. Pour le timeout de session, j'ai fait ci-dessous le mapping dans web.xml:Le délai d'attente de session ne fonctionne pas dans JSF 2.0

<session-config> 
    <session-timeout>1</session-timeout> 
</session-config> 

<error-page> 
    <exception-type>javax.faces.application.ViewExpiredException</exception-type> 
    <location>/resources/login/timeout.xhtml</location> 
</error-page> 

après la connexion, l'utilisateur redirige vers admin.xhtml dans lequel il élément comme

<h:link value="showAnotherPage" outcome="other.xhtml"/> 

Mais au bout de 2 ou 5 minutes, lorsque je clique sur le lien, il me redirige vers la page other.xhtml pas page de délai d'attente.

Est-ce que quelque chose me manque pour configurer? S'il vous plaît aider.

+0

'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

+0

@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. –

Répondre

-1

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.

+0

Vous ajouteriez plusieurs yards de code redondant à l'application pourquoi? Et cela résout le problème comment? Le problème étant quoi? – EJP

+0

Je viens de proposer une solution que je trouve utile au moins dans une grande application (entreprise). Ce type de contrôle/personnalisation m'aide beaucoup dans mes projets. Le problème à résoudre consiste à amener l'utilisateur sur une page si une erreur ou une exception est levée. Telles que javax.faces.application.ViewExpiredException demandé ci-dessus. C'était ma solution. @EJP pourquoi n'écrivez-vous pas un meilleur? –