2

嗨,我正在使用 Primefaces 4.0 开发 JSF 2.2.4 项目

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

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

- 打开另一个选项卡,单击 ap: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 错误页面?

任何有助于阐明这个问题的帮助将不胜感激。谢谢!

4

1 回答 1

0

ViewHandler#createView()不接受导航案例(您那里的“过期页面”)。相反,您应该拥有的是错误页面的实际视图 ID 或相对路径:

fc.getApplication().getViewHandler().createView(fc, "/myapplication/denied/viewexpiredpage.jsf");
于 2015-05-29T14:10:19.903 回答