从JSF中的会话中排除公共页面

时间:2014-01-16 13:04:33

标签: java session jsf-2

我有一个用JSF 2编写的主页。当我点击主页上的“注册”时,它会通过MyControllerNamed bean Session scoped

我的问题是,如果用户打开主页,等待会话过期并单击“注册”链接,我们会收到ViewExpiredException。

我们如何从主页中排除要从会话中排除的链接?

1 个答案:

答案 0 :(得分:0)

处理实现ExceptionHandlerWrapper和Factory所需的ViewExpiredExceptions:

的ExceptionHandler:

import java.io.Serializable;
import java.util.Iterator;

import javax.faces.FacesException;
import javax.faces.application.NavigationHandler;
import javax.faces.application.ViewExpiredException;
import javax.faces.context.ExceptionHandler;
import javax.faces.context.ExceptionHandlerWrapper;
import javax.faces.context.FacesContext;
import javax.faces.event.ExceptionQueuedEvent;
import javax.faces.event.ExceptionQueuedEventContext;

public class SystemNaviExceptionHandler  extends ExceptionHandlerWrapper implements Serializable {

    private static final long serialVersionUID = -1865349922998154148L;
    private ExceptionHandler parent;

    public SystemNaviExceptionHandler(ExceptionHandler parent) {
        this.parent = parent;
    }

    @Override
    public ExceptionHandler getWrapped() {
        return this.parent;
    }

    @Override
    public void handle() throws FacesException {
        for (Iterator<ExceptionQueuedEvent> i = getUnhandledExceptionQueuedEvents().iterator(); i.hasNext();) {
            ExceptionQueuedEvent event = i.next();
            System.out.println("Iterating over ExceptionQueuedEvents. Current:" + event.toString());
            ExceptionQueuedEventContext context = (ExceptionQueuedEventContext) event.getSource();
            Throwable t = context.getException();
            if (t instanceof ViewExpiredException) {
                ViewExpiredException vee = (ViewExpiredException) t;
                FacesContext fc = FacesContext.getCurrentInstance();

                NavigationHandler nav =
                        fc.getApplication().getNavigationHandler();
                try {
                    // Push some stuff to the flash scope for use in the page
                    fc.getExternalContext().getFlash().put("expiredViewId", vee.getViewId());
                    // i do a redirect to login page
                    nav.handleNavigation(fc, null, "client-login");
                    fc.renderResponse();

                } finally {
                    i.remove();
                }
            }
        }

        getWrapped().handle();
    }
}

和工厂类:

import java.io.Serializable;

import javax.faces.context.ExceptionHandler;

public class ExceptionHandlerFactory extends javax.faces.context.ExceptionHandlerFactory implements Serializable {

    private static final long serialVersionUID = 4685303865999111737L;
    private javax.faces.context.ExceptionHandlerFactory parent;

    public ExceptionHandlerFactory(javax.faces.context.ExceptionHandlerFactory parent) {
        this.parent = parent;
    }

    @Override
    public ExceptionHandler getExceptionHandler() {
        ExceptionHandler result = parent.getExceptionHandler();
        result = new SystemNaviExceptionHandler(result);
        return result;
    }

}

在faces-config.xml中注册工厂类:

<factory>
        <exception-handler-factory>path.to.factoryClass.ExceptionHandlerFactory</exception-handler-factory>
    </factory>