JSF javax.faces.convert Converter getAsString Object null

泪湿孤枕 提交于 2019-12-23 04:52:30

问题


I am working on an application and I used JSF within this application, I wrote a Java class which implements this interface JSF javax.faces.convert, and also overwrite the method getAsString of Converter, here is the Java doc of this method:

java.lang.String getAsString(FacesContext context,
                             UIComponent component,
                             java.lang.Object value)

But sometimes, the value here is Null, sometimes it works well,

does anybody know that why this value is null here? how to prevent it to happen?


回答1:


It will be null if the model value is null. For example,

public class SomeBean {

    private SomeObject someObject;

    // Let's assume, someObject is never initialized and defaults to null.
}

If you use

<h:outputText value="#{someBean.someObject}" converter="someConverter" />

then the getAsString() will be invoked with null value.

Also, if you use for example,

<h:selectOneMenu ... converter="someConverter">
    <f:selectItem itemValue="#{null}" itemLabel="Please select ..." />
    <f:selectItems value="#{data.availableItems}" />
</h:selectOneMenu>

then the getAsString() will be invoked with null value for the "Please select..." item.

If you're facing NullPointerException, then it's actually a bug in your own converter implementation. You can't prevent the supplied value from being null. Even more, the javadoc which you apparently already found yourself, also explicitly tells that the model object value may be null.

value - Model object value to be converted (may be null)

You should just be checking on that yourself. Here's a typical example how a converter should look like for a fictive User entity:

@Override
public Object getAsObject(FacesContext context, UIComponent component, String submittedValue) {
    if (submittedValue == null || submittedValue.isEmpty()) {
        return null;
    }

    try {
        return userService.find(Long.valueOf(submittedValue));
    } catch (NumberFormatException e) {
        throw new ConverterException(new FacesMessage(String.format("%s is not a valid User ID", submittedValue)), e);
    }
}

@Override
public String getAsString(FacesContext context, UIComponent component, Object modelValue) {
    if (modelValue == null) {
        return "";
    }

    if (modelValue instanceof User) {
        return String.valueOf(((User) modelValue).getId());
    } else {
        throw new ConverterException(new FacesMessage(String.format("%s is not a valid User", modelValue)), e);
    }
}



回答2:


Take a look converter

public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object o)
  {
    if (o == null)
      {
         return "";
      }
    Value p = (Value) o;
    if (p.getId() != null)
      {
        return p.getId().toString();
      }
    return (p.toString());
  }


来源:https://stackoverflow.com/questions/19088241/jsf-javax-faces-convert-converter-getasstring-object-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!