我的英语不是很好,很抱歉关于that.My的问题是关于jsf validation.When我的验证bean抛出ValidatorException,我的受管bean save_method works.but我不希望我的save_method works.if电子邮件无效,我的save_method不能工作。
复合接口
<composite:attribute name="validator" required="false"
method-signature="void Action(javax.faces.context.FacesContext, javax.faces.component.UIComponent,Object)" />像这样的复合实现
<c:if test="#{cc.getValueExpression('validator')!=null}">
<p:inputText value="#{cc.attrs.value}" id="#{cc.attrs.id}"
required="#{cc.attrs.required}" validator="#{cc.attrs.validator}"/>验证bean中的验证方法。
@Override
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
String email = (String) value;
Pattern mask = null;
mask = Pattern.compile(EMAIL_REGEX);
Matcher matcher = mask.matcher(email);
if (!matcher.matches()) {
messageManager.warn(MessageManager.Bundles.WO, "validator.message.email");
throw new ValidatorException(new FacesMessage("hoppala"));
}
}和我的托管bean保存方法。
@Override
public boolean save() {
super.save();
}发布于 2012-09-16 06:50:26
您是否正在使用2.1.10版本以下的mojarra?这是最近修复的was a bug。因此,如果可以,请更新到2.1.13的最新版本。但不管怎样,在JSF中,您不会像您所描述的那样去做它。我建议使用<f:validator/>来应用验证器,如下所示:
<mytags:customInput value="#{validationModel.value}">
<f:validator for="input" validatorId="org.validationexample.EmailValidator" />
</mytags:customInput>并且您的复合组件必须实现<cc:editableValueHolder>
<cc:interface>
<cc:attribute name="value"/>
<cc:editableValueHolder name="input" targets="inputText"/>
</cc:interface>
<cc:implementation>
<p:inputText id="inputText" value="#{cc.attrs.value}" />
</cc:implementation>最后,您的验证由一个专用的验证器类处理:
@FacesValidator("org.validationexample.EmailValidator")
public class EmailValidator implements Validator {
@Override
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
// your validation here
}
}无论如何,还有其他的方法/风格。您还可以将<f:validator>直接放入<p:inputText>中的复合组件中。由于<f:validator>具有disabled属性,因此您可以按需禁用它。另一种方法是使用Bean验证,并使用Hibernate Validator附带的现有EmailValidator。在这种情况下,您只需将注释@Email设置在您的属性之上。
https://stackoverflow.com/questions/12436855
复制相似问题