Adding custom attribute (HTML5) support to JSF 2.0 UIInput component

后端 未结 3 2029
执笔经年
执笔经年 2020-11-27 06:39

I am trying to write a renderer which would process the placeholder attribute on an component. I headed to this path after read

3条回答
  •  心在旅途
    2020-11-27 06:48

    You can just override ResponseWriters startElement method, that method is only called once and then you can restore to the original responsewriter object.

    import javax.faces.context.*;
    import java.io.IOException;
    
    public class InputRenderer extends com.sun.faces.renderkit.html_basic.TextRenderer{
    
          // Put all of the attributes you want to render here...
          private static final String[] ATTRIBUTES = {"required","placeholder"};
    
        @Override
        protected void getEndTextToRender(FacesContext context,
                UIComponent component, String currentValue) throws IOException {
            final ResponseWriter originalResponseWriter = context.getResponseWriter();
            context.setResponseWriter(new ResponseWriterWrapper() {
    
                @Override
    // As of JSF 1.2 this method is now public.
                public ResponseWriter getWrapped() {
                    return originalResponseWriter;
                }   
    
                @Override
                public void startElement(String name, UIComponent component)
                        throws IOException {
                    super.startElement(name, component);
    if ("input".equals(name)) {
      for (String attribute : ATTRIBUTES)
      {
        Object value = component.getAttributes().get(attribute);
        if (value != null)
        {
          super.writeAttribute(attribute,value,attribute);
    } 
      }
    }   
            });
            super.getEndTextToRender(context, component, currentValue);
            context.setResponseWriter(originalResponseWriter); // Restore original writer.
        }
    
    
    
    }
    

提交回复
热议问题