How to write a custom converter for

后端 未结 6 550
隐瞒了意图╮
隐瞒了意图╮ 2020-11-29 00:59

How can I write a custom converter when working with PrimeFaces components that use a list of POJO? My particular problem is with

<         


        
6条回答
  •  无人及你
    2020-11-29 01:57

    Yes, it's possible:

    public class DocumentSBean sBean implements Serializable{
    
    private List projects;
    // projects methods...
    // ...
    
    public Converter getDocumentConverter(){
     return docConverter;
    }
    
    private Converter docConverter = new Converter() {
    
            @Override
            public Object getAsObject(FacesContext context, UIComponent component, String value) {
                return projects.stream().filter(p -> p.getName().equals(value)).findFirst().orElse(null);
            }
    
            @Override
            public String getAsString(FacesContext context, UIComponent component, Object value) {
                return (value != null)
                        ? ((Document) value).toString()
                        : null;
            }
        };
    }
    
    
                                                            
提交回复
热议问题