Validation JSF2,RichFaces 4,rich:select,如果没有更改,则返回null

Validation JSF2,RichFaces 4,rich:select,如果没有更改,则返回null,validation,jsf-2,combobox,richfaces,Validation,Jsf 2,Combobox,Richfaces,我使用JSF2、RichFaces4、rich:select。允许手动输入,用户可以输入与任何预定义项目标签不对应的内容。在这种情况下,如何设置value=null(如果未选择任何项) 使用如下自定义项: package com.whatever.conveters; import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; import javax.faces.convert.Con

我使用JSF2、RichFaces4、rich:select。允许手动输入,用户可以输入与任何预定义项目标签不对应的内容。在这种情况下,如何设置value=null(如果未选择任何项)


使用如下自定义项:

package com.whatever.conveters;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

// If you are using spring...
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

// Again, if you are using spring...
@Component("testConverter")
@Scope("request")
public class TestConverter implements Converter {

    // Resolve this collection either by injection or another method
    private Map<Country> countries;

    @Override
    public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value) {
        // Check if the "value" is in the collection of items  
        //
        // Here I'm using a map, but you can inject a service 
        // an check if the value is contained in by one of the service methods
        if (countries.contains(value)) { 
            return value;
        } else {
            return null;
        }
    }

    @Override
    public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value) {
        // Here, just return the toString of the item
        return value.toString();
    }
}
package com.whatever.converters;
导入javax.faces.component.UIComponent;
导入javax.faces.context.FacesContext;
导入javax.faces.convert.Converter;
//如果您正在使用spring。。。
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.context.annotation.Scope;
导入org.springframework.stereotype.Component;
//同样,如果您使用的是spring。。。
@组件(“测试转换器”)
@范围(“请求”)
公共类TestConverter实现转换器{
//通过注入或其他方法解析此集合
私有地图国家;
@凌驾
公共对象getAsObject(FacesContext FacesContext,UIComponent UIComponent,字符串值){
//检查“值”是否在项目集合中
//
//这里我使用的是一个映射,但是您可以注入一个服务
//检查某个服务方法是否包含该值
如果(countries.contains(value)){
返回值;
}否则{
返回null;
}
}
@凌驾
公共字符串getAsString(FacesContext FacesContext、UIComponent UIComponent、对象值){
//在这里,只需返回项目的toString
返回值.toString();
}
}
这应该会有帮助

package com.whatever.conveters;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

// If you are using spring...
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

// Again, if you are using spring...
@Component("testConverter")
@Scope("request")
public class TestConverter implements Converter {

    // Resolve this collection either by injection or another method
    private Map<Country> countries;

    @Override
    public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value) {
        // Check if the "value" is in the collection of items  
        //
        // Here I'm using a map, but you can inject a service 
        // an check if the value is contained in by one of the service methods
        if (countries.contains(value)) { 
            return value;
        } else {
            return null;
        }
    }

    @Override
    public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value) {
        // Here, just return the toString of the item
        return value.toString();
    }
}