h:body在使用FullAjaxExceptionHandler时未重新呈现 [英] h:body not rerendered when using FullAjaxExceptionHandler
问题描述
我正在使用OmniFaces FullAjaxExceptionHandler 来显示错误页面.错误页面可以正确显示,但是这些页面的样式存在问题.
I'm using the OmniFaces FullAjaxExceptionHandler to display error pages. The error pages are shown correctly, but I'm having issues with the styling of those pages.
我的应用程序使用的模板具有在body元素上定义的CSS类.对于正常页面和错误页面,这些类是不同的:
My application is using a template which has CSS classes defined on the body element. These classes are different for normal and error pages:
正常页面:
<h:body styleClass="main-body layout-compact">
错误页面:
<h:body styleClass="exception-body error-page">
当FullAjaxExceptionHandler处理异常时,将转发到错误页面(基于web.xml
中的<error-page>
机制).显然,这不会重新呈现<h:body>
标记,因为在检查HTML输出时,我可以看到<body>
标记仍包含正常页面的CSS类(而不是错误页面的类).
When the FullAjaxExceptionHandler processes an exception, a forward to the error page is performed (based on the <error-page>
mechanism in web.xml
). Apparently this does not rerender the <h:body>
tag, because when checking the HTML output, I can see that the <body>
tag still contains the CSS classes from the normal page (instead of the classes of the error page).
似乎原始<h:body>
的内容已替换为错误页面<h:body>
的内容,而不仅仅是替换了完整的<h:body>
.我不知道这是否是默认的JSF/FullAjaxExceptionHandler行为.
It seems that the content of the original <h:body>
is replaced with the content of the error page <h:body>
instead of just replacing the full <h:body>
. I don't know if this is default JSF / FullAjaxExceptionHandler behaviour.
是否可以用正确的CSS类渲染<h:body>
?不能将CSS类从<h:body>
移开.
Is there any way to have the <h:body>
rendered with the correct CSS classes? Moving the CSS classes away from <h:body>
is not an option.
推荐答案
不幸的是,这是设计使然".在执行Ajax导航时,JSF不会替换整个文档,而是仅替换单个<head>
和<body>
元素的子元素,而父元素保持不变.这样做是出于历史原因;较旧的Internet Explorer版本即不支持完全替换它们.
This is unfortunately "by design". JSF doesn't replace the entire document when performing ajax navigation, but it only replaces the children of individual <head>
and <body>
elements, leaving the parents untouched. This is done so for historical reasons; older Internet Explorer versions namely doesn't support replacing them altogether.
我自己所做的就是将样式简单地放入<main>
元素中.在最终的HTML输出中,<header>
和<footer>
通常通常是相同的.基本上:
What I have done myself is to simply put the style into the <main>
element instead. The <header>
and <footer>
are usually identical anyway in the final HTML output. Basically:
<html>
<head>
<title>...</title>
</head>
<body>
<header>...</header>
<main class="#{page.type}">...</main>
<footer>...</footer>
</body>
</html>
如果您确实需要修改<body class>
,那么最好的选择是通过嵌入在错误页面模板中的JavaScript来实现.
If you really need to have the <body class>
modified, then your best bet is to do so via JavaScript embedded in the error page template.
<h:outputScript rendered="#{faces.ajaxRequest}">
document.body.className = "exception-body error-page";
</h:outputScript>
注意:#{faces}
仅从OmniFaces 2.5开始可用,如果您使用的是旧版本,请改用#{facesContext.partialViewContext.ajaxRequest}
).
Note: #{faces}
is only available since OmniFaces 2.5, if you're using an older version, use instead #{facesContext.partialViewContext.ajaxRequest}
).
这篇关于h:body在使用FullAjaxExceptionHandler时未重新呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!