DeltaSpike处理异常和重定向(ajax请求)

时间:2014-12-20 18:56:03

标签: ajax jsf exception-handling jsf-2.2 deltaspike

当在AJAX请求上处理异常时,必须将用户重定向到另一个页面。 有一个带静态方法的类可以执行重定向。请求是ajax时出现问题。以下是上述功能的代码:

public static void redirect(FacesContext fc, String view, int paramValue){
    ExternalContext ec = fc.getExternalContext();
    String path = ec.encodeResourceURL(fc.getApplication().getViewHandler().getActionURL(fc, view) + "?p=" + paramValue);
    try {
        ec.redirect(path);
    } catch(Exception e) {
        throw new RuntimeException();
    }
}

该行&#ec ;redirect(路径)'不要抛出任何异常。在浏览器调试控制台中,我可以看到下一个500内部服务器错误:

Stack Trace
javax.servlet.ServletException
javax.faces.webapp.FacesServlet.service(FacesServlet.java:659)
io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:61)
io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)
io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:113)
io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:56)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)
io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:45)
io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:61)
io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:58)
io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:70)
io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)
org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)
io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:240)
io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:227)
io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:73)
io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:146)
 io.undertow.server.Connectors.executeRootHandler(Connectors.java:177)
 io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:727)
 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
java.lang.Thread.run(Thread.java:745)

未重定向用户。如果是来自同一页面的另一个ajax请求,则重复该过程,并在浏览器控制台中启动服务器500错误。

我使用的是wildfly 8.1和Mojarra 2.2.6-jbossorg-4 20140501-1134。如何在ajax请求上正确重定向?

修改

Fox异常处理使用自定义ExceptionHandlerWrapper和Deltaspike异常处理程序:

public class CustomExceptionHandlerWrapper extends ExceptionHandlerWrapper {

@Override
public void handle() throws FacesException {
    FacesContext context = FacesContext.getCurrentInstance();
    Iterator<ExceptionQueuedEvent> it = getUnhandledExceptionQueuedEvents().iterator();
    while (it.hasNext()) {
        try {
            ExceptionQueuedEvent evt = it.next();
            ExceptionToCatchEvent etce = null;

            if (context == null || !context.getPartialViewContext().isAjaxRequest()) {
                etce = new ExceptionToCatchEvent(evt.getContext().getException(), WebRequestLiteral.INSTANCE);
            } else {
                etce = new ExceptionToCatchEvent(evt.getContext().getException(), FacesRequestLiteral.INSTANCE);
            }
            beanManager.fireEvent(etce);
        } finally {
            it.remove();
        }

    }
    getWrapped().handle();
}
}

Deltaspike Handler(Documentation):

public class FacesExceptionHandler {
void showFacesMessage(@Handles @FacesRequest ExceptionEvent<Throwable> evt, FacesContext fc) {
    Throwable exception = evt.getException();
    if(exception  instanceof NotLoggedException) {
        evt.handled();
        JSF.redirect(FacesContext.getCurrentInstance(), "/login.jsf", 1);
    }
}

}

0 个答案:

没有答案