2012-07-10 53 views
0

我使用JSF2,RichFaces 4,rich:select。手动输入是允许的,用户可以输入与任何预定义项目标签不符的东西。在这种情况下如何设置value = null(如果没有选择任何项目)?JSF2,RichFaces 4,rich:select,如果没有变化,则返回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> 

回答

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

应该帮助