开发者

JSF inputText NumberConverter store empty string as null

开发者 https://www.devze.com 2023-02-27 04:10 出处:网络
<h:inputText value=\"#{testBean.b}\"><f:converter converterId=\"dc\" /></h:inputText>
<h:inputText value="#{testBean.b}"><f:converter converterId="dc" /></h:inputText>

@SessionScoped
@ManagedBean
class TestBean{
    private Double b;
    //getter-setter
}

I have a requirement to store empty values as null and 0 as 0 in a double data type

I have overridden NumberConverter to return null if the string(in textbox) is null or empty The code in the if part is executed and retuning null, but the setter(testBean.setB) is called with value 0.

  public class DoubleFormatter extends Numbe开发者_开发技巧rConverter {
   @Override
 public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String param){
     if(param ==null || param.trim().length()==0){
      System.out.println("returning null");
      return null;
     }
     return super.getAsObject(facesContext, uiComponent, param);
    }

    @Override
 public String getAsString(FacesContext facesContext, UIComponent uiComponent,Object obj) {
     if(obj ==null)
      return "";
     return super.getAsString(facesContext, uiComponent, obj);
    }
    }

The javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL context param is not useful for me as I want to apply this on specific fields only.


Customer Converter(DoubleFormatter) is working in glashfish3 ,but not with tomcat6+mojarra(2.1.0 and 2.0.3

0

精彩评论

暂无评论...
验证码 换一张
取 消