简体   繁体   English

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

[英]Exclude public pages from session in JSF

I have a home page that is written in JSF 2 . 我有一个用JSF 2编写的主页。 When I click on ' Sign Up ' on the home page, it goes through MyController that is a Named and Session scoped bean. 当我单击主页上的“ Sign Up ”时,它将通过MyController ,它是一个NamedSession scoped bean。

My problem is, if the user opens the home page, waits till the session expires and click on the 'Sign Up' link, we get a ViewExpiredException. 我的问题是,如果用户打开主页,等到会话过期并单击“注册”链接,我们将收到ViewExpiredException。

How do we exclude the links from home page to be excluded from session ? 我们如何从主页中排除要从会话中排除的链接?

to deal with ViewExpiredExceptions you need to implement a ExceptionHandlerWrapper and a Factory: 要处理ViewExpiredExceptions,您需要实现ExceptionHandlerWrapper和Factory:

ExceptionHandler: 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();
    }
}

and the Factory class: 和工厂类:

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;
    }

}

register the factory Class in your faces-config.xml: 在您的faces-config.xml中注册工厂类:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM