1

当 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(path)' 行不会引发任何异常。在浏览器调试控制台中,我可以看到下一个 500 Internal Server Error:

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 Exception Handler:

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 处理程序(文档):

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

}

4

0 回答 0