2012-07-10 64 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(); 
    } 
} 

應該幫助

相關問題