【问题标题】:JSF2, RichFaces 4, rich:select, Return null if nothing changedJSF2,RichFaces 4,rich:select,如果没有改变则返回 null
【发布时间】:2012-07-09 19:41:30
【问题描述】:

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

【问题讨论】:

    标签: validation jsf-2 combobox richfaces


    【解决方案1】:

    像这样使用自定义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();
        }
    }
    

    应该有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-01
      • 2012-11-21
      • 1970-01-01
      相关资源
      最近更新 更多