当我在 try-catch 的 catch 块中添加FacesMessage
时,它不会显示在p:messages
组件中。如果我将FacesMessage
添加到另一个位置,则会显示它。为什么?
添加FacesMessage
:
try {
...
} catch (ServiceException | IOException e) {
LOG.error("Failed to load content for download", e);
MessageUtils.addErrorMessage(msgs, "msg.error.general", (Object[]) null);
}
public static void addErrorMessage(final MessagesProxy msgs, final String key, final Object... params)
{
final FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, msgs.getText(key, params), null);
final FacesContext fc = FacesContext.getCurrentInstance();
fc.addMessage(null, msg);
fc.validationFailed();
}
msgs
是对资源包的访问。
这是带有autoUpdate=true
p:messages
:
<p:messages id="mainMessagesId" autoUpdate="true" />
背景:UI 提供在后备 Bean 中处理的文件下载。如果用户选择一个大文件并在浏览器的另存为对话框中单击按钮取消,则会出现异常:
15:29:13,599 ERROR [xxx.web.bl.ContentBL] (http-/127.0.0.1:9090-1) Failed to load content for download: ClientAbortException: java.net.SocketException: Connection reset by peer: socket write error
at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:403) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.tomcat.util.buf.ByteChunk.flushBuffer(ByteChunk.java:450) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.tomcat.util.buf.ByteChunk.append(ByteChunk.java:351) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.catalina.connector.OutputBuffer.writeBytes(OutputBuffer.java:426) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.catalina.connector.OutputBuffer.write(OutputBuffer.java:415) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.catalina.connector.CoyoteOutputStream.write(CoyoteOutputStream.java:89) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.omnifaces.servlet.GzipHttpServletResponse$GzipThresholdOutputStream.write(GzipHttpServletResponse.java:204) [omnifaces-1.8.1.jar:1.8.1-20140603]
at org.omnifaces.io.ResettableBufferedOutputStream.write(ResettableBufferedOutputStream.java:77) [omnifaces-1.8.1.jar:1.8.1-20140603]
at org.omnifaces.io.ResettableBufferedOutputStream.write(ResettableBufferedOutputStream.java:56) [omnifaces-1.8.1.jar:1.8.1-20140603]
at org.omnifaces.servlet.HttpServletResponseOutputWrapper$1.write(HttpServletResponseOutputWrapper.java:92) [omnifaces-1.8.1.jar:1.8.1-20140603]
at java.io.OutputStream.write(OutputStream.java:116) [rt.jar:1.7.0_75]
at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:1793) [commons-io-2.4.jar:2.4]
at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:1769) [commons-io-2.4.jar:2.4]
at org.apache.commons.io.IOUtils.copy(IOUtils.java:1744) [commons-io-2.4.jar:2.4]
at xxx.web.bl.ContentBL.downloadContent(ContentBL.java:309) [classes:]
at xxx.web.bl.ContentBL$Proxy$_$$_WeldClientProxy.downloadContent(ContentBL$Proxy$_$$_WeldClientProxy.java) [classes:]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_75]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [rt.jar:1.7.0_75]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.7.0_75]
at java.lang.reflect.Method.invoke(Method.java:606) [rt.jar:1.7.0_75]
at javax.el.BeanELResolver.invokeMethod(BeanELResolver.java:735) [jboss-el-api_2.2_spec-1.0.2.Final-redhat-1.jar:1.0.2.Final-redhat-1]
at javax.el.BeanELResolver.invoke(BeanELResolver.java:467) [jboss-el-api_2.2_spec-1.0.2.Final-redhat-1.jar:1.0.2.Final-redhat-1]
at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:246) [jboss-el-api_2.2_spec-1.0.2.Final-redhat-1.jar:1.0.2.Final-redhat-1]
at de.odysseus.el.tree.impl.ast.AstMethod.eval(AstMethod.java:91) [juel-impl-2.2.7.jar:2.2.7]
at de.odysseus.el.tree.impl.ast.AstMethod.invoke(AstMethod.java:104) [juel-impl-2.2.7.jar:2.2.7]
at de.odysseus.el.tree.impl.ast.AstEval.invoke(AstEval.java:71) [juel-impl-2.2.7.jar:2.2.7]
at de.odysseus.el.TreeMethodExpression.invoke(TreeMethodExpression.java:132) [juel-impl-2.2.7.jar:2.2.7]
at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40) [weld-core-1.1.16.Final-redhat-1.jar:1.1.16.Final-redhat-1]
at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50) [weld-core-1.1.16.Final-redhat-1.jar:1.1.16.Final-redhat-1]
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) [jsf-impl-2.2.11.jar:2.2.11]
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87) [jsf-api-2.2.11.jar:2.2]
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102) [jsf-impl-2.2.11.jar:2.2.11]
at org.apache.myfaces.extensions.cdi.jsf.impl.security.SecurityViolationAwareActionListener.processAction(SecurityViolationAwareActionListener.java:56) [myfaces-extcdi-jsf20-module-impl-1.0.5.jar:1.0.5]
at org.apache.myfaces.extensions.cdi.jsf.impl.config.view.ViewControllerActionListener.processAction(ViewControllerActionListener.java:68) [myfaces-extcdi-jsf20-module-impl-1.0.5.jar:1.0.5]
at org.apache.myfaces.extensions.cdi.jsf.impl.listener.action.CodiActionListener.processAction(CodiActionListener.java:58) [myfaces-extcdi-jsf20-module-impl-1.0.5.jar:1.0.5]
at org.omnifaces.eventlistener.ResetInputAjaxActionListener.processAction(ResetInputAjaxActionListener.java:197) [omnifaces-1.8.1.jar:1.8.1-20140603]
at javax.faces.component.UICommand.broadcast(UICommand.java:315) [jsf-api-2.2.11.jar:2.2]
然后p:messages
没有显示任何内容。我正在使用素数 5.2.6 和 JSF 2.2.11。
问候奥利弗
大多数现代浏览器已经在后台下载文件,即使最终用户尚未决定是将文件保存在指定位置还是取消文件下载。取消显然会中止当前正在运行的下载请求。如果文件的下载速度快于最终用户决定取消的速度,那么服务器完全不会注意到它,浏览器将静默地将其从(临时)磁盘中删除,而不是移动/重命名它。
这种异常无法在服务器端以向最终用户显示消息的形式处理,原因很简单,不再有请求/响应的方法。客户端已中止请求/响应。没有响应可以写入消息。最好的选择是推送连接(例如,通过PrimeFaces <p:socket>
的websockets)。
就让例外过去吧。客户拥有一切。如有必要,请禁止记录特定ClientAbortException
,以免在服务器日志中受到它的困扰。
另请参阅:
- ClientAbortException at 部署在 jboss 使用 IE8 浏览器
- iframe 导致 ClientAbortException: java.io.IOException at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:369)
与具体问题无关,根据您使用 OmniFaces 的堆栈跟踪。如果您还不知道它,它有一个Messages
实用程序类,可以将您从MessageUtils
样板中拯救出来。