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

时间:2012-07-10 10:33:59

标签: validation jsf-2 combobox richfaces

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

<rich:select id="select"  valueChangeListener="#{comboBoxBean.valueChangedGo}"
        enableManualInput="true" required="false">
    <f:selectItem itemValue="0" itemLabel="Russia" />
    <f:selectItem itemValue="1" itemLabel="Ukraine" />
    <f:selectItem itemValue="2" itemLabel="Norway" />
    <f:selectItem itemValue="3" itemLabel="Sweden" />
    <f:selectItem itemValue="4" itemLabel="Finland" />
    <f:selectItem itemValue="4" itemLabel="Belarus" />
    <f:ajax render="count2"/>

</rich:select>

1 个答案:

答案 0 :(得分:0)

使用这样的自定义JSF Converter

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();
    }
}

应该有帮助