Search code examples
javajsfjsf-2converters

JSF Converter warning


From my xhtml file:

<h:inputTextarea value="#{newPostForm.post.body}">
     <f:converter converterId="NL2BRConverter" />
</h:inputTextarea>

From my java file:

@FacesConverter(forClass=String.class)
public class NL2BRConverter implements Converter {

@Override
public Object getAsObject(FacesContext ctx, UIComponent comp, String str) {
    return str.replaceAll("\r\n+", "<br />");
    //return str.replaceAll("\r\n+", "&#13");
}

@Override
public String getAsString(FacesContext ctx, UIComponent comp, Object obj) {
    return obj.toString();
}

}

Eclipse is giving me a warning in my xhtml file that 'NL2BRConverter' converterID is not registered.

I've tried replacing the converter annotation with

@FacesConverter("NL2BRConverter")

but the error persists. Is this not sufficient to register a converter in JSF2.0 ?

Currently if I used the full class name "com.tracker.converter.NL2BRConverter" as the annotated name and the converterID in my XHTML files, it works. I still get that warning however...


Solution

  • You don't need a <f:converter> because your converter is already explicitly been declared by forClass=String.class to run on every String input type.

    If your actual intent is to declare it explicitly for specific input fields in the view yourself, then you should instead be using

    @FacesConverter(value="NL2BRConverter")
    

    Then you can use

    <f:converter converterId="NL2BRConverter" />