<p:selectOneMenu id="tag1" value="#{attachmentManagement.tag1}" converter="#{stringFilterConverter}">
<f:selectItems value="#{attachmentManagement.tag1List}" var="tag1" itemLabel="#{not empty tag1.value ? tag1.value : '无'}"/>
</p:selectOneMenu>
<p:commandButton value="button" actionListener="#{attachmentManagement.someFuction()}"/>
When I click the button, this exception was throwed:
javax.faces.component.UpdateModelException: java.lang.IllegalArgumentException: Cannot convert com.bolan.fzsystem.appserver.view.model.StringFilter@17dbb2e of type class java.lang.String to class com.bolan.fzsystem.appserver.view.model.StringFilter
....
Caused by: java.lang.IllegalArgumentException: Cannot convert com.bolan.fzsystem.appserver.view.model.StringFilter@17dbb2e of type class java.lang.String to class com.bolan.fzsystem.appserver.view.model.StringFilter
....
stringFilterConverter and someFunction is not called at all. Since I have set a converter, why jsf not use it and use toString() to update the value.
My converter implementation is a CDI bean. I found the solution. I change the scope of converter from default to application scope. And now converter works. But why?
This is my converter implementation:
@Named
@ApplicationScoped //change scope from default to this
public class StringFilterConverter implements Converter, Serializable {
private static final long serialVersionUID = -6691961589607931061L;
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
if (value == null || value.isEmpty()) {
return ValueFilter.UNDEFINE;
}
return JSON.parseObject(value, new TypeReference<String>() {});
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
ValueFilter<String> filter = (ValueFilter<String>) value;
return JSON.toJSONString(filter);
}
}