使用BalusC回答有关处理JSF ajax异常的多个问题我尝试过:
结果始终相同 - 对ajax事件期间的任何异常的响应是页面如下所示:
常规h:commandButton
重定向到登录页面的例外情况。这很奇怪,因为应该重定向到404.xhtml
反正: 这是我的web.xml:
<error-page>
<error-code>404</error-code>
<location>/errors/404.xhtml</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/errors/404.xhtml</location>
</error-page>
<error-page>
<exception-type>javax.faces.application.ViewExpiredException</exception-type>
<location>/errors/404.xhtml</location>
</error-page>
这是我的customExceptionHandler:
@Override
public void handle() throws FacesException {
final Iterator<ExceptionQueuedEvent> i = getUnhandledExceptionQueuedEvents().iterator();
while (i.hasNext()) {
ExceptionQueuedEvent event = i.next();
ExceptionQueuedEventContext context
= (ExceptionQueuedEventContext) event.getSource();
// get the exception from context
Throwable t = context.getException();
final FacesContext fc = FacesContext.getCurrentInstance();
//here you do what ever you want with exception
try {
//log error ?
logger.info("error thrown {}", t);
//redirect error page
fc.getExternalContext().redirect("http://www.o2.pl");
fc.renderResponse();
// remove the comment below if you want to report the error in a jsf error message
//JsfUtil.addErrorMessage(t.getMessage());
} catch (IOException e) {
e.printStackTrace();
} finally {
//remove it from queue
i.remove();
}
}
//parent hanle
getWrapped().handle();
}
加上我的服务器(wildfly 9.0.0)日志只有从customExceptionHandler记录的这条消息:
(default task-104) error thrown {}: javax.faces.application.ViewExpiredException: viewId:/login.xhtml - View /login.xhtml could not be restored.
at com.sun.faces.lifecycle.RestoreViewPhase.execute(RestoreViewPhase.java:212)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.RestoreViewPhase.doPhase(RestoreViewPhase.java:123)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:658)
at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:86)
我再次切换到omnifaces.FullAjaxExceptionHandler,这是服务器日志:
[org.omnifaces.exceptionhandler.FullAjaxExceptionHandler] (default task-27) FullAjaxExceptionHandler: An exception occurred during processing JSF ajax request. Error page '/errors/404.xhtml' will be shown.: javax.faces.application.ViewExpiredException: viewId:/login.xhtml - View /login.xhtml could not be restored.
据我所知,两个处理程序都被正确解雇,但不知何故,他们的响应已被破坏 所以最大的问题是 - 我做错了什么?我错过了什么?
编辑BalusC的项目描述
和我的web.xml的另一部分定义安全性:
<security-constraint>
<web-resource-collection>
<web-resource-name>Secure assets</web-resource-name>
<url-pattern>/secure/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>WRITER</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>FORM</auth-method>
<form-login-config>
<form-login-page>/login.xhtml</form-login-page>
<form-error-page>/login.xhtml</form-error-page>
</form-login-config>
</login-config>
编辑BalusC reauest我发布回复标题
Cache-Control:no-cache
Cache-Control:no-cache, no-store, must-revalidate
Cache-Control:no-cache, no-store, must-revalidate
Connection:keep-alive
Content-Type:text/xml;charset=UTF-8
Date:Tue, 22 Sep 2015 10:19:15 GMT
Expires:0
Expires:0
Pragma:no-cache
Pragma:no-cache
Server:WildFly/9
Transfer-Encoding:chunked
X-Powered-By:Undertow/1
编辑错误来源
将错误附加到按钮的ajax:
<h:commandLink value="#{msg.add}">
<f:ajax render=":addingModalBody"
execute="@this"
listener="#{myBean.setFields()}"
onevent="renderAddModalBody"/>
</h:commandLink>
此按钮的目标是在模态(渲染)的值渲染体上显示辅助bean(侦听器)的值,并显示模态(onevent)。