0

我使用 ConversatonScoped bean 作为我的 JSF 支持 bean。有时,url 会附加 ?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>

它没有用。当 url 的 cid 错误时,它会显示 500 错误。我该如何解决?

4

1 回答 1

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>    
于 2013-12-18T20:42:06.477 回答