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