Reputation: 371
I have the following as annotation for the custom converter
@FacesConverter(value="capsConverter", forClass=String.class)
public class CapsConverter implements Converter{
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
return value.toUpperCase();
}
@Override
public String getAsString(FacesContext context, UIComponent component,Object value) {
if(value!=null && value instanceof String){
String s = (String)value;
s = WordUtils.capitalize(s.toLowerCase());
return s;
}
return null;
}
}
Issue is, the converter sometimes gets called even when I didnt explicitly call it in my page, does the forClass have anything to do with the interference for my inputTexts, should I be using value only and remove forClass=String.class ?
Any shed of light is highly appreciated.
Upvotes: 2
Views: 600
Reputation: 1109745
It are actually two distinct ways of registering a converter. Both are mutually exclusive. They have no influence on each other and you can omit one or the other.
The forClass
attribute will register the converter in the JSF application to kick in during processing and rendering of every model value of the specified type. In other words, with forClass=String.class
, the converter will be implicitly invoked for every String
typed input and output, unless the associated input/output component has already an explicit converter declared.
Generally you use forClass
only for custom/complex types, not for standard types such as String
, Long
, BigDecimal
, etc for which JSF has already builtin implicit converters.
So you should indeed remove it and rely on converter ID only.
@FacesConverter("capsConverter")
<h:someComponent ... converter="capsConverter" />
An example of correct appliance of forClass
would be the following converter between a Person
entity and a String
representing its ID
@FacesConverter(forClass=Person.class)
on something like
<f:viewParam name="id" value="#{bean.person}" />
which converts an incoming request parameter representing an entity ID like below
/edit-person.xhtml?id=42
to a fullworthy entity property in bean (without needing any additional logic in bean)
private Person person;
Upvotes: 4