2011-05-10 64 views
3

在我的JSF應用程序,我知道如何使用JSF轉換器來驗證用戶輸入針對一個模式在我ice:selectInputDate如何根據多種模式驗證輸入日期?

<f:convertDateTime pattern="MM/dd/yyyy" /> 

,但我應該怎麼做,如果我想要讓用戶能夠在輸入一個日期格式:「MM-dd-yyyy」呢?

我認爲這可以通過jsf擴展DateConverter來完成,但我已經嘗試過了,我失敗了。你有一個有效的例子驗證輸入日期針對多種模式?

謝謝。

UPDATE:我使用JSF 1.2

+0

JSF 1.x or 2.x? – BalusC 2011-05-10 15:12:38

+0

@BalusC - 我在這個項目中使用JSF 1.2 ... – 2011-05-10 15:27:08

+0

謝謝,它畢竟不是很相關,唯一的區別就是能否使用'@ FacesConverter'。無論如何,請經常提及JSF impl /版本以後的問題:) – BalusC 2011-05-10 15:29:50

回答

5

創建該組件由<f:attribute>接受多個圖案的定製轉換器。

這裏是你想怎麼有視圖的樣子:

<h:inputText id="input" value="#{bean.date}"> 
    <f:converter converterId="multiDateConverter" /> 
    <f:attribute name="pattern1" value="MM/dd/yyyy" /> 
    <f:attribute name="pattern2" value="MM-dd-yyyy" /> 
</h:inputText> 

而這裏的轉換器可看怎麼樣(對JSF 1.x中,將其註冊爲

<converter-id>multiDateConverter</converter-id> 

faces-config.xml代替)

@FacesConverter(value="multiDateConverter") 
public class MultiDateConverter implements Converter { 

    @Override 
    public Object getAsObject(FacesContext context, UIComponent component, String value) throws ConverterException { 
     List<String> patterns = getPatterns(component); 
     Date date = null; 

     for (String pattern : patterns) { 
      SimpleDateFormat sdf = new SimpleDateFormat(pattern); 
      sdf.setLenient(false); // Don't parse dates like 33-33-3333. 

      try { 
       date = sdf.parse(value); 
       break; 
      } catch (ParseException ignore) { 
       // 
      } 
     } 

     if (date == null) { 
      throw new ConverterException(new FacesMessage("Invalid date format, must match either of " + patterns)); 
     } 

     return date; 
    } 

    @Override 
    public String getAsString(FacesContext context, UIComponent component, Object value) throws ConverterException { 
     return new SimpleDateFormat(getPatterns(component).get(0)).format((Date) value); 
    } 

    private static List<String> getPatterns(UIComponent component) { 
     List<String> patterns = new ArrayList<String>(); 

     for (int i = 1; i < Integer.MAX_VALUE; i++) { 
      String pattern = (String) component.getAttributes().get("pattern" + i); 

      if (pattern != null) { 
       patterns.add(pattern); 
      } else { 
       break; 
      } 
     } 

     if (patterns.isEmpty()) { 
      throw new IllegalArgumentException("Please provide <f:attribute name=\"patternX\"> where X is the order number"); 
     } 

     return patterns; 
    } 

} 

注意,只挑選第一個(默認)模式來重新顯示 價值。因此,在上例中,如果輸入05-10-2011,則會重新顯示爲05/10/2011


無關的具體問題,模式MM-dd-yyyy是不是很常見。你不是想要用dd-MM-yyyy

+0

非常感謝。我完全像你說的那樣,也註冊了轉換器,但似乎並沒有因爲某種原因而被調用......(我在'getAsObject'和'getAsString'方法中添加了日誌記錄,但它沒有顯示給jboss日誌)。我在'ice:selectInputDate'組件中使用了這個f:轉換器。如果我把我的初始f:converDateTime都很好......你看到有什麼問題嗎? – 2011-05-10 16:04:37

+0

好吧...似乎我的評論的答案是你的另一個答案:http://stackoverflow.com/questions/5515193/my-first-custom-converter-whys-not-called/5515298#5515298。 – 2011-05-10 16:08:27

+0

噢,是的,IceFaces組件確實需要這些,或者其他原因。對不起,沒有考慮過,因爲我沒有使用它。無論如何你都歡迎:) – BalusC 2011-05-10 16:09:35