2012-04-24 157 views

回答

2

是的,你可以寫自己的驗證器類是這樣的:

@FacesValidator("customValidator") 
public class CustomValidator implements Validator{ 

    public void validate(FacesContext context, UIComponent component, 
      Object value) throws ValidatorException { 
     // do your validation here 
    } 
} 

,並用它在你的小面:

<h:inputText value="#{myBean.myValue}"> 
    <f:validator validatorId="customValidator" /> 
</h:inputText> 

注意,@FacesValidator註釋的內容必須與validatorId匹配屬性f:validator

此外,jsf輸入元素具有validator屬性。您可以使用此屬性將驗證委託給您的支持bean中的特殊方法。

public void validateInput(FacesContext context, 
          UIComponent component, Object object) { 
    // your validation with 'object' here 
} 
1
+0

這不是我想要的。它將驗證添加到jsf頁面。 – Kayser 2012-04-24 12:32:28

+0

它就在那裏,你需要實現Validator接口來創建你自己的驗證器。你想要什麼? – 2012-04-24 12:35:09

+0

在Bean中,您可以使用註釋檢查值。相似的東西。我想寫一個驗證器來驗證Bean中的輸入值。並且我不想在facelet – Kayser 2012-04-24 12:39:49

相關問題