如何在JSF 2.0中及早刷新缓冲区? [英] How to flush buffer early in JSF 2.0?
问题描述
我正在使用JSF 2.0,并且希望尽早(在头部和响应之间)刷新缓冲区,因为这是提高性能的最佳实践之一.在此文章 BalusC演示了如何在JSP页面(<% response.getWriter().flush(); %>
),但是我正在使用xhtml页面.
有人知道怎么做吗?
I'm using JSF 2.0 and I want to flush the buffer early (between head and response) as it is one of the best practices to improve performance. In this article BalusC shows how to do in in a JSP page (<% response.getWriter().flush(); %>
), but I'm using xhtml pages.
Does anyone know how to do it?
谢谢!
达米安
Thanks!
Damian
推荐答案
如果您在JSF2中使用<h:head>
,则可以理论上通过显式刷新基础servlet输出流来做到这一点.在与组件关联的Renderer
的encodeEnd()
方法中.
If you're using <h:head>
in JSF2, then you can in theory do this by explicitly flushing the underlying servlet output stream in the encodeEnd()
method of the Renderer
associated with the component.
基本上有两种方法可以实现自定义渲染器.如果要独立于实现,则您自己编写了整个Renderer
,这是一项工作(尽管对于简单的HTML <head>
元素来说,这并不难).您还可以选择特定于实现,以便最终得到一个更简单的自定义Renderer
实现.假设您使用的是Mojarra,则只需扩展com.sun.faces.renderkit.html_basic.HeadRenderer
.
There are basically two ways to implement a custom renderer. If you want to be implementation independent, then you have write the entire Renderer
yourself which is a piece of work (although for a simple HTML <head>
element this is not that hard). You can also choose to be implementation specific so that you end up with a simpler custom Renderer
implementation. Assuming that you're using Mojarra, then you just have to extend the com.sun.faces.renderkit.html_basic.HeadRenderer
.
例如
package com.example;
import java.io.IOException;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import com.sun.faces.renderkit.html_basic.HeadRenderer;
public class FlushedHeadRenderer extends HeadRenderer {
@Override
public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
super.encodeEnd(context, component);
context.getExternalContext().getResponseOutputWriter().flush(); // Forces flush.
}
}
,然后您在faces-config.xml
中进行如下注册:
which you then register as follows in faces-config.xml
:
<render-kit>
<renderer>
<component-family>javax.faces.Output</component-family>
<renderer-type>javax.faces.Head</renderer-type>
<renderer-class>com.example.FlushedHeadRenderer</renderer-class>
</renderer>
</render-kit>
但是,这种方法有一些警告. JSF不再有机会在必要时创建会话,或在呈现视图时抛出异常时将任何异常作为错误页面进行处理. 我不建议JSF使用这种方法.
However, this approach has caveats. JSF does not have the opportunity anymore to create a session whenever necessary, or to handle any exception as error page whenever an exception is been thrown in midst of rendering the view. I would not recommend this approach for JSF.
这篇关于如何在JSF 2.0中及早刷新缓冲区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!