0

我使用 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>
4

1 回答 1

0

像这样使用自定义JSF 转换器

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

它应该有帮助

于 2012-10-17T15:50:04.283 回答