Using a “Please select” f:selectItem with null/empty value inside a p:selectOneMenu

后端 未结 6 1057
粉色の甜心
粉色の甜心 2020-11-28 10:08

I\'m populating a from database as follows.



        
6条回答
  •  执笔经年
    2020-11-28 10:31

    This is fully working to me :

    
    
        
    
        
    
        
    
    

    The Converter

    @Controller
    @Scope("request")
    public final class CountryConverter implements Converter {
    
        @Autowired
        private final transient Service service = null;
    
        @Override
        public Object getAsObject(FacesContext context, UIComponent component, String value) {
            if (value == null || value.trim().equals("")) {
                return null;
            }
            //....
            // No change
        }
    
        @Override
        public String getAsString(FacesContext context, UIComponent component, Object value) {
            return value == null ? null : value instanceof Country ? ((Country) value).getCountryId().toString() : null;
            //**** Returns an empty string, when no Country is found ---> wrong should return null, don't care about the rendering.
        }
    }
    

提交回复
热议问题