Trim Leading and Trailing spaces from InputText on tab out.

Hi,

Here I would like to explain one use case to remove Leading and Trailing spaces from input text.

Use case: On tab out of input text box, leading and trailing spaces should be removed.

To implement this use case I have written custom converter which will do this work. But I need to refresh my component inside the converter code in order to reflect the changes on tab out. There are many use ways to implement this. Below is my way.

Create Custom Converter class by implementing javax.faces.convert.Converter

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import org.apache.myfaces.trinidad.context.RequestContext;

public class TrimEndSpacesConverter implements Converter{
    public TrimEndSpacesConverter() {
        super();
    }
    
    public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String string) 
      {
            if (string != null) {
                // trim the entered value
                       RequestContext adfContext = RequestContext.getCurrentInstance();
                       adfContext.addPartialTarget(uiComponent);
                 return string.trim();
                   }

                   return string;
      }

      public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object object)
      {
            if (object != null) {
                RequestContext adfContext = RequestContext.getCurrentInstance();
                adfContext.addPartialTarget(uiComponent);
                 return object.toString().trim();
                }
                   return null;
      }
}

Now register this class in faces-config.xml as converter.












Now use this converter tag in inputText component.

<af:inputText label="Username" id="pt_it1" value="#{sessionBean.username}" autoSubmit="true">
 <f:converter converterId="TrimEndSpaces"/>
 </af:inputText>

Cheers :) :) :) 

Comments

Popular posts from this blog

BEA-000362 Server failed. Reason: [Management:141268]Parsing Failure in config.xml

Oracle OIM - Search and Update Organization using java API