0
<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);
    }
}
vipcxj
  • 402
  • 2
  • 8
  • Please read this very interesting post [CDI Bean](http://stackoverflow.com/questions/4347374/backing-beans-managedbean-or-cdi-beans-named) – Yagami Light Jan 04 '17 at 08:47

1 Answers1

0

In your code snippet you define converter as converter="#{stringFilterConverter}" however I think that you shouldn't wrap the name of your converter with EL expression syntax, so proper usage of converter should be :

<p:selectOneMenu id="tag1" value="#{attachmentManagement.tag1} converter="stringFilterConverter"/> 

(of course I assume that your converter implementation class has annotation @FacesConverter("stringFilterConverter"))

Artur Skrzydło
  • 995
  • 11
  • 32
  • 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? – vipcxj Jan 04 '17 at 07:12
  • Could you update your answer with converter implementation? – Artur Skrzydło Jan 04 '17 at 07:28