2011-10-11 127 views
1

我想爲簡單文本輸入添加HTML5屬性。因此,我想裝飾h:inputText的標準渲染器。我的解決方案基於繼承自Mojarra特有的com.sun.faces.renderkit.html_basic.TextRenderer。有沒有更好的方法來裝飾現有的渲染器而無需綁定到特定的實現,並且不必重新實現基本渲染器的功能?裝飾渲染器

這是到目前爲止我的解決方案:

@FacesRenderer(componentFamily=Html5InputRenderer.COMPONENT_FAMILY, rendererType=Html5InputRenderer.COMPONENT_TYPE) 
public class Html5InputRenderer extends TextRenderer { 

public static final String COMPONENT_FAMILY = "javax.faces.Input"; 

public static final String COMPONENT_TYPE = "javax.faces.Text"; 

public static final String RENDERED_TYPE_NAME = "renderedType"; 

@Override 
public void encodeBegin(final FacesContext context, final UIComponent component) throws IOException { 

    if (component.getAttributes().containsKey(RENDERED_TYPE_NAME)) { 

     ResponseWriter writer = context.getResponseWriter(); 
     StringWriter buffer = new StringWriter(); 

     String replacement; 

     try { 
      ResponseWriter clonedWriter = writer.cloneWithWriter(buffer); 
      context.setResponseWriter(clonedWriter); 
      super.encodeBegin(context, component); 

      String renderedMarkup = buffer.toString(); 

      // do some stuff with markup rendered by the basic renderer 

      clonedWriter.close(); 

      if (null != replacement) { 
       writer.write(replacement); 
      } 

     } finally { 
      context.setResponseWriter(writer); 
     } 

    } else { 
     super.encodeBegin(context, component); 
    } 
} 

THX,DS

回答

0

您可以通過providing your own render kit裝飾渲染器。爲此,請創建一個帶公共構造函數的RenderKitFactory,該構造函數接受類型爲RenderKitFactory的參數。在faces-config.xml中定義新的RenderKitFactory。在初始化期間,JSF框架將把先前配置的RenderKitFactory傳遞給你的構造函數。

有關更多詳細信息,請參閱JSF spec

+0

我明白了。我可以同時使用原始的RenderKitFactory和RenderKit來獲取原始Renderer的實例。然後將渲染委託給Renderer實例。 Thx,這應該可以做到。 – Darkspirit