由于ViewExpiredException导致从CustomExceptionHandler导航后显示的部分响应xml

时间:2015-05-28 07:24:38

标签: ajax jsf primefaces navigation partial-response

您好我正在使用Primefaces 4.0

开发JSF 2.2.4项目

我正在尝试使用以下步骤复制ViewExpiredException。

- 转到浏览器登录应用程序

- 打开另一个标签,点击p:menuitem(例如会计屏幕)

- 重新启动服务器(其中一个选项卡会自动重定向到登录页面)

- 再次登录应用程序,然后再次单击相同的p:menuitem [突然浏览器显示部分响应xml而不是jsf页面]

浏览器上显示的XML:

<partial-response>
  <redirect url="/myapplication/denied/viewexpiredpage.jsf"/>
</partial-response>

CustomExceptionHandler.java

public class CustomExceptionHandler extends ExceptionHandlerWrapper {

private Logger logger = LoggerFactory.getLogger(CustomExceptionHandler.class);

private ExceptionHandler wrapped;

CustomExceptionHandler(ExceptionHandler exception) {
    this.wrapped = exception;
}

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

@Override
public void handle() throws FacesException {
    FacesContext fc = FacesContext.getCurrentInstance();
    Map<String, Object> requestMap = fc.getExternalContext().getRequestMap();
    NavigationHandler nav = fc.getApplication().getNavigationHandler();

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

        try {
            if (t instanceof ViewExpiredException) {
                logger.error("Custom Exception Handler caught an error: " + t.getMessage());
                fc.setViewRoot(fc.getApplication().getViewHandler().createView(fc, "expiredpage"));
                nav.handleNavigation(fc, null, "expiredpage");
                fc.getPartialViewContext().setRenderAll(true);
                fc.renderResponse();
            }else{     
                logger.error("Custom Exception Handler caught an error: " + t.getMessage());
            }
        }finally{
            i.remove();
        }
    }
    // At this point, the queue will not contain any ViewExpiredEvents.
    // Therefore, let the parent handle them.
    getWrapped().handle();
}
}

面-config.xml中

<factory>
    <exception-handler-factory>
        com.pemc.crss.web.commons.exceptionhandler.CustomExceptionHandlerFactory
    </exception-handler-factory>
</factory>

<navigation-rule>
    <from-view-id>*</from-view-id>
    <navigation-case>
        <from-outcome>expiredpage</from-outcome>
        <to-view-id>/denied/viewexpiredpage.jsf</to-view-id>
        <redirect />
    </navigation-case>
</navigation-rule>

ViewExpiredException已在异常处理程序中捕获,部分响应的url似乎是正确的但是出于某些奇怪的原因,为什么它显示部分响应xml而不是实际的jsf错误页面?

任何帮助澄清这个问题的人都会非常感激。谢谢!

1 个答案:

答案 0 :(得分:0)

ViewHandler#createView()不接受导航案例(&#34;已过期页&#34;您有)。您应该具有的是错误页面的实际视图ID或相对路径:

fc.getApplication().getViewHandler().createView(fc, "/myapplication/denied/viewexpiredpage.jsf");