如何使用url中的cid处理过期的会话?

时间:2013-07-29 19:01:30

标签: java java-ee glassfish rewrite

我使用ConversatonScoped bean作为我的JSF支持bean。有时,网址会附加?cid = XX。但是,这通常会导致会话范围bean过期时出现问题。它在浏览器中显示500错误,在服务器日志中显示org.jboss.weld.context.ContextNotActiveException。我已将以下内容放入我的web.xml

<error-page>
    <exception-type>org.jboss.weld.context.ContextNotActiveException</exception-type>
    <location>/faces/index.xhtml</location>
</error-page>
<error-page>
    <error-code>500</error-code>   
    <location>/faces/index.xhtml</location>
</error-page>

它不起作用。当网址有错误的cid时,它会显示500错误。我该如何解决?

1 个答案:

答案 0 :(得分:1)

您可以创建自己的“ViewExpiredExceptionHandler”并重定向到所需的页面。

这样的事情应该有效:

import java.util.Iterator;
import org.jboss.weld.context.NonexistentConversationException;

import javax.faces.FacesException;
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 ViewExpiredExceptionHandler
        extends ExceptionHandlerWrapper {

    private ExceptionHandler wrapped;

    public ViewExpiredExceptionHandler(ExceptionHandler wrapped) {
        this.wrapped = wrapped;
    }

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

    @Override
    public void handle()
            throws FacesException {

        for (Iterator<ExceptionQueuedEvent> i =
                getUnhandledExceptionQueuedEvents().iterator();
                i.hasNext();) {
            ExceptionQueuedEvent event = i.next();
            ExceptionQueuedEventContext context =
                    (ExceptionQueuedEventContext) event.getSource();
            Throwable t = context.getException();

            if (t instanceof ViewExpiredException
                    || t instanceof NonexistentConversationException) {

                FacesContext fc = FacesContext.getCurrentInstance();
                try {

                    if (!fc.getExternalContext().isResponseCommitted()) {
                        fc.getExternalContext().redirect("index.xhtml");
                    }
                    fc.renderResponse();
                    break;
                } catch (Exception ex) {
                    throw new FacesException(ex);
                } finally {
                    i.remove();
                }
            }
        }
        getWrapped().handle();
    }
}

像这样创建一个ViewExpiredExceptionHandlerFactory:

import javax.faces.context.ExceptionHandler;
import javax.faces.context.ExceptionHandlerFactory;

public class ViewExpiredExceptionHandlerFactory
    extends ExceptionHandlerFactory {

    private ExceptionHandlerFactory parent;

    public ViewExpiredExceptionHandlerFactory(
            ExceptionHandlerFactory parent) {
        this.parent = parent;
    }

    @Override public ExceptionHandler getExceptionHandler() {

        ExceptionHandler result = parent.getExceptionHandler();  
        result = new ViewExpiredExceptionHandler(result);    
        return result;  
    }   
}

将此添加到您的faces-config.xml

<factory>
    <exception-handler-factory>
        com.yourcode.ViewExpiredExceptionHandlerFactory
    </exception-handler-factory>
</factory>