Glassfish 3.1.2.2上的'ExceptionHandlerFactory未正确配置'

时间:2013-12-06 00:16:07

标签: jsf glassfish glassfish-3 exceptionhandler

我正在尝试通过ExceptionHandler处理未捕获的异常。遵循JSF2完整参考中的代码,我已经为我的处理程序创建了类。但是当我部署我的应用程序时,它会抛出下一个stackTrace:

SEVERE: Critical error during deployment: 
com.sun.faces.config.ConfigurationException: Factory 'javax.faces.context.ExceptionHandlerFactory' was not configured properly.
at com.sun.faces.config.processor.FactoryConfigProcessor.verifyFactoriesExist(FactoryConfigProcessor.java:305)
at com.sun.faces.config.processor.FactoryConfigProcessor.process(FactoryConfigProcessor.java:219)
at com.sun.faces.config.ConfigManager.initialize(ConfigManager.java:360)
at com.sun.faces.config.ConfigureListener.contextInitialized(ConfigureListener.java:225)
at org.apache.catalina.core.StandardContext.contextListenerStart(StandardContext.java:4750)
at com.sun.enterprise.web.WebModule.contextListenerStart(WebModule.java:550)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:5366)
at com.sun.enterprise.web.WebModule.start(WebModule.java:498)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:917)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:901)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:733)
at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:2019)
at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:1669)
at com.sun.enterprise.web.WebApplication.start(WebApplication.java:109)
at org.glassfish.internal.data.EngineRef.start(EngineRef.java:130)
at org.glassfish.internal.data.ModuleInfo.start(ModuleInfo.java:269)
at org.glassfish.internal.data.ApplicationInfo.start(ApplicationInfo.java:301)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:461)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:240)
at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:389)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$1.execute(CommandRunnerImpl.java:348)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:363)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1085)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1200(CommandRunnerImpl.java:95)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1291)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1259)
at com.sun.enterprise.v3.admin.AdminAdapter.doCommand(AdminAdapter.java:461)
at com.sun.enterprise.v3.admin.AdminAdapter.service(AdminAdapter.java:212)
at com.sun.grizzly.tcp.http11.GrizzlyAdapter.service(GrizzlyAdapter.java:179)
at com.sun.enterprise.v3.server.HK2Dispatcher.dispath(HK2Dispatcher.java:117)
at com.sun.enterprise.v3.services.impl.ContainerMapper$Hk2DispatcherCallable.call(ContainerMapper.java:354)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:662)
Caused by: javax.faces.FacesException: exception.ExceptionHandlerFactory
at javax.faces.FactoryFinder.getImplGivenPreviousImpl(FactoryFinder.java:643)
at javax.faces.FactoryFinder.getImplementationInstance(FactoryFinder.java:509)
at javax.faces.FactoryFinder.access$400(FactoryFinder.java:139)
at javax.faces.FactoryFinder$FactoryManager.getFactory(FactoryFinder.java:993)
at javax.faces.FactoryFinder.getFactory(FactoryFinder.java:343)
at com.sun.faces.config.processor.FactoryConfigProcessor.verifyFactoriesExist(FactoryConfigProcessor.java:303)
... 45 more
Caused by: java.lang.InstantiationException: exception.ExceptionHandlerFactory
at java.lang.Class.newInstance0(Class.java:342)
at java.lang.Class.newInstance(Class.java:310)
at javax.faces.FactoryFinder.getImplGivenPreviousImpl(FactoryFinder.java:641)
... 50 more

这是我的faces-config.xml:

<factory>
    <exception-handler-factory>
        exception.ExceptionHandlerFactory
    </exception-handler-factory>
</factory>

ExceptionHandlerFactory.java

public class ExceptionHandlerFactory extends javax.faces.context.ExceptionHandlerFactory {

private transient Logger logger = Logger.getLogger(this.getClass().getName());
private ExceptionHandlerFactory parent;

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

@Override
public ExceptionHandler getExceptionHandler() {
    ExceptionHandler result = parent.getExceptionHandler();
    result = new CustomExceptionHandler(result);
    return result;
}

}

CustomExceptionHandler.java

public class CustomExceptionHandler extends ExceptionHandlerWrapper {

private ExceptionHandler parent;

public CustomExceptionHandler(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();
        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 {

                fc.getExternalContext().
                        getFlash().put("currentViewId",
                        vee.getViewId());
                nav.handleNavigation(fc, null, "/login?faces-redirect=true");
                fc.renderResponse();
            } finally {
                i.remove();
            }
        } 
    }

    getWrapped().handle();
}
}

服务器是Glassfish 3.1.2.2。

当我得到ViewExpiredException时,我需要一些逻辑。我错过了什么? 在此先感谢!!!!

修改

正如@BalusC指出的那样,我改变了我的工厂类名,所以这是我的CustomExceptionHandlerFactory.java:

public class CustomExceptionHandlerFactory extends ExceptionHandlerFactory {

    private ExceptionHandlerFactory parent;

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

    @Override
    public ExceptionHandler getExceptionHandler() {
        ExceptionHandler result = parent.getExceptionHandler();
        result = new CustomExceptionHandler(result);
        return result;
    }
}

和我的faces-config.xml:

<factory>
    <exception-handler-factory>
        com.exception.CustomExceptionHandlerFactory
    </exception-handler-factory>
</factory>

我希望这对有这个愚蠢问题的人有所帮助。

1 个答案:

答案 0 :(得分:5)

您的错误在于,您出于某些不明原因重复使用与现有API类相同的类名,并将其混淆在代码中。

parent构造函数参数(和局部变量)必须是javax.faces.context.ExceptionHandlerFactory类型,而不是exception.ExceptionHandlerFactory类型。这种错误的类型导致JSF无法调用构造函数,因此-rather自我解释 - InstantiationException (无法实例化)。

最好将您的课程重命名为CustomExceptionHandlerFactory之类的独特内容。