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

后端 未结 3 2001
执笔经年
执笔经年 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:44

    And to override for MyFaces 2.0.8+

    package com.hsop.abc.eld;
    
    import java.io.IOException;
    
    import javax.faces.component.UIComponent;
    import javax.faces.context.FacesContext;
    import javax.faces.context.ResponseWriter;
    
    import org.apache.myfaces.renderkit.html.HtmlTextRenderer;
    
    public class InputRenderer extends HtmlTextRenderer
    {
        @Override
        protected void renderInputBegin(FacesContext context, UIComponent component)
                throws IOException
        {
            // TODO Auto-generated method stub
            super.renderInputBegin(context, component);
    
        Object placeholder = component.getAttributes().get("placeholder");
        if(placeholder != null) { 
            ResponseWriter writer = context.getResponseWriter();
            writer.writeAttribute("placeholder", placeholder, "placeholder");
        }
    
        }
    }
    
    0 讨论(0)
  • 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.
        }
    
    
    
    }
    
    0 讨论(0)
  • 2020-11-27 07:08

    This is my way. I added placeholder and data-theme attributes. If you want to add more attributes, you should just add its name to attributes array.

    import javax.faces.component.UIComponent;
    import javax.faces.context.FacesContext;
    import javax.faces.context.ResponseWriter;
    
    import com.sun.faces.renderkit.html_basic.TextRenderer;
    
    public class InputRender extends TextRenderer {
    
        @Override
        protected void getEndTextToRender(FacesContext context,
                UIComponent component,
                String currentValue)
         throws java.io.IOException{
    
            String [] attributes = {"placeholder","data-theme"};
    
            ResponseWriter writer = context.getResponseWriter();
    
            for(String attribute : attributes)
            {
                String value = (String)component.getAttributes().get(attribute);
                if(value != null) {                             
                    writer.writeAttribute(attribute, value, attribute);
                }
            }
    
            super.getEndTextToRender(context, component, currentValue);
    
        }
    
    }
    

    You should add this to faces-config.xml file.

     <render-kit>
        <renderer>
            <component-family>javax.faces.Input</component-family>
            <renderer-type>javax.faces.Text</renderer-type>
            <renderer-class>your.package.InputRenderer</renderer-class>
        </renderer>
    </render-kit>
    
    0 讨论(0)
提交回复
热议问题