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

后端 未结 3 2027
执笔经年
执笔经年 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 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.

     
        
            javax.faces.Input
            javax.faces.Text
            your.package.InputRenderer
        
    
    

提交回复
热议问题