2
我想用JSF 2.3實現f:validateWholeBean
。 我試圖實現與Mojarra 2.3.0-m05
這個例子和Tomcat 8:f:validateWholeBean in JSF 2.3
<h:form>
<h:panelGroup>
<h:inputSecret id="passwd" value="#{bean.dataList['passwd']}">
<f:ajax event="blur" render="passwdvalidator" />
</h:inputSecret>
<h:message id="passwdvalidator" for="passwd" />
</h:panelGroup>
<h:panelGroup>Confirm Password</h:panelGroup>
<h:panelGroup>
<h:inputSecret id="confurmpasswd" value="#{bean.dataList['passwd']}">
<f:ajax event="blur" render="confurmpasswdvalidator" />
</h:inputSecret>
<h:message id="confurmpasswdvalidator" for="confurmpasswd" />
</h:panelGroup>
<h:commandButton action="#{bean.submit}">
<f:ajax render="@form" execute="@form"></f:ajax>
</h:commandButton>
<f:validateWholeBean value="#{contactBean}" validationGroups="validateBean.ContactGroup" />
</h:form>
自定義驗證
@Named
@ViewScoped
public class NewAccountValidator implements Validator, Serializable
{
@Override
public void validate(FacesContext fc, UIComponent uic, Object o) throws ValidatorException
{
// not used
}
public void validatePasswords(FacesContext context, UIComponent component, Object value)
{
String l;
String s = value.toString().trim();
if (s != null)
{
// compare passwords
}
else
{
throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_INFO,
s.isEmpty() ? " This field cannot be empty!" : " '" + s + "' is not a number!", null));
}
}
}
什麼是實現與f:validateWholeBean
和定製JSF驗證解決方案的正確方法?
謝謝你的例子。但我需要在驗證器中執行SQL查詢。你能告訴我一個快速的例子,我可以在哪部分代碼中進行查詢? –
所有的檢查都應該在* isValid *方法中完成,包括數據庫訪問。請參閱更新。 –
我還有一些問題:我可以使用'ViewScoped'還是強制使用'RequestScoped'?另外如果我有一個需要將幾個SQL查詢放入一個驗證器的情況呢?進入「經典」解決方案,我只是添加了額外的Java方法。請參閱 –